From 2a2516bff91e3be63f9ef0f59751fc0dda20f6c6 Mon Sep 17 00:00:00 2001 From: j Date: Tue, 3 Dec 2024 19:35:37 +0000 Subject: [PATCH] pad audio tracks to scene duration --- render.py | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/render.py b/render.py index 4cb2c4a..97d412a 100644 --- a/render.py +++ b/render.py @@ -66,6 +66,7 @@ def write_if_new(path, data, mode=''): def compose(clips, target=150, base=1024, voice_over=None): + fps = 24 length = 0 scene = { 'front': { @@ -100,6 +101,7 @@ def compose(clips, target=150, base=1024, voice_over=None): used = [] voice_overs = [] + sub_offset = 0 if voice_over: vo_keys = list(sorted(voice_over)) if chance(seq, 0.5): @@ -118,7 +120,7 @@ def compose(clips, target=150, base=1024, voice_over=None): if vo_min > target: target = vo_min elif vo_min < target: - offset = (target - vo_min) / 2 + offset = int(((target - vo_min) / 2) * fps) / fps scene['audio-center']['A1'].append({ 'blank': True, 'duration': offset @@ -298,6 +300,16 @@ def compose(clips, target=150, base=1024, voice_over=None): }) used.append(clip) print("scene duration %0.3f (target: %0.3f, vo_min: %0.3f)" % (length, target, vo_min)) + if sub_offset < length: + delta = length - sub_offset + scene['audio-center']['A1'].append({ + 'blank': True, + 'duration': delta + }) + scene['audio-rear']['A1'].append({ + 'blank': True, + 'duration': delta + }) return scene, used def get_scene_duration(scene): @@ -321,7 +333,7 @@ def get_offset_duration(prefix): def render(root, scene, prefix=''): fps = 24 files = [] - scene_duration = int(get_scene_duration(scene) * 24) + scene_duration = int(get_scene_duration(scene) * fps) for timeline, data in scene.items(): if timeline == "subtitles": path = os.path.join(root, prefix + "front.srt") @@ -338,14 +350,14 @@ def render(root, scene, prefix=''): #print(track) for clip in clips: project.append_clip(track, clip) - track_durations[track] = int(sum([c['duration'] for c in clips]) * 24) + track_durations[track] = int(sum([c['duration'] for c in clips]) * fps) if timeline.startswith('audio-'): track_duration = project.get_duration() delta = scene_duration - track_duration if delta > 0: for track in track_durations: if track_durations[track] == track_duration: - project.append_clip(track, {'blank': True, "duration": delta/24}) + project.append_clip(track, {'blank': True, "duration": delta/fps}) break path = os.path.join(root, prefix + "%s.kdenlive" % timeline) project_xml = project.to_xml()