90 lines
5.2 KiB
Python
90 lines
5.2 KiB
Python
# containers/IsoBmff fixtures (tools/fixtures.py): small files from ffmpeg / MP4Box / avifenc (nix) with ffprobe's
|
|
# packet lists as the reference.
|
|
# tests/gen/isobmff.luau { cases = { { name, data, streams = { { codec, kind, timescale, width?, height?,
|
|
# sampleRate?, channels?, extradata }, ... }, packets = { { stream, pos, size, dts, pts, key },
|
|
# ... } } }, tagged = { data, tags, cover }, avif = { { name, data, width, height, alpha } } }
|
|
import json, os, shutil, subprocess, tempfile
|
|
|
|
PKGS = ["ffmpeg", "gpac", "libavif"]
|
|
|
|
|
|
def sh(ctx, script, cwd):
|
|
return ctx.nix(PKGS, ["sh", "-c", "cd '%s' && %s" % (cwd, script)])
|
|
|
|
|
|
def probe(ctx, path, cwd):
|
|
out = sh(ctx, "ffprobe -v error -ignore_editlist 1 -show_streams -show_packets -of json '%s'" % path, cwd)
|
|
j = json.loads(out)
|
|
streams = []
|
|
for s in j["streams"]:
|
|
kind = s["codec_type"]
|
|
if kind == "video" and s.get("disposition", {}).get("attached_pic"):
|
|
continue
|
|
tb = s["time_base"].split("/")
|
|
e = {"codec": s["codec_name"], "kind": kind, "timescale": int(tb[1]), "index": s["index"],
|
|
"extradata": int(s.get("extradata_size", 0))}
|
|
if kind == "video":
|
|
e["width"], e["height"] = s["width"], s["height"]
|
|
if kind == "audio":
|
|
e["sampleRate"], e["channels"] = int(s["sample_rate"]), s["channels"]
|
|
streams.append(e)
|
|
index = {s["index"]: i + 1 for i, s in enumerate(streams)}
|
|
packets = []
|
|
for p in j["packets"]:
|
|
if p["stream_index"] not in index:
|
|
continue
|
|
packets.append({"stream": index[p["stream_index"]], "pos": int(p["pos"]), "size": int(p["size"]),
|
|
"dts": int(p["dts"]), "pts": int(p["pts"]), "key": "K" in p["flags"]})
|
|
for s in streams:
|
|
del s["index"]
|
|
return streams, packets
|
|
|
|
|
|
def generate(ctx):
|
|
d = tempfile.mkdtemp(prefix="isobmff-fx-")
|
|
try:
|
|
src_v = "-f lavfi -i testsrc=size=64x48:rate=10 -t 1.2"
|
|
src_a = "-f lavfi -i sine=frequency=440:sample_rate=44100 -t 1.0"
|
|
steps = [
|
|
("h264_aac.mp4", "ffmpeg -v error %s %s -c:v libx264 -bf 2 -g 5 -pix_fmt yuv420p -c:a aac -b:a 64k -ac 2 h264_aac.mp4" % (src_v, src_a)),
|
|
("faststart.mp4", "ffmpeg -v error -i h264_aac.mp4 -c copy -movflags +faststart faststart.mp4"),
|
|
("mjpeg_pcm.mov", "ffmpeg -v error %s %s -c:v mjpeg -c:a pcm_s16be mjpeg_pcm.mov" % (src_v, src_a)),
|
|
("hevc.mp4", "ffmpeg -v error %s -c:v libx265 -x265-params log-level=none -tag:v hvc1 hevc.mp4" % src_v),
|
|
("frag.mp4", "ffmpeg -v error -i h264_aac.mp4 -c copy -movflags frag_keyframe+empty_moov+default_base_moof frag.mp4"),
|
|
("mp4box_frag.mp4", "MP4Box -quiet -frag 300 -out mp4box_frag.mp4 h264_aac.mp4 >/dev/null 2>&1"),
|
|
("stz2_co64.mp4", "MP4Box -quiet -add h264_aac.mp4#video -add h264_aac.mp4#audio:stz2 -co64 -new stz2_co64.mp4 >/dev/null 2>&1"),
|
|
("alac.m4a", "ffmpeg -v error %s -c:a alac alac.m4a" % src_a),
|
|
]
|
|
cases = []
|
|
for name, cmd in steps:
|
|
sh(ctx, cmd, d)
|
|
streams, packets = probe(ctx, name, d)
|
|
with open(os.path.join(d, name), "rb") as f:
|
|
data = f.read()
|
|
cases.append({"name": name, "data": data, "streams": streams, "packets": packets})
|
|
# tags + cover
|
|
sh(ctx, "ffmpeg -v error -f lavfi -i color=red:size=8x8 -frames:v 1 -update 1 cover.png", d)
|
|
tags = {"title": "Test Title", "artist": "Some Artist", "album": "An Album", "date": "2024",
|
|
"genre": "Electronic", "comment": "a comment", "composer": "Composer X", "album_artist": "AA"}
|
|
meta = " ".join("-metadata %s='%s'" % (k, v) for k, v in tags.items())
|
|
sh(ctx, "ffmpeg -v error %s -i cover.png -map 0 -map 1 -c:a aac -b:a 48k -c:v png -disposition:v attached_pic -t 1 %s -metadata track=3/12 -metadata disc=1/2 tagged.m4a" % (src_a, meta), d)
|
|
with open(os.path.join(d, "tagged.m4a"), "rb") as f:
|
|
tagged = f.read()
|
|
with open(os.path.join(d, "cover.png"), "rb") as f:
|
|
cover = f.read()
|
|
want = {"title": "Test Title", "artist": "Some Artist", "album": "An Album", "date": "2024",
|
|
"genre": "Electronic", "comment": "a comment", "composer": "Composer X", "albumartist": "AA",
|
|
"track": "3", "tracktotal": "12", "disc": "1", "disctotal": "2"}
|
|
# AVIF stills (libavif): plain and with alpha
|
|
avif = []
|
|
sh(ctx, "ffmpeg -v error -f lavfi -i testsrc=size=40x24 -frames:v 1 still.png && "
|
|
"ffmpeg -v error -f lavfi -i 'testsrc=size=40x24,format=rgba,geq=r=r(X\\,Y):a=X*6' -frames:v 1 alpha.png", d)
|
|
for name, png, alpha in (("still.avif", "still.png", False), ("alpha.avif", "alpha.png", True)):
|
|
sh(ctx, "avifenc -q 60 -s 10 %s %s >/dev/null" % (png, name), d)
|
|
with open(os.path.join(d, name), "rb") as f:
|
|
avif.append({"name": name, "data": f.read(), "width": 40, "height": 24, "alpha": alpha})
|
|
ctx.emit("isobmff", {"cases": cases, "tagged": {"data": tagged, "tags": want, "cover": len(cover)},
|
|
"avif": avif})
|
|
finally:
|
|
shutil.rmtree(d, ignore_errors=True)
|