Hardcode fewer things. Convert tabs to spaces.

This commit is contained in:
yrriban 2025-06-12 23:47:22 -04:00
parent 2e9b8b148e
commit 97616b341d

View file

@ -35,6 +35,9 @@ class Concat(dcc.doom_base.Wad):
continue continue
chunk = av.open(v) chunk = av.open(v)
if not (len(chunk.streams.video) == 1 and len(chunk.streams.audio) == 1):
raise Exception(f"irregular chunk {v}: streams {chunk.streams} (expected 1 video & 1 audio)")
ograph = av.filter.Graph() ograph = av.filter.Graph()
sink = ograph.add("buffersink") sink = ograph.add("buffersink")
asink = ograph.add("abuffersink") asink = ograph.add("abuffersink")
@ -52,35 +55,45 @@ class Concat(dcc.doom_base.Wad):
padfactor=8 padfactor=8
img.border("transparent", padfactor, 0) img.border("transparent", padfactor, 0)
img.crop(width=img.width-img.width%padfactor, height=img.height) img.crop(width=img.width-img.width%padfactor, height=img.height)
text_frame = av.video.frame.VideoFrame(img.width, img.height, format="rgba")
if len(output.streams.get()) == 0: if len(output.streams.get()) == 0:
# TODO: less hardcoding. # We can't use the input stream as a template here; it doesn't
output.add_stream("h264", rate=61440) # have everything needed to do encoding and will fail
output.streams[0].extradata = copy.deepcopy(chunk.streams[0].extradata) # mysteriously later.
output.streams[0].height=1440 vs = chunk.streams.video[0]
output.streams[0].width=2560 output.add_stream("h264", rate=int(vs.time_base.denominator/vs.time_base.numerator))
output.streams[0].extradata = copy.deepcopy(vs.extradata)
output.streams[0].height=vs.height
output.streams[0].width=vs.width
output.streams[0].qmax = vs.qmax
output.streams[0].qmin = vs.qmin
output.streams[0].codec_context.bit_rate = vs.codec_context.bit_rate
output.streams[0].codec_context.framerate = vs.base_rate
output.streams[0].codec_context.pix_fmt = vs.codec_context.pix_fmt
# The following are only used for encoding and have no equivalent on the input stream.
output.streams[0].profile="High" output.streams[0].profile="High"
output.streams[0].qmax = chunk.streams[0].qmax
output.streams[0].qmin = chunk.streams[0].qmin
output.streams[0].codec_context.gop_size=30 output.streams[0].codec_context.gop_size=30
output.streams[0].codec_context.max_b_frames=2 output.streams[0].codec_context.max_b_frames=2
output.streams[0].codec_context.framerate = fractions.Fraction(60,1)
output.streams[0].codec_context.pix_fmt="yuv420p" astr = chunk.streams.audio[0]
output.streams[0].codec_context.bit_rate = chunk.streams[0].codec_context.bit_rate output.add_stream("aac", rate=astr.rate)
output.add_stream("aac", rate=48000) output.streams[1].extradata = copy.deepcopy(astr.extradata)
output.streams[1].extradata = copy.deepcopy(output.streams[1].extradata) output.streams[1].bit_rate=astr.bit_rate
output.streams[1].rate=48000
output.streams[1].bit_rate=chunk.streams[1].bit_rate src = ograph.add_buffer(template=chunk.streams.video[0], time_base=chunk.streams.video[0].time_base)
src = ograph.add_buffer(template=chunk.streams[0], time_base=chunk.streams[0].time_base) asrc = ograph.add_abuffer(template=chunk.streams.audio[0], time_base=chunk.streams.audio[0].time_base)
asrc = ograph.add_abuffer(template=chunk.streams[1], time_base=chunk.streams[1].time_base) # TODO: video fades are absolute relative to the input video; audio
ifade = ograph.add("fade", args="in:0:60") # fades need to have their timestamps offset by the position in the
iafade = ograph.add("afade", args="in:{}:48000".format(offset*48000/1000000)) # final video. Clarify if this is really necessary.
ofade = ograph.add("fade", args="out:{}:60".format((chunk.duration*60/1000000)-60)) frame_rate = chunk.streams.video[0].base_rate
oafade = ograph.add("afade", args="out:{}:48000".format(((offset+chunk.duration)*48000/1000000)-48000)) sample_rate = chunk.streams.audio[0].rate
ifade = ograph.add("fade", args="in:0:{}".format(frame_rate))
ofade = ograph.add("fade", args="out:{}:{}".format((chunk.duration*frame_rate/1000000)-frame_rate, frame_rate))
iafade = ograph.add("afade", args="in:{}:{}".format(offset*sample_rate/1000000, sample_rate))
oafade = ograph.add("afade", args="out:{}:{}".format(((offset+chunk.duration)*sample_rate/1000000)-sample_rate, sample_rate))
if not parsed_args.nooverlay: if not parsed_args.nooverlay:
overlay = ograph.add_buffer(width=img.width, height=img.height, format="rgba", time_base=chunk.streams[0].time_base) overlay = ograph.add_buffer(width=img.width, height=img.height, format="rgba", time_base=chunk.streams[0].time_base)
overlay_fo = ograph.add("fade", args="out:240:60") overlay_fo = ograph.add("fade", args="out:{}:{}".format(4*frame_rate, frame_rate))
overlay.link_to(overlay_fo, 0, 0) overlay.link_to(overlay_fo, 0, 0)
composite = ograph.add("overlay", args="x=4:y=4") composite = ograph.add("overlay", args="x=4:y=4")
src.link_to(composite, 0, 0) src.link_to(composite, 0, 0)
@ -88,19 +101,20 @@ class Concat(dcc.doom_base.Wad):
composite.link_to(ifade, 0, 0) composite.link_to(ifade, 0, 0)
else: else:
src.link_to(ifade, 0, 0) src.link_to(ifade, 0, 0)
asrc.link_to(iafade, 0, 0) asrc.link_to(iafade, 0, 0)
ifade.link_to(ofade, 0, 0) ifade.link_to(ofade, 0, 0)
iafade.link_to(oafade, 0, 0) iafade.link_to(oafade, 0, 0)
ofade.link_to(sink, 0, 0) ofade.link_to(sink, 0, 0)
oafade.link_to(asink, 0, 0) oafade.link_to(asink, 0, 0)
ograph.configure() ograph.configure()
for packet in chunk.demux(): for packet in chunk.demux():
if packet.dts is None: if packet.dts is None:
continue continue
packet.dts += (offset * packet.time_base.denominator) / (packet.time_base.numerator * 1000000) packet.dts += (offset * packet.time_base.denominator) / (packet.time_base.numerator * 1000000)
packet.pts += (offset * packet.time_base.denominator) / (packet.time_base.numerator * 1000000) packet.pts += (offset * packet.time_base.denominator) / (packet.time_base.numerator * 1000000)
if packet.stream_index == 0: # TODO: robustness if packet.stream == chunk.streams.video[0]:
for ifr in packet.decode(): for ifr in packet.decode():
if not parsed_args.nooverlay: if not parsed_args.nooverlay:
text_frame = av.video.frame.VideoFrame(img.width, img.height, format="rgba") text_frame = av.video.frame.VideoFrame(img.width, img.height, format="rgba")