server: /v1/videos writes WebM (VP9 and AV1) as well as MP4

A `format` field chooses the container a generated video is written in:
mp4 (the default, H.264 + AAC, unchanged) or webm (VP9 + Opus) or webm-av1
(AV1 + Opus). Old-hardware users -- this box's CPU is Zen 3, AVX2 only --
get the browser-friendly WebM the DaSiWa workflow ships without a fragile
client-side re-encode, and both webm codecs so either a VP9-friendly or an
AV1-parity file is available.

core/mm/mp4.{h,cpp} become core/mm/video_out.{h,cpp}: Mp4Writer generalises
to VideoWriter with a VideoFormat enum, branching the muxer, the video
encoder (libx264/libopenh264 for H.264, libvpx-vp9, libaom-av1/libsvtav1),
the rate-control options and the audio codec. Opus encodes only at 48 kHz,
so a WebM soundtrack is resampled to 48 kHz with libswresample (the model's
is 32 kHz); the encoder is fed packed FLT at its fixed frame size, AAC keeps
the planar FLTP path at its own rate. faststart and the shift_data reader are
MP4-only; WebM writes forward. Each codec/muxer is runtime-discovered and a
missing one fails open() with a message naming it, so the mp4 default always
works.

videos.cpp parses `format` (mp4|webm|webm-av1, mp4 default, refusing any
other name), threads it to the job and the writer, serves the file under the
matching content-type and a .webm/.mp4 Content-Disposition, echoes `format`
in the object, and advertises video_formats in the capability block.

The Docker FFmpeg build gains libvpx/libaom/libopus and the webm/matroska
muxers (all BSD, so the LGPL posture holds) and their runtime libraries.

Verified host-side: the videos_test webm case writes a real VP9+Opus file
(completed on libvpx-vp9, EBML magic + content-type + disposition checked);
a standalone writer harness produced mp4/webm/webm-av1 at 512x512 with stereo
32 kHz audio -- ffprobe reports h264+aac@32k, vp9+opus@48k and av1+opus@48k
respectively, and all three decode in software on this AVX2-only box.

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
gearwave00001andClaude committed 2026-10-20 11:51:52 -05:00
1 parent 0fe7771210
commit ad7e0f8b89
6 files changed
+331 -128

No files matched your search

+40
View File
@@ -154,6 +154,8 @@ TEST(a_video_request_that_cannot_be_served_names_its_field) {
std::string("num_inference_steps"));
CHECK_EQ(v.refused(Videos::body({ { "input_reference", "file-1" } })),
std::string("input_reference"));
/* `format` is a known field; a container it does not write names the field. */
CHECK_EQ(v.refused(Videos::body({ { "format", "avi" } })), std::string("format"));
/* fl2va is a valid task, but one that promises a first frame -- refused naming the missing
* part, not the task. */
CHECK_EQ(v.refused(Videos::body({ { "task", "fl2va" } })), std::string("first_frame"));
@@ -288,6 +290,7 @@ TEST(a_video_job_completes_and_serves_its_file) {
CHECK(o["completed_at"].is_number());
CHECK(o["expires_at"].get<int64_t>() > o["completed_at"].get<int64_t>());
CHECK(o["error"].is_null());
CHECK_EQ(str_at(o, "format"), std::string("mp4")); /* the default container */
HttpResponse c = v.f->get(("/v1/videos/" + id + "/content").c_str());
REQUIRE_EQ(c.status, 200);
CHECK_EQ(c.content_type, std::string("video/mp4"));
@@ -325,6 +328,43 @@ TEST(a_video_job_completes_and_serves_its_file) {
CHECK_EQ(v.f->get(("/v1/videos/" + id + "/content").c_str()).status, 404);
}
/* format=webm writes the same job as a WebM instead: an EBML header first, a VP9 video and an Opus
* soundtrack (the 32 kHz fake resampled to Opus's 48 kHz), served as video/webm under a .webm
* filename. The codecs are runtime-discovered, not a compile-time guarantee, so a build without a
* VP9/Opus encoder or a WebM muxer skips rather than fails. The capability block lists the
* containers format accepts. */
TEST(a_video_can_be_written_as_webm) {
Videos v;
HttpResponse r = v.create(Videos::body({ { "format", "webm" } }));
REQUIRE_EQ(r.status, 200);
json o0 = json::parse(r.body, nullptr, false);
CHECK_EQ(str_at(o0, "format"), std::string("webm")); /* echoed before a byte is encoded */
const std::string id = str_at(o0, "id");
json o = v.wait(id);
if (str_at(o, "status") == "failed") {
const std::string m = str_at(o["error"], "message");
if (has(m, "no libavcodec") || has(m, "VP9") || has(m, "Opus") || has(m, "WebM"))
SKIP("this build has no WebM encoder (VP9/Opus) to write the video with");
}
REQUIRE_EQ(str_at(o, "status"), std::string("completed"));
HttpResponse c = v.f->get(("/v1/videos/" + id + "/content").c_str());
REQUIRE_EQ(c.status, 200);
CHECK_EQ(c.content_type, std::string("video/webm"));
REQUIRE(c.body.size() > 64);
/* The EBML magic every WebM/Matroska file opens with. */
CHECK(c.body.compare(0, 4, std::string("\x1a\x45\xDF\xA3", 4)) == 0);
bool named = false;
for (const auto& kv : c.headers)
if (kv.first == "Content-Disposition" && has(kv.second, ".webm")) named = true;
CHECK(named);
HttpRequest mq;
mq.method = "GET";
mq.path = "/v1/models";
json caps =
json::parse(v.f->srv->router().dispatch(mq).body, nullptr, false)["data"][0]["capabilities"];
CHECK_EQ(caps["video_formats"], json::array({ "mp4", "webm", "webm-av1" }));
}
/* A request the generator fails is a failed job carrying the reason. */
TEST(a_failed_generation_is_a_failed_job_with_its_reason) {
Videos v;