From 5208ad5f1e7037cdb6ee5cc278c145365796e30c Mon Sep 17 00:00:00 2001 From: Daniel Tsvetkov Date: Sun, 6 Sep 2020 14:07:00 +0200 Subject: [PATCH] into tmp dir --- .gitignore | 1 + main.py | 38 +++++++++++++++++++++++--------------- 2 files changed, 24 insertions(+), 15 deletions(-) diff --git a/.gitignore b/.gitignore index a7349bd..29d11cd 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,7 @@ *.mp4 *.vtt *.txt +tmp/ logs/ .idea venv \ No newline at end of file diff --git a/main.py b/main.py index 8ca43d8..18dd872 100644 --- a/main.py +++ b/main.py @@ -11,9 +11,17 @@ WORD_BREAK = 60 MAX_ITER = 0 SHOULD_REMOVE_TEMPS = False -SHOULD_CUT_VIDEOS = False +SHOULD_CUT_VIDEOS = True SHOULD_COPY_CODEC = False +# FINAL_TITLE = "plastic" +# DE_VIDEO = "https://www.youtube.com/watch?v=mhmpeIyG0uM" +# EN_VIDEO = "https://www.youtube.com/watch?v=RS7IzU2VJIQ" +# +# FINAL_TITLE = "atomic_bombs" +# DE_VIDEO = "https://www.youtube.com/watch?v=NBeeRVkTeJg" +# EN_VIDEO = "https://www.youtube.com/watch?v=JyECrGp-Sw8" + FINAL_TITLE = "fusion_power" DE_VIDEO = "https://www.youtube.com/watch?v=lj4IC70kDIU" EN_VIDEO = "https://www.youtube.com/watch?v=mZsaaturR6E" @@ -50,37 +58,37 @@ def cut_videos(idx, en_start, en_end, de_start, de_end, en_txt, de_txt): print("cut_{:02d}: en: {:6.2f} -> {:6.2f}, de: {:6.2f} -> {:6.2f}".format( idx, en_start, en_end, de_start, de_end, )) - with open('concat_{}.txt'.format(idx), 'w+') as f: + with open('tmp/concat_{}.txt'.format(idx), 'w+') as f: f.write('file de_{}.mp4\n'.format(idx)) f.write('file en_{}.mp4\n'.format(idx)) f.write('file de_{}.mp4\n'.format(idx)) - run_os_cmd("ffmpeg -y -i en.mp4 -ss {en_start} -to {en_end} en_{idx}.mp4 2>logs/01_ffmpeg_cut_en_{idx}.log".format( + run_os_cmd("ffmpeg -y -i en.mp4 -ss {en_start} -to {en_end} tmp/en_{idx}.mp4 2>logs/01_ffmpeg_cut_en_{idx}.log".format( en_start=en_start, en_end=en_end, idx=idx)) - run_os_cmd("ffmpeg -y -i de.mp4 -ss {de_start} -to {de_end} de_{idx}.mp4 2>logs/01_ffmpeg_cut_en_{idx}.log".format( + run_os_cmd("ffmpeg -y -i de.mp4 -ss {de_start} -to {de_end} tmp/de_{idx}.mp4 2>logs/01_ffmpeg_cut_en_{idx}.log".format( de_start=de_start, de_end=de_end, idx=idx)) if SHOULD_COPY_CODEC: run_os_cmd( - """ffmpeg -y -f concat -safe 0 -i concat_{idx}.txt -c copy deende_{idx}.mp4 2>logs/02_ffmpeg_concat_{idx}_copy.log""".format( + """ffmpeg -y -f concat -safe 0 -i tmp/concat_{idx}.txt -c copy tmp/deende_{idx}.mp4 2>logs/02_ffmpeg_concat_{idx}_copy.log""".format( idx=idx)) else: run_os_cmd( - """ffmpeg -y -f concat -safe 0 -i concat_{idx}.txt deende_{idx}.mp4 2>logs/02_ffmpeg_concat_{idx}_nocopy.log""".format( + """ffmpeg -y -f concat -safe 0 -i tmp/concat_{idx}.txt tmp/deende_{idx}.mp4 2>logs/02_ffmpeg_concat_{idx}_nocopy.log""".format( idx=idx)) en_txt_spl = '\n'.join(textwrap.wrap(en_txt, WORD_BREAK, break_long_words=False)) de_txt_spl = '\n'.join(textwrap.wrap(de_txt, WORD_BREAK, break_long_words=False)) text = "{}\n\n\n{}".format(de_txt_spl, en_txt_spl) - with open('text_{}.txt'.format(idx), 'w+') as f: + with open('tmp/text_{}.txt'.format(idx), 'w+') as f: f.write(text) run_os_cmd( - """ffmpeg -y -i deende_{idx}.mp4 -vf drawtext="textfile=text_{idx}.txt: fontcolor=white: fontsize={fontsize}: box=1: boxcolor=black@0.5: boxborderw=5: x=(w-text_w)/2: y=(h-text_h)/2" -c:a copy deende_{idx}_s.mp4 2>logs/03_ffmpeg_sub_{idx}.log""".format( + """ffmpeg -y -i tmp/deende_{idx}.mp4 -vf drawtext="textfile=tmp/text_{idx}.txt: fontcolor=white: fontsize={fontsize}: box=1: boxcolor=black@0.5: boxborderw=5: x=(w-text_w)/2: y=(h-text_h)/2" -c:a copy tmp/deende_{idx}_s.mp4 2>logs/03_ffmpeg_sub_{idx}.log""".format( idx=idx, text=text, fontsize=FONT_SIZE)) - if SHOULD_REMOVE_TEMPS: - run_os_cmd("rm en_{iter}.mp4 de_{iter}.mp4 concat_{iter}.txt text_{iter}.txt".format(iter=idx)) def download_vids_and_subs(): if SHOULD_REMOVE_TEMPS: run_os_cmd("rm en.mp4 de.mp4 subs.en.vtt subs.de.vtt") + run_os_cmd("mkdir -p logs") + run_os_cmd("mkdir -p tmp") run_os_cmd("""youtube-dl -f worst --output="de.%(ext)s" {}""".format(DE_VIDEO)) run_os_cmd("""youtube-dl -f worst --output="en.%(ext)s" {}""".format(EN_VIDEO)) run_os_cmd( @@ -131,26 +139,26 @@ def time_subs(): def concat_all_videos(max_iters): - with open('concat.txt', 'w+') as f: + with open('tmp/concat.txt', 'w+') as f: for idx in range(max_iters): f.write('file deende_{}_s.mp4\n'.format(idx)) if SHOULD_COPY_CODEC: run_os_cmd( - """ffmpeg -y -f concat -safe 0 -i concat.txt -c copy {final_title}.mp4 2>logs/04_ffmpeg_all_copy.log""".format( + """ffmpeg -y -f concat -safe 0 -i tmp/concat.txt -c copy {final_title}.mp4 2>logs/04_ffmpeg_all_copy.log""".format( final_title=FINAL_TITLE)) else: run_os_cmd( - """ffmpeg -y -f concat -safe 0 -i concat.txt {final_title}.mp4 2>logs/04_ffmpeg_all_nocopy.log""".format( + """ffmpeg -y -f concat -safe 0 -i tmp/concat.txt {final_title}.mp4 2>logs/04_ffmpeg_all_nocopy.log""".format( final_title=FINAL_TITLE)) if SHOULD_REMOVE_TEMPS: - run_os_cmd("rm concat.txt") + run_os_cmd("rm -r tmp/") for idx in range(MAX_ITER): run_os_cmd("rm deende_{idx}.mp4".format(idx=idx)) run_os_cmd("rm deende_{idx}_s.mp4".format(idx=idx)) def main(): - download_vids_and_subs() + # download_vids_and_subs() max_iters = time_subs() concat_all_videos(max_iters)