Skip to content

Commit 0e9adfc

Browse files
committed
ffmpeg_editlist: Cache intermediate encoding file before updating subs/etc for .mkv file
1 parent 86b7c28 commit 0e9adfc

File tree

1 file changed

+36
-25
lines changed

1 file changed

+36
-25
lines changed

ffmpeg_editlist.py

Lines changed: 36 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,7 @@ def main(argv=sys.argv[1:]):
246246
parser.add_argument('--limit', '-l', action='append',
247247
help='Limit to only outputs matching this pattern. There is no wildcarding. This option can be given multiple times.')
248248
parser.add_argument('--check', '-c', action='store_true',
249-
help="Don't encode or generate output files, just check consistency of the YAML file. This *will* override the .info.txt output file.")
249+
help="Don't encode or generate output files, just check consistency of the YAML file. This *will* override the .info.txt output file. If the temporary encoded intermediate file exists, DO update the final .mkv file with subtitles/descriptions/etc.")
250250
parser.add_argument('--force', '-f', action='store_true',
251251
help='Overwrite existing output files without prompting')
252252
parser.add_argument('--dry-run', action='store_true',
@@ -544,6 +544,8 @@ def main(argv=sys.argv[1:]):
544544
options_ffmpeg_segment = [ ]
545545
segment_type = 'video'
546546

547+
output_raw = args.output / segment['output']
548+
output_raw = output_raw.parent / 'tmp' / output_raw.name
547549
output = args.output / segment['output']
548550

549551
# TODO: should continue further down to actually test other code.
@@ -580,15 +582,14 @@ def main(argv=sys.argv[1:]):
580582
if not args.check:
581583
subprocess.check_call(cmd)
582584

583-
# Embed subtitles in mkv if they are there
584-
if args.srt and args.mkv_props:
585-
cmd = ['mkvmerge', tmpdir_out, srt_output,
586-
'-o', tmpdir_out+'.2.mkv',
587-
]
588-
print(cmd)
589-
if not args.check:
590-
subprocess.check_call(cmd)
591-
shutil.move(tmpdir_out+'.2.mkv', tmpdir_out)
585+
# This is raw encoding without subtitles or anithing
586+
# We need another copy, since ffmpeg detects output based on
587+
# filename. Yet for atomicness, we need a temporary filename for
588+
# the temp part
589+
if not args.check:
590+
ensure_filedir_exists(output_raw)
591+
with atomic_write(output_raw) as tmp_output:
592+
shutil.move(tmpdir_out, tmp_output)
592593

593594

594595
# Create the video properties/chapters/etc (needs to be done before
@@ -635,26 +636,36 @@ def main(argv=sys.argv[1:]):
635636
with atomic_write(video_description_file, 'w') as toc_file:
636637
open(toc_file, 'w').write('\n\n'.join(video_description))
637638

639+
# Finalize the video
640+
641+
# Embed subtitles in mkv if they are there
642+
if args.srt and args.mkv_props:
643+
cmd_merge = ['mkvmerge', output_raw, srt_output,
644+
'-o', output,
645+
]
646+
print(cmd_merge)
647+
if (not args.check) or output_raw.exists():
648+
subprocess.check_call(cmd_merge)
649+
#shutil.move(tmpdir_out, output)
650+
else:
651+
if not args.check:
652+
shutil.copy(output_raw, output)
653+
654+
638655
# mkv chapters
639656
if title or toc or video_description:
640-
cmd = ['mkvpropedit', tmpdir_out,
641-
*(['--set', f'title={title}',] if title else []),
642-
*(['--chapters', str(chapter_file),] if toc else []),
643-
*(['--attachment-name', 'description', '--add-attachment', video_description_file] if video_description else []),
644-
]
645-
print(cmd)
646-
if not args.check and args.mkv_props:
647-
subprocess.check_call(cmd)
657+
cmd_propedit = [
658+
'mkvpropedit', output,
659+
*(['--set', f'title={title}',] if title else []),
660+
*(['--chapters', str(chapter_file),] if toc else []),
661+
*(['--attachment-name', 'description', '--add-attachment', video_description_file] if video_description else []),
662+
]
663+
print(cmd_propedit)
664+
if (not args.check) or output_raw.exists():
665+
subprocess.check_call(cmd_propedit)
648666

649667

650-
# Finalize the video
651668

652-
# We need another copy, since ffmpeg detects output based on
653-
# filename. Yet for atomicness, we need a temporary filename for
654-
# the temp part
655-
if not args.check:
656-
with atomic_write(output) as tmp_output:
657-
shutil.move(tmpdir_out, tmp_output)
658669

659670
# Print out covered segments (for verification purposes)
660671
for seg_n, time in covers:

0 commit comments

Comments
 (0)