Files
rosetta/tools/fixtures/mpegts.py
T

60 lines
3.3 KiB
Python

# MPEG-TS / PS fixtures (tools/fixtures.py): transport streams (MPEG-1 + MP2 as bugtv plays them, with B-frames,
# H.264 + AAC, M2TS 192-byte packets, garbage before the first packet) and program streams (MPEG-1 system stream,
# MPEG-2 PS / VOB layout, B-frames, a raw .m1v elementary stream), made by ffmpeg. For each file: ffprobe's streams
# and packets, ffmpeg's frames (CRC-32 of yuv420p; the first frames raw) and audio (f32).
# tests/gen/mpegts_cases.luau, tests/gen/mpegps_cases.luau
import importlib.util, os, tempfile
_spec = importlib.util.spec_from_file_location("_video_helpers", os.path.join(os.path.dirname(os.path.abspath(__file__)), "_video.py"))
V = importlib.util.module_from_spec(_spec)
_spec.loader.exec_module(V)
INPUTS = ["tools/fixtures/_video.py"]
SRC = ["-f", "lavfi", "-i", "testsrc2=size=160x120:rate=25"]
AUD = ["-f", "lavfi", "-i", "sine=frequency=440:sample_rate=44100"]
def case(tmp, name, raw_frames=3, pcm_frames=8192, packets=True):
path = os.path.join(tmp, name)
c = {"name": name, "data": open(path, "rb").read(), "streams": V.streams(path)}
if packets:
c["packets"] = V.packets(path)
vs = [s for s in c["streams"] if s["kind"] == "video"]
if vs:
frames = V.yuv_frames(path, vs[0]["width"], vs[0]["height"])
c["frames"] = [V.crc(f) for f in frames]
c["yuv"] = frames[:raw_frames]
if any(s["kind"] == "audio" for s in c["streams"]):
pcm = V.pcm_f32(path)
c["pcm"] = pcm[:pcm_frames * 4]
c["pcmBytes"] = len(pcm)
return c
def generate(ctx):
tmp = tempfile.mkdtemp(prefix="mpegfx_")
ff = lambda *a: V.ffmpeg(list(a), cwd=tmp)
ff(*SRC, *AUD, "-t", "1", "-c:v", "mpeg1video", "-b:v", "600k", "-bf", "0", "-c:a", "mp2", "-b:a", "128k",
"-f", "mpegts", "mpeg1_mp2.ts")
ff(*SRC, *AUD, "-t", "1", "-c:v", "mpeg1video", "-b:v", "600k", "-bf", "2", "-c:a", "mp2", "-f", "mpegts",
"bframes.ts")
ff(*SRC, *AUD, "-t", "1", "-c:v", "libx264", "-preset", "veryfast", "-g", "10", "-c:a", "aac", "-b:a", "96k",
"-f", "mpegts", "h264_aac.ts")
ff(*SRC, "-t", "1", "-c:v", "mpeg1video", "-b:v", "600k", "-f", "mpegts", "-mpegts_m2ts_mode", "1", "m2ts.m2ts")
open(os.path.join(tmp, "garbage.ts"), "wb").write(bytes(ctx.rng(3).randrange(256) for _ in range(333)).replace(b"G", b"g") +
open(os.path.join(tmp, "mpeg1_mp2.ts"), "rb").read())
ts = [case(tmp, "mpeg1_mp2.ts"), case(tmp, "bframes.ts"), case(tmp, "h264_aac.ts"), case(tmp, "m2ts.m2ts"),
case(tmp, "garbage.ts", packets=False)]
full = ts[0]["data"]
ts.append({"name": "truncated.ts", "data": full[: len(full) // 2 + 77], "truncated": True})
ctx.emit("mpegts_cases", ts)
ff(*SRC, *AUD, "-t", "1", "-c:v", "mpeg1video", "-b:v", "600k", "-bf", "0", "-c:a", "mp2", "-f", "mpeg", "system.mpg")
ff(*SRC, *AUD, "-t", "1", "-c:v", "mpeg1video", "-b:v", "600k", "-bf", "2", "-c:a", "mp2", "-f", "mpeg",
"bframes.mpg")
ff(*SRC, *AUD, "-t", "1", "-c:v", "mpeg1video", "-b:v", "600k", "-c:a", "mp2", "-f", "vob", "mpeg2ps.vob")
ff(*SRC, "-t", "1", "-c:v", "mpeg1video", "-b:v", "600k", "-bf", "2", "-f", "mpeg1video", "es.m1v")
ps = [case(tmp, n, packets=False) for n in ("system.mpg", "bframes.mpg", "mpeg2ps.vob", "es.m1v")]
ctx.emit("mpegps_cases", ps)