Files
rosetta/tools/fixtures/videoenc.py
T

64 lines
3.3 KiB
Python

# Our video writers checked by ffprobe / ffmpeg (tools/fixtures.py): tools/fixtures/_video_encode.luau remuxes /
# re-encodes the container fixtures (WebM remux, MJPEG Matroska / AVI / MP4 from VP8 and MPEG-1, MJPEG copies, H.264 +
# AAC MP4 to MOV / Matroska, PCM MOV, FLAC MP4, an animated GIF with uneven delays to AVI / Matroska / Y4M / MP4, WebM
# to Y4M); each output must probe in ffprobe and decode in
# ffmpeg. Emitted per file: our bytes, ffprobe's streams and packets, ffmpeg's frames (CRC-32; the first raw) and
# audio, so the test can decode our file with our decoders and compare.
# tests/gen/videoenc_src.luau { gif = <animated GIF made by Pillow: 4 frames, 100 / 50 / 200 / 100 ms> }
# tests/gen/videoenc_cases.luau { { name, data, streams, packets, frames, yuv, pcm, pcmBytes } | { name, err } }
import importlib.util, os, shutil, subprocess, 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", "tools/fixtures/_video_encode.luau", "tools/fixtures/mkv.py", "tools/fixtures/avi.py", "tools/fixtures/mp4.py",
"src/Rosetta/video", "src/Rosetta/codecs/video/Video", "src/Rosetta/containers/Ebml", "src/Rosetta/containers/Avi", "src/Rosetta/containers/IsoBmff",
"src/Rosetta/codecs/image/Jpeg", "src/Rosetta/codecs/image/Vp8"]
GIF = r"""
import io, sys
from PIL import Image, ImageDraw
frames = []
for i, c in enumerate([(255, 0, 0), (0, 160, 0), (0, 0, 255), (240, 200, 0)]):
im = Image.new("RGB", (64, 48), (20, 20, 20))
d = ImageDraw.Draw(im)
d.rectangle([4 + i * 10, 8, 24 + i * 10, 40], fill=c)
d.ellipse([40, 4 + i * 6, 60, 20 + i * 6], fill=(255, 255, 255))
frames.append(im)
b = io.BytesIO()
frames[0].save(b, "GIF", save_all=True, append_images=frames[1:], duration=[100, 50, 200, 100], loop=0)
sys.stdout.buffer.write(b.getvalue())
"""
def generate(ctx):
tmp = tempfile.mkdtemp(prefix="videoenc_")
gif = ctx.python(["pillow"], GIF)
ctx.emit("videoenc_src", {"gif": gif})
luau = os.environ.get("LUAU") or shutil.which("luau")
cmd = [luau, "tools/fixtures/_video_encode.luau"] if luau else ["nix", "shell", "nixpkgs#luau", "-c", "luau", "tools/fixtures/_video_encode.luau"]
out = subprocess.run(cmd, capture_output=True, check=True, cwd=ctx.root).stdout.decode()
cases = []
for line in out.split("\n"):
if not line.strip():
continue
name, rest = line.split(" ", 1)
if rest.startswith("ERR "):
cases.append({"name": name, "err": rest[4:]})
continue
path = os.path.join(tmp, name)
open(path, "wb").write(bytes.fromhex(rest))
c = {"name": name, "data": open(path, "rb").read(), "streams": V.streams(path), "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"], codec=vs[0]["codec"])
c["frames"] = [V.crc(f) for f in frames]
c["yuv"] = frames[:3]
if any(s["kind"] == "audio" for s in c["streams"]):
pcm = V.pcm_f32(path)
c["pcm"] = pcm[:8192 * 4]
c["pcmBytes"] = len(pcm)
cases.append(c)
ctx.emit("videoenc_cases", cases)