117 lines
4.2 KiB
Python
117 lines
4.2 KiB
Python
# Docs inputs for the audio / music / video / subtitle guide pages (tools/fixtures.py; tools/docs_check.py reads them):
|
|
# tiny ffmpeg test clips (testsrc 64x48, 10 frames at 10 fps, with a sine track): MP4 (H.264 + AAC), AVI (MJPEG +
|
|
# PCM), WebM (VP8 + Vorbis); a MIDI file written here (SMF 1: a tempo track and a melody), the SoundFont of
|
|
# tools/fixtures/sf2.py (build_sf2), a ProTracker module from tools/fixtures/mod.py (song_basic), and SRT / WebVTT /
|
|
# ASS subtitles.
|
|
# tests/gen/docs_av.luau { files = { [name] = bytes }, vars = { mp4Bytes = "clip.mp4", ... } }
|
|
import importlib.util, os, struct, tempfile
|
|
|
|
INPUTS = ["tools/docs_check.py", "tools/fixtures/sf2.py", "tools/fixtures/mod.py"]
|
|
|
|
|
|
def load(ctx, name):
|
|
spec = importlib.util.spec_from_file_location("docs_av_" + name, os.path.join(ctx.root, "tools", name + ".py"))
|
|
m = importlib.util.module_from_spec(spec)
|
|
spec.loader.exec_module(m)
|
|
return m
|
|
|
|
|
|
def vlq(n):
|
|
out = [n & 0x7F]
|
|
n >>= 7
|
|
while n:
|
|
out.append(0x80 | (n & 0x7F))
|
|
n >>= 7
|
|
return bytes(reversed(out))
|
|
|
|
|
|
def track(events):
|
|
body = b"".join(vlq(dt) + data for dt, data in events) + b"\x00\xff\x2f\x00"
|
|
return b"MTrk" + struct.pack(">I", len(body)) + body
|
|
|
|
|
|
def midi():
|
|
"""SMF 1, 480 ticks per quarter: track 1 = name + tempo 120 bpm, track 2 = an eight-note melody on channel 0"""
|
|
t1 = track([(0, b"\xff\x03\x05Tempo"), (0, b"\xff\x51\x03" + (500000).to_bytes(3, "big")),
|
|
(0, b"\xff\x58\x04\x04\x02\x18\x08")])
|
|
ev = [(0, b"\xff\x03\x06Melody"), (0, b"\xc0\x00")]
|
|
for i, note in enumerate([60, 62, 64, 65, 67, 65, 64, 62]):
|
|
ev.append((0, bytes((0x90, note, 100))))
|
|
ev.append((240, bytes((0x80, note, 0))))
|
|
t2 = track(ev)
|
|
return b"MThd" + struct.pack(">IHHH", 6, 1, 2, 480) + t1 + t2
|
|
|
|
|
|
SRT = """1
|
|
00:00:00,500 --> 00:00:02,000
|
|
Hello, <i>world</i>.
|
|
|
|
2
|
|
00:00:02,500 --> 00:00:04,250
|
|
Second line
|
|
spans two rows.
|
|
"""
|
|
|
|
VTT = """WEBVTT
|
|
|
|
intro
|
|
00:00.500 --> 00:02.000 align:start
|
|
Hello, <b>world</b>.
|
|
|
|
00:02.500 --> 00:04.250
|
|
Second cue.
|
|
"""
|
|
|
|
ASS = """[Script Info]
|
|
Title: Docs
|
|
ScriptType: v4.00+
|
|
PlayResX: 640
|
|
PlayResY: 360
|
|
|
|
[V4+ Styles]
|
|
Format: Name, Fontname, Fontsize, PrimaryColour, SecondaryColour, OutlineColour, BackColour, Bold, Italic, Underline, StrikeOut, ScaleX, ScaleY, Spacing, Angle, BorderStyle, Outline, Shadow, Alignment, MarginL, MarginR, MarginV, Encoding
|
|
Style: Default,Arial,20,&H00FFFFFF,&H000000FF,&H00000000,&H00000000,0,0,0,0,100,100,0,0,1,2,0,2,10,10,10,1
|
|
Style: Sign,Arial,16,&H0000FFFF,&H000000FF,&H00000000,&H00000000,-1,0,0,0,100,100,0,0,1,1,0,8,10,10,10,1
|
|
|
|
[Events]
|
|
Format: Layer, Start, End, Style, Name, MarginL, MarginR, MarginV, Effect, Text
|
|
Dialogue: 0,0:00:00.50,0:00:02.00,Default,,0,0,0,,Hello, {\\i1}world{\\i0}.
|
|
Dialogue: 0,0:00:02.50,0:00:04.25,Sign,,0,0,0,,A sign\\Nwith two lines
|
|
"""
|
|
|
|
|
|
def generate(ctx):
|
|
dc = load(ctx, "docs_check")
|
|
files = {}
|
|
with tempfile.TemporaryDirectory() as d:
|
|
src = ["-f", "lavfi", "-i", "testsrc=size=64x48:rate=10:duration=1",
|
|
"-f", "lavfi", "-i", "sine=frequency=440:sample_rate=44100:duration=1"]
|
|
for name, codec in (
|
|
("clip.mp4", ["-c:v", "libx264", "-profile:v", "baseline", "-pix_fmt", "yuv420p", "-g", "5",
|
|
"-c:a", "aac", "-b:a", "64k"]),
|
|
("clip.avi", ["-c:v", "mjpeg", "-q:v", "4", "-pix_fmt", "yuvj420p", "-c:a", "pcm_s16le"]),
|
|
("clip.webm", ["-c:v", "libvpx", "-b:v", "200k", "-g", "5", "-c:a", "libvorbis", "-q:a", "2"]),
|
|
):
|
|
out = os.path.join(d, name)
|
|
ctx.nix("ffmpeg", ["ffmpeg", "-v", "error", "-y"] + src + codec + ["-shortest", out])
|
|
files[name] = open(out, "rb").read()
|
|
files["tune.mid"] = midi()
|
|
sf2 = load(ctx, "fixtures/sf2")
|
|
files["bank.sf2"] = sf2.build_sf2(False)[0]
|
|
mod = load(ctx, "fixtures/mod")
|
|
files["song.mod"] = mod.song_basic()
|
|
files["subs.srt"] = SRT
|
|
files["subs.vtt"] = VTT
|
|
files["subs.ass"] = ASS
|
|
dc.emit_inputs(ctx, files, {
|
|
"mp4Bytes": "clip.mp4",
|
|
"aviBytes": "clip.avi",
|
|
"webmBytes": "clip.webm",
|
|
"midiBytes": "tune.mid",
|
|
"sf2Bytes": "bank.sf2",
|
|
"modBytes": "song.mod",
|
|
"srtText": "subs.srt",
|
|
"vttText": "subs.vtt",
|
|
"assText": "subs.ass",
|
|
})
|