From bb22ffbaaee36e6840adcb9f926bcd0cb405fe2a Mon Sep 17 00:00:00 2001 From: j Date: Sat, 10 May 2025 10:04:25 +0100 Subject: [PATCH] add stereo downmix --- management/commands/infinity.py | 1 + management/commands/render.py | 1 + render.py | 39 ++++++++++++++++++++++++--------- 3 files changed, 31 insertions(+), 10 deletions(-) diff --git a/management/commands/infinity.py b/management/commands/infinity.py index e642092..73e91dc 100644 --- a/management/commands/infinity.py +++ b/management/commands/infinity.py @@ -16,6 +16,7 @@ class Command(BaseCommand): parser.add_argument('--duration', action='store', dest='duration', default="3600", help='target duration of all fragments in seconds') parser.add_argument('--single-file', action='store_true', dest='single_file', default=False, help='render to single video') parser.add_argument('--keep-audio', action='store_true', dest='keep_audio', default=False, help='keep independent audio tracks') + parser.add_argument('--stereo-downmix', action='store_true', dest='stereo_downmix', default=False, help='stereo downmix') parser.add_argument('--debug', action='store_true', dest='debug', default=False, help='output more info') def handle(self, **options): diff --git a/management/commands/render.py b/management/commands/render.py index 54e66c2..91e8fa5 100644 --- a/management/commands/render.py +++ b/management/commands/render.py @@ -18,6 +18,7 @@ class Command(BaseCommand): parser.add_argument('--no-video', action='store_true', dest='no_video', default=False, help='don\'t render video') parser.add_argument('--single-file', action='store_true', dest='single_file', default=False, help='render to single video') parser.add_argument('--keep-audio', action='store_true', dest='keep_audio', default=False, help='keep independent audio tracks') + parser.add_argument('--stereo-downmix', action='store_true', dest='stereo_downmix', default=False, help='stereo downmix') parser.add_argument('--debug', action='store_true', dest='debug', default=False, help='output more info') def handle(self, **options): diff --git a/render.py b/render.py index 547b4b0..b70fee8 100644 --- a/render.py +++ b/render.py @@ -575,19 +575,37 @@ def render_all(options): "-filter_complex", "[0:a][1:a][2:a][3:a][4:a][5:a]amerge=inputs=6[a]", "-map", "[a]", "-c:a", "aac", fragment_prefix / "audio-5.1.mp4" ]) + audio_front = "audio-5.1.mp4" + audio_back = "audio-back.wav" + copy = '-c' + if options["stereo_downmix"]: + cmds.append([ + "ffmpeg", "-y", + "-nostats", "-loglevel", "error", + "-i", fragment_prefix / "audio-front.wav", + "-i", fragment_prefix / "audio-center.wav", + "-i", fragment_prefix / "audio-rear.wav", + "-i", fragment_prefix / audio_back, + "-filter_complex", "[0:a][1:a][2:a][3:a]amerge=inputs=2[a]", + "-map", "[a]", '-ac', '2', fragment_prefix / "audio-stereo.wav" + ]) + audio_front = "audio-stereo.wav" + audio_back = "audio-stereo.wav" + copy = '-c:v' + cmds.append([ "ffmpeg", "-y", "-nostats", "-loglevel", "error", "-i", fragment_prefix / "front.mp4", - "-i", fragment_prefix / "audio-5.1.mp4", - "-c", "copy", - fragment_prefix / "front-5.1.mp4", + "-i", fragment_prefix / audio_front, + copy, "copy", + fragment_prefix / "front-mixed.mp4", ]) cmds.append([ "ffmpeg", "-y", "-nostats", "-loglevel", "error", "-i", fragment_prefix / "back.mp4", - "-i", fragment_prefix / "audio-back.wav", + "-i", fragment_prefix / audio_back, "-c:v", "copy", fragment_prefix / "back-audio.mp4", ]) @@ -598,7 +616,7 @@ def render_all(options): for a, b in ( ("back-audio.mp4", "back.mp4"), - ("front-5.1.mp4", "front.mp4"), + ("front-mixed.mp4", "front.mp4"), ): duration_a = ox.avinfo(str(fragment_prefix / a))['duration'] duration_b = ox.avinfo(str(fragment_prefix / b))['duration'] @@ -607,7 +625,7 @@ def render_all(options): print('!!', duration_b, fragment_prefix / b) sys.exit(-1) shutil.move(fragment_prefix / "back-audio.mp4", fragment_prefix / "back.mp4") - shutil.move(fragment_prefix / "front-5.1.mp4", fragment_prefix / "front.mp4") + shutil.move(fragment_prefix / "front-mixed.mp4", fragment_prefix / "front.mp4") if options["keep_audio"]: shutil.move(fragment_prefix / "audio-center.wav", fragment_prefix / "vocals.wav") shutil.move(fragment_prefix / "audio-front.wav", fragment_prefix / "foley.wav") @@ -617,6 +635,7 @@ def render_all(options): "audio-center.wav", "audio-rear.wav", "audio-front.wav", "audio-back.wav", "back-audio.mp4", "fl.wav", "fr.wav", "fc.wav", "lfe.wav", "bl.wav", "br.wav", + "audio-stereo.wav", ): fn = fragment_prefix / fn if os.path.exists(fn): @@ -680,7 +699,7 @@ def render_all(options): "-i", base_prefix / "front.mp4", "-i", base_prefix / "audio-5.1.mp4", "-c", "copy", - base_prefix / "front-5.1.mp4", + base_prefix / "front-mixed.mp4", ]) cmds.append([ "ffmpeg", "-y", @@ -697,7 +716,7 @@ def render_all(options): for a, b in ( ("back-audio.mp4", "back.mp4"), - ("front-5.1.mp4", "back.mp4"), + ("front-mixed.mp4", "back.mp4"), ): duration_a = ox.avinfo(str(base_prefix / a))['duration'] duration_b = ox.avinfo(str(base_prefix / b))['duration'] @@ -706,7 +725,7 @@ def render_all(options): print('!!', duration_b, base_prefix / b) sys.exit(-1) shutil.move(base_prefix / "back-audio.mp4", base_prefix / "back.mp4") - shutil.move(base_prefix / "front-5.1.mp4", base_prefix / "front.mp4") + shutil.move(base_prefix / "front-mixed.mp4", base_prefix / "front.mp4") if options["keep_audio"]: shutil.move(base_prefix / "audio-center.wav", base_prefix / "vocals.wav") shutil.move(base_prefix / "audio-front.wav", base_prefix / "foley.wav") @@ -848,7 +867,7 @@ def render_infinity(options): "max-items": 30, "no_video": False, } - for key in ("prefix", "duration", "debug", "single_file", "keep_audio"): + for key in ("prefix", "duration", "debug", "single_file", "keep_audio", "stereo_downmix"): state[key] = options[key] while True: