ws-video: video/mp4 (MP4 / MOV / 3GP on containers/IsoBmff: edit lists, fragments, dOps -> OpusHead, PCM entries, AAC priming as codec delay; writer: copied H.264 / HEVC / AV1 / MJPEG / AAC / ALAC / Opus / FLAC / MP3, MJPEG from frames, PCM in MOV, edit lists for B-frame / codec delays); IsoBmff Mux writes no empty DecoderSpecificInfo (ffmpeg refuses it); writer checks extended to MP4 / MOV / H.264 remux
This commit is contained in:
1 parent
8d2deb13ab
commit
f2a4a33713
8 files changed
+475
-6
No files matched your search
@@ -29,6 +29,7 @@ is tested under both runtimes (luau `--codegen -O2` and LuaJIT through the build
|
||||
| audio | wav | .wav .wave .rf64 .bw64 | yes | yes | streaming: true, depths: 8, 16, 24, 32, float: true, rf64: true | `tests/audio/wav.luau` |
|
||||
| video | avi | .avi | yes | yes | streaming: true, video: "mjpeg", "mpeg1", "rawvideo", audio: true, odml: true | `tests/video/avi.luau` |
|
||||
| video | mkv | .mkv .webm .mka .mk3d | yes | yes | streaming: true, remux: true, video: "vp8", "mjpeg", "mpeg1", "rawvideo", audio: true, subtitles: true | `tests/video/mkv.luau` |
|
||||
| video | mp4 | .mp4 .m4v .mov .qt .3gp .3g2 .mj2 | yes | yes | streaming: true, remux: true, video: "mjpeg", "mpeg1", "h264", audio: true, fragments: true | `tests/video/mp4.luau` |
|
||||
| video | mpegps | .mpg .mpeg .vob .m1v .mpv .dat | yes | - | streaming: true, video: "mpeg1", audio: true | `tests/video/mpegps.luau` |
|
||||
| video | mpegts | .ts .m2ts .mts .tsv | yes | - | streaming: true, video: "mpeg1", "h264", audio: true | `tests/video/mpegts.luau` |
|
||||
| video | y4m | .y4m | yes | yes | streaming: true, video: "rawvideo", depths: 8, 10, 12, 16 | `tests/video/y4m.luau` |
|
||||
|
||||
@@ -302,6 +302,18 @@ local list: { Entry } = {
|
||||
return require("@formats/video/mkv")
|
||||
end,
|
||||
},
|
||||
{
|
||||
name = "mp4",
|
||||
category = "video",
|
||||
path = "video/mp4",
|
||||
extensions = { "mp4", "m4v", "mov", "qt", "3gp", "3g2", "mj2" },
|
||||
mime = { "video/mp4", "video/quicktime", "video/3gpp" },
|
||||
deps = { "core/Err", "core/Bytes", "Types", "containers/IsoBmff", "containers/Demux", "codecs/Video", "codecs/Video/Source" },
|
||||
caps = { decode = true, encode = true, streaming = true, remux = true, video = { "mjpeg", "mpeg1", "h264" }, audio = true, fragments = true },
|
||||
load = function()
|
||||
return require("@formats/video/mp4")
|
||||
end,
|
||||
},
|
||||
{
|
||||
name = "mpegps",
|
||||
category = "video",
|
||||
|
||||
@@ -92,7 +92,7 @@ local function esds(w: Bytes.Writer, t: MuxTrack)
|
||||
c:u24be(0)
|
||||
c:u32be(t.bitrate or 0)
|
||||
c:u32be(t.bitrate or 0)
|
||||
if t.config then
|
||||
if t.config and buffer.len(t.config) > 0 then -- (MJPEG / MP3 have no decoder-specific info)
|
||||
descriptor(c, 5, function(s)
|
||||
s:bytes(t.config :: buffer)
|
||||
end)
|
||||
|
||||
@@ -0,0 +1,324 @@
|
||||
--!strict
|
||||
-- video/mp4: MP4 / M4V / QuickTime MOV / 3GP (ISO/IEC 14496-12 / 14496-14, the QuickTime file format), on
|
||||
-- containers/IsoBmff (ws-m4a's box parser, sample tables, fragments and muxer). New in formats-luau.
|
||||
-- Read: every track's samples into a Demuxer (pts = dts + composition offset, moved by the edit list: the first
|
||||
-- edit's media time is subtracted (B-frame delay, AAC priming), an initial empty edit added; sync samples as
|
||||
-- keyframes; fragmented files too) and open() returns a VideoDecoder: video through the codecs/Video registry (MJPEG
|
||||
-- 'jpeg' / 'mjpa' / mp4v 0x6C, MPEG-1, raw; H.264 'avc1' with its avcC when a decoder is registered), audio through
|
||||
-- the api's opts.openAudio hook (AAC, ALAC (audio/m4a), MP3, Opus (dOps -> OpusHead), FLAC, PCM 'sowt' / 'twos' /
|
||||
-- 'lpcm' / 'ipcm').
|
||||
-- Write: MP4 (or MOV with opts.format = "mov" / a .mov destination): H.264 / HEVC / AV1 / MJPEG / MPEG-4 video copied
|
||||
-- from an ISO / Matroska source (their configurations are the same boxes), else MJPEG encoded from the frames (mp4v
|
||||
-- object type 0x6C in MP4, 'jpeg' in MOV); AAC / ALAC / Opus / FLAC / MP3 audio copied, else PCM s16 ('sowt', MOV
|
||||
-- only; MP4 drops other audio unless opts.audio = true, which fails).
|
||||
-- Edit lists are written for a B-frame delay and an audio codec delay (AAC priming). Not supported: fragmented
|
||||
-- writing, timed text tracks, encrypted tracks.
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ requires
|
||||
local Err = require("@formats/core/Err")
|
||||
local Bytes = require("@formats/core/Bytes")
|
||||
local Types = require("@formats/Types")
|
||||
local IsoBmff = require("@formats/containers/IsoBmff")
|
||||
local Demux = require("@formats/containers/Demux")
|
||||
local Video = require("@formats/codecs/Video")
|
||||
local Source = require("@formats/codecs/Video/Source")
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ contract
|
||||
local Mp4 = {
|
||||
name = "mp4",
|
||||
category = "video",
|
||||
extensions = { "mp4", "m4v", "mov", "qt", "3gp", "3g2", "mj2" },
|
||||
mime = { "video/mp4", "video/quicktime", "video/3gpp" },
|
||||
deps = { "core/Err", "core/Bytes", "Types", "containers/IsoBmff", "containers/Demux", "codecs/Video", "codecs/Video/Source" },
|
||||
caps = { decode = true, encode = true, streaming = true, remux = true, video = { "mjpeg", "mpeg1", "h264" }, audio = true, fragments = true },
|
||||
}
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ types
|
||||
export type OpenOptions = {
|
||||
rgba: boolean?,
|
||||
track: number?, -- the video track id (default: the first video track)
|
||||
audio: boolean?,
|
||||
maxPixels: number?,
|
||||
maxSamples: number?,
|
||||
openAudio: any?, -- set by the api
|
||||
}
|
||||
export type EncodeOptions = {
|
||||
format: string?, -- "mov" writes QuickTime (brand 'qt '); else MP4 (brand 'isom')
|
||||
quality: number?, -- MJPEG quality when frames are encoded (default 90)
|
||||
reencode: boolean?, -- encode the frames to MJPEG even when the video could be copied
|
||||
video: boolean?,
|
||||
audio: boolean?, -- false: no audio; true: fail when the audio cannot be written
|
||||
}
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ constants
|
||||
local IMAGE_BRANDS = { avif = true, avis = true, heic = true, heix = true, mif1 = true, msf1 = true, ["crx "] = true }
|
||||
local AUDIO_BRANDS = { ["M4A "] = true, ["M4B "] = true, ["M4P "] = true }
|
||||
-- copied video codecs -> sample entry (MP4 / MOV)
|
||||
local VIDEO_ENTRY: { [string]: string } = { h264 = "avc1", hevc = "hvc1", av1 = "av01", vp9 = "vp09" }
|
||||
local AUDIO_ENTRY: { [string]: string } = { aac = "mp4a", alac = "alac", opus = "Opus", flac = "fLaC", mp3 = "mp4a" }
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ sniff
|
||||
function Mp4.sniff(r: Bytes.Reader): number
|
||||
local brand = IsoBmff.sniff(r)
|
||||
if brand == nil or IMAGE_BRANDS[brand] then
|
||||
return 0
|
||||
end
|
||||
if AUDIO_BRANDS[brand] then
|
||||
return 0.2
|
||||
end
|
||||
if brand == "M4V " then
|
||||
return 0.9
|
||||
end
|
||||
return if brand == "" then 0.6 else 0.8
|
||||
end
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ decode
|
||||
-- OpusHead (RFC 7845, little-endian) from an ISO dOps box payload (big-endian)
|
||||
local function opusHead(d: buffer): buffer?
|
||||
if buffer.len(d) < 11 then
|
||||
return nil
|
||||
end
|
||||
local w = Bytes.writer(19 + buffer.len(d))
|
||||
w:string("OpusHead")
|
||||
w:u8(1)
|
||||
local ch = buffer.readu8(d, 1)
|
||||
w:u8(ch)
|
||||
w:u16le(buffer.readu8(d, 2) * 256 + buffer.readu8(d, 3))
|
||||
w:u32le(((buffer.readu8(d, 4) * 256 + buffer.readu8(d, 5)) * 256 + buffer.readu8(d, 6)) * 256 + buffer.readu8(d, 7))
|
||||
w:u16le(buffer.readu8(d, 8) * 256 + buffer.readu8(d, 9))
|
||||
local family = buffer.readu8(d, 10)
|
||||
w:u8(family)
|
||||
if family ~= 0 and buffer.len(d) >= 13 + ch then
|
||||
w:bytes(d, 11, 2 + ch) -- stream count, coupled count, mapping
|
||||
end
|
||||
return w:finish()
|
||||
end
|
||||
|
||||
local function pcmCodec(t: any): string
|
||||
local p = t.pcm
|
||||
local bits = if p then p.bits else (t.bitsPerSample or 16)
|
||||
if p and p.float then
|
||||
return "pcm_f" .. bits .. (if p.little then "le" else "be")
|
||||
end
|
||||
if bits == 8 then
|
||||
return if t.entry == "raw " then "pcm_u8" else "pcm_s8"
|
||||
end
|
||||
return "pcm_s" .. bits .. (if p == nil or p.little then "le" else "be")
|
||||
end
|
||||
|
||||
-- demux(r, opts?) -> the Demuxer | nil, Err
|
||||
function Mp4.demux(r: Bytes.Reader, opts: OpenOptions?): (any, Err.Err?)
|
||||
local ok, movie, err = pcall(IsoBmff.parse, r, opts and opts.maxSamples and { maxSamples = opts.maxSamples } or nil)
|
||||
if not ok then
|
||||
return nil, Err.new(Err.CORRUPT, "internal error: " .. tostring(movie))
|
||||
end
|
||||
if not movie then
|
||||
return nil, err
|
||||
end
|
||||
local tracks: { Types.Track } = {}
|
||||
local src = {}
|
||||
for _, it in movie.tracks do
|
||||
local t: any = {
|
||||
id = it.id,
|
||||
kind = it.kind,
|
||||
codec = it.codec,
|
||||
codecTag = it.entry,
|
||||
timescale = it.timescale,
|
||||
duration = it.duration,
|
||||
language = it.language,
|
||||
width = it.width,
|
||||
height = it.height,
|
||||
sampleRate = it.sampleRate,
|
||||
channels = it.channels,
|
||||
bitsPerSample = it.bitsPerSample,
|
||||
config = it.config,
|
||||
default = true,
|
||||
}
|
||||
if it.kind == "video" then
|
||||
if not Video.entry(t.codec) then
|
||||
t.codec = Video.fromFourcc(it.entry) or t.codec
|
||||
end
|
||||
if it.sampleCount > 0 and it.duration > 0 then
|
||||
t.frameRate = it.sampleCount / it.duration
|
||||
end
|
||||
elseif it.kind == "audio" then
|
||||
if it.codec == "pcm" then
|
||||
t.codec = pcmCodec(it)
|
||||
elseif it.codec == "ulaw" then
|
||||
t.codec = "pcm_mulaw"
|
||||
elseif it.codec == "alaw" then
|
||||
t.codec = "pcm_alaw"
|
||||
elseif it.codec == "opus" and it.config then
|
||||
t.config = opusHead(it.config)
|
||||
end
|
||||
if it.skip and it.skip > 0 then
|
||||
t.codecDelay = it.skip / it.timescale -- AAC priming: dropped like ffmpeg
|
||||
end
|
||||
end
|
||||
table.insert(tracks, t)
|
||||
table.insert(src, it)
|
||||
end
|
||||
local dmx = Demux.new(if movie.brand == "qt " then "mov" else "mp4", tracks, r.buf, { brand = movie.brand })
|
||||
-- samples in file order across tracks
|
||||
local items: { { number } } = {}
|
||||
for k, it in src do
|
||||
-- the edit list moves presentation times: minus the first edit's media time (video: the B-frame delay; audio
|
||||
-- keeps it as its codec delay), plus an initial empty edit (converted to media ticks)
|
||||
local shift = 0
|
||||
if it.kind == "video" then
|
||||
shift = -(it.skip or 0)
|
||||
end
|
||||
if it.delay and it.delay > 0 and movie.timescale and movie.timescale > 0 then
|
||||
shift += it.delay / movie.timescale * it.timescale
|
||||
end
|
||||
for i = 1, it.sampleCount do
|
||||
table.insert(items, { it.offsets[i], k, i, shift })
|
||||
end
|
||||
end
|
||||
table.sort(items, function(a: { number }, b: { number }): boolean
|
||||
return a[1] < b[1]
|
||||
end)
|
||||
for _, e in items do
|
||||
local it = src[e[2]]
|
||||
local i = e[3]
|
||||
local dts = it.dts[i]
|
||||
local pts = dts + (if it.cts then it.cts[i] or 0 else 0) + e[4]
|
||||
dmx:add(it.id, pts, dts + e[4], it.durations[i], it.sync == nil or it.sync[i] == true, it.offsets[i], it.sizes[i])
|
||||
end
|
||||
dmx:finish()
|
||||
return dmx, nil
|
||||
end
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ encode
|
||||
-- OpusHead -> dOps payload
|
||||
local function dOps(head: buffer?): buffer?
|
||||
if not head or buffer.len(head) < 19 then
|
||||
return nil
|
||||
end
|
||||
local w = Bytes.writer(16)
|
||||
w:u8(0)
|
||||
local ch = buffer.readu8(head, 9)
|
||||
w:u8(ch)
|
||||
w:u16be(buffer.readu16(head, 10))
|
||||
w:u32be(buffer.readu32(head, 12))
|
||||
w:u16be(buffer.readu16(head, 16))
|
||||
local family = buffer.readu8(head, 18)
|
||||
w:u8(family)
|
||||
if family ~= 0 and buffer.len(head) >= 21 + ch then
|
||||
w:bytes(head, 19, 2 + ch)
|
||||
end
|
||||
return w:finish()
|
||||
end
|
||||
|
||||
-- encode(video, opts?) -> buffer | nil, Err
|
||||
function Mp4.encode(value: any, opts: EncodeOptions?): (buffer?, Err.Err?)
|
||||
local o: any = table.clone(opts or {})
|
||||
o.quality = o.quality or 90
|
||||
local mov = type(o.format) == "string" and (string.lower(o.format) == "mov" or string.lower(o.format) == "qt")
|
||||
local vcopy: { [string]: boolean } = { mjpeg = true }
|
||||
for id in VIDEO_ENTRY do
|
||||
vcopy[id] = true
|
||||
end
|
||||
local acopy: { [string]: boolean } = {}
|
||||
for id in AUDIO_ENTRY do
|
||||
acopy[id] = true
|
||||
end
|
||||
acopy.pcm_s16le = mov
|
||||
local streams, err = Source.streams(value, o, {
|
||||
video = { copy = vcopy, encode = "mjpeg" },
|
||||
audio = { copy = acopy, pcm = mov },
|
||||
})
|
||||
if not streams then
|
||||
return nil, err
|
||||
end
|
||||
local mtracks = {}
|
||||
for _, s in streams do
|
||||
local scale = if s.kind == "audio" then math.floor((s.sampleRate or 48000) + 0.5) else 90000
|
||||
if s.kind == "video" and s.track and s.track.timescale and s.copied and s.track.timescale == math.floor(s.track.timescale) then
|
||||
scale = s.track.timescale
|
||||
end
|
||||
local samples, durations, cts, sync = {}, {}, {}, {}
|
||||
local anyCts = false
|
||||
local firstDts = nil :: number?
|
||||
for i, p in s.packets do
|
||||
samples[i] = p.data
|
||||
local dts = p.dts or p.pts
|
||||
firstDts = firstDts or dts
|
||||
local nextP = s.packets[i + 1]
|
||||
local d = if nextP then (nextP.dts or nextP.pts) - dts else p.duration
|
||||
durations[i] = math.max(0, math.floor(d * scale + 0.5))
|
||||
local c = math.floor((p.pts - dts) * scale + 0.5)
|
||||
cts[i] = c
|
||||
if c ~= 0 then
|
||||
anyCts = true
|
||||
end
|
||||
if p.keyframe then
|
||||
sync[i] = true
|
||||
end
|
||||
end
|
||||
-- an edit list keeps the source's start: video decode times before 0 (B-frame delay), audio codec delay
|
||||
local skip = 0
|
||||
if firstDts and firstDts < 0 then
|
||||
skip = math.floor(-firstDts * scale + 0.5)
|
||||
end
|
||||
if s.kind == "audio" and s.track and s.track.codecDelay and s.copied then
|
||||
skip = math.floor(s.track.codecDelay * scale + 0.5)
|
||||
end
|
||||
local t: any = {
|
||||
kind = s.kind,
|
||||
timescale = scale,
|
||||
skip = if skip > 0 then skip else nil,
|
||||
samples = samples,
|
||||
durations = durations,
|
||||
cts = if anyCts then cts else nil,
|
||||
sync = sync,
|
||||
width = s.width,
|
||||
height = s.height,
|
||||
sampleRate = s.sampleRate and math.floor(s.sampleRate + 0.5),
|
||||
channels = s.channels,
|
||||
}
|
||||
if s.kind == "video" then
|
||||
if s.codec == "mjpeg" then
|
||||
t.entry = if mov then "jpeg" else "mp4v"
|
||||
if not mov then
|
||||
t.objectType = 0x6C
|
||||
t.configBox = "esds"
|
||||
t.config = buffer.create(0)
|
||||
end
|
||||
else
|
||||
t.entry = VIDEO_ENTRY[s.codec]
|
||||
t.config = s.config
|
||||
end
|
||||
else
|
||||
if s.codec == "pcm_s16le" then
|
||||
t.entry = "sowt"
|
||||
t.bitsPerSample = 16
|
||||
else
|
||||
t.entry = AUDIO_ENTRY[s.codec]
|
||||
t.config = if s.codec == "opus" then dOps(s.config) else s.config
|
||||
if s.codec == "mp3" then
|
||||
t.objectType = 0x6B
|
||||
t.configBox = "esds"
|
||||
t.config = buffer.create(0)
|
||||
end
|
||||
end
|
||||
end
|
||||
if not t.entry then
|
||||
return nil, Err.new(Err.UNSUPPORTED, "no sample entry for " .. s.kind .. " codec " .. s.codec)
|
||||
end
|
||||
table.insert(mtracks, t)
|
||||
end
|
||||
return IsoBmff.mux({ brand = if mov then "qt " else "isom", compatible = if mov then { "qt " } else { "isom", "iso2", "mp41" }, tracks = mtracks })
|
||||
end
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ open
|
||||
-- open(r, opts?) -> a VideoDecoder | nil, Err
|
||||
function Mp4.open(r: Bytes.Reader, opts: OpenOptions?): (Types.VideoDecoder?, Err.Err?)
|
||||
local dmx, err = Mp4.demux(r, opts)
|
||||
if not dmx then
|
||||
return nil, err
|
||||
end
|
||||
return Video.open(dmx, opts)
|
||||
end
|
||||
|
||||
return Mp4
|
||||
@@ -0,0 +1,74 @@
|
||||
-- video/mp4 tests: MP4 / MOV files from ffmpeg (MJPEG + AAC / PCM / FLAC / MP2, H.264 with B-frames and edit lists +
|
||||
-- AAC, fragmented MP4, MPEG-1 + Opus): tracks and every packet against ffprobe (pts moved by the edit lists, as ffmpeg),
|
||||
-- frames against ffmpeg (PSNR), audio through the api hook (AAC priming dropped), seeking, our MP4 / MOV writer read
|
||||
-- back, fuzz. Fixtures: tools/fixtures/mp4.py.
|
||||
local T = require("@tests/T")
|
||||
local C = require("@tests/video/_common")
|
||||
local F = require("@formats")
|
||||
local Mp4 = require("@formats/video/mp4")
|
||||
local Bytes = require("@formats/core/Bytes")
|
||||
local Fx = require("@tests/gen/mp4_cases")
|
||||
|
||||
local t = T.suite("mp4")
|
||||
t:eq(Mp4.name, "mp4", "name")
|
||||
|
||||
local EXACT_AUDIO = { pcm_s16le = true, flac = true }
|
||||
local byName = {}
|
||||
for _, c in Fx do
|
||||
byName[c.name] = c
|
||||
end
|
||||
|
||||
for _, c in Fx do
|
||||
local label = c.name
|
||||
t:check(Mp4.sniff(Bytes.reader(buffer.fromstring(c.data))) >= 0.6, label .. ": sniff")
|
||||
local v, err = C.open(c.data)
|
||||
t:check(v ~= nil, label .. ": open " .. tostring(err))
|
||||
if v then
|
||||
local dmx = v.demuxer
|
||||
t:eq(#dmx.tracks, #c.streams, label .. ": track count")
|
||||
C.packets(t, dmx, c, label)
|
||||
C.frames(t, v, c, label, 38)
|
||||
local at = dmx:track("audio")
|
||||
if at then
|
||||
C.audio(t, v, c, label, EXACT_AUDIO[at.codec], 30)
|
||||
end
|
||||
C.seek(t, v, c, label, 0.5)
|
||||
end
|
||||
end
|
||||
|
||||
-- our writer: MP4 (MJPEG + AAC copied) and MOV (MJPEG + PCM) read back packet for packet
|
||||
for _, job in { { "mjpeg_aac.mp4", "mp4" }, { "mjpeg_pcm.mov", "mov" }, { "h264_aac.mp4", "mp4" } } do
|
||||
local src = byName[job[1]]
|
||||
local v = C.open(src.data)
|
||||
local out, err = F.save(v, nil, { format = job[2] })
|
||||
t:check(out ~= nil, "write " .. job[2] .. " from " .. job[1] .. ": " .. tostring(err))
|
||||
if out then
|
||||
local v2 = F.video.open(out)
|
||||
t:check(v2 ~= nil, "write " .. job[1] .. ": reads back")
|
||||
if v2 then
|
||||
local a, b = v.demuxer, v2.demuxer
|
||||
local same = #a.tracks == #b.tracks
|
||||
for i, tr in a.tracks do
|
||||
local tr2 = b.tracks[i]
|
||||
if not tr2 or a:count(tr.id) ~= b:count(tr2.id) then
|
||||
same = false
|
||||
else
|
||||
for k = 1, a:count(tr.id) do
|
||||
local p, q = a:get(tr.id, k), b:get(tr2.id, k)
|
||||
if not q or buffer.tostring(p.data) ~= buffer.tostring(q.data) or math.abs(p.pts / tr.timescale - q.pts / tr2.timescale) > 0.002 then
|
||||
same = false
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
t:check(same, "write " .. job[1] .. ": same packets and times")
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
local samples = {}
|
||||
for _, n in { "mjpeg_aac.mp4", "fragmented.mp4", "h264_aac.mp4" } do
|
||||
table.insert(samples, string.sub(byName[n].data, 1, 30000))
|
||||
end
|
||||
t:fuzz("open", C.fuzzOpen(Mp4), samples, { mutations = 6, garbage = 100 })
|
||||
t:done()
|
||||
@@ -1,9 +1,10 @@
|
||||
-- tools/fixtures/_video_encode: run by tools/fixtures/videoenc.py (luau, from the repo root) so ffprobe / ffmpeg can
|
||||
-- check our video writers: remuxes and re-encodes the container fixtures (tests/gen/mkv_cases, avi_cases) and the
|
||||
-- check our video writers: remuxes and re-encodes the container fixtures (tests/gen/mkv_cases, avi_cases, mp4_cases) and the
|
||||
-- animated GIF of tests/gen/videoenc_src, and prints "<name> <hex>" lines (or "<name> ERR <message>").
|
||||
local F = require("@formats")
|
||||
local Mkv = require("@tests/gen/mkv_cases")
|
||||
local Avi = require("@tests/gen/avi_cases")
|
||||
local Mp4 = require("@tests/gen/mp4_cases")
|
||||
local Src = require("@tests/gen/videoenc_src")
|
||||
|
||||
local function hex(b: buffer): string
|
||||
@@ -34,6 +35,12 @@ local JOBS = {
|
||||
{ "gif.mkv", Src.gif, "mkv", { quality = 95 } },
|
||||
{ "gif.y4m", Src.gif, "y4m", {} },
|
||||
{ "webm.y4m", find(Mkv, "vp8_opus.webm"), "y4m", {} },
|
||||
{ "gif.mp4", Src.gif, "mp4", { quality = 95 } },
|
||||
{ "pcm.mov", find(Mkv, "mjpeg_pcm.mkv"), "mov", {} },
|
||||
{ "h264_aac.mov", find(Mp4, "h264_aac.mp4"), "mov", {} },
|
||||
{ "h264_aac.mkv", find(Mp4, "h264_aac.mp4"), "mkv", {} },
|
||||
{ "vp8_mjpeg.mp4", find(Mkv, "vp8_opus.webm"), "mp4", { quality = 92 } },
|
||||
{ "flac.mp4", find(Mp4, "mjpeg_flac.mp4"), "mp4", {} },
|
||||
}
|
||||
for _, j in JOBS do
|
||||
local name, data, fmt, opts = j[1], j[2], j[3], j[4]
|
||||
|
||||
@@ -0,0 +1,50 @@
|
||||
# MP4 / MOV fixtures (tools/fixtures.py): files made by ffmpeg (MJPEG + AAC MP4, MJPEG + PCM MOV, H.264 with B-frames
|
||||
# + AAC (edit lists), fragmented MP4, MPEG-1 + Opus, MJPEG + FLAC, MJPEG + MP2 MOV) with ffprobe's streams and packets,
|
||||
# ffmpeg's frames (CRC-32; the first raw) and audio (f32). Our MP4 / MOV writer's files are checked in
|
||||
# tools/fixtures/videoenc.py.
|
||||
# tests/gen/mp4_cases.luau { { name, data, streams, packets, frames, yuv, pcm, pcmBytes } }
|
||||
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=12"]
|
||||
|
||||
|
||||
def aud(rate, ch=1):
|
||||
return ["-f", "lavfi", "-i", "sine=frequency=550:sample_rate=%d" % rate, "-ac", str(ch)]
|
||||
|
||||
|
||||
def case(tmp, name):
|
||||
path = os.path.join(tmp, name)
|
||||
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[:2]
|
||||
if any(s["kind"] == "audio" for s in c["streams"]):
|
||||
pcm = V.pcm_f32(path)
|
||||
c["pcm"] = pcm[:8192 * 4]
|
||||
c["pcmBytes"] = len(pcm)
|
||||
return c
|
||||
|
||||
|
||||
def generate(ctx):
|
||||
tmp = tempfile.mkdtemp(prefix="mp4fx_")
|
||||
ff = lambda *a: V.ffmpeg(list(a), cwd=tmp)
|
||||
ff(*SRC, *aud(44100), "-t", "1", "-c:v", "mjpeg", "-q:v", "4", "-c:a", "aac", "-b:a", "96k", "mjpeg_aac.mp4")
|
||||
ff(*SRC, *aud(48000, 2), "-t", "1", "-c:v", "mjpeg", "-c:a", "pcm_s16le", "mjpeg_pcm.mov")
|
||||
ff(*SRC, *aud(44100), "-t", "1", "-c:v", "libx264", "-preset", "veryfast", "-bf", "2", "-g", "6", "-c:a", "aac",
|
||||
"h264_aac.mp4")
|
||||
ff(*SRC, *aud(48000), "-t", "1", "-c:v", "mjpeg", "-c:a", "aac", "-movflags", "frag_keyframe+empty_moov",
|
||||
"fragmented.mp4")
|
||||
ff(*SRC, *aud(48000), "-t", "1", "-r", "25", "-c:v", "mpeg1video", "-c:a", "libopus", "mpeg1_opus.mp4")
|
||||
ff(*SRC, *aud(44100, 2), "-t", "1", "-c:v", "mjpeg", "-c:a", "flac", "mjpeg_flac.mp4")
|
||||
ff(*SRC, *aud(44100), "-t", "1", "-c:v", "mjpeg", "-c:a", "mp2", "mjpeg_mp2.mov")
|
||||
cases = [case(tmp, n) for n in ("mjpeg_aac.mp4", "mjpeg_pcm.mov", "h264_aac.mp4", "fragmented.mp4",
|
||||
"mpeg1_opus.mp4", "mjpeg_flac.mp4", "mjpeg_mp2.mov")]
|
||||
ctx.emit("mp4_cases", cases)
|
||||
@@ -1,6 +1,7 @@
|
||||
# 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 from VP8 and MPEG-1, MJPEG copies, an animated
|
||||
# GIF with uneven delays to AVI / Matroska / Y4M, WebM to Y4M); each output must probe in ffprobe and decode in
|
||||
# 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> }
|
||||
@@ -11,8 +12,8 @@ _spec = importlib.util.spec_from_file_location("_video_helpers", os.path.join(os
|
||||
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",
|
||||
"src/Formats/video", "src/Formats/codecs/Video", "src/Formats/containers/Ebml", "src/Formats/containers/Avi",
|
||||
INPUTS = ["tools/fixtures/_video.py", "tools/fixtures/_video_encode.luau", "tools/fixtures/mkv.py", "tools/fixtures/avi.py", "tools/fixtures/mp4.py",
|
||||
"src/Formats/video", "src/Formats/codecs/Video", "src/Formats/containers/Ebml", "src/Formats/containers/Avi", "src/Formats/containers/IsoBmff",
|
||||
"src/Formats/codecs/Jpeg", "src/Formats/codecs/Vp8"]
|
||||
|
||||
GIF = r"""
|
||||
|
||||
Reference in new issue
Block a user