Skip to content

Commit

Permalink
Now doing cleanup if interrupted by ctrl+C, and doing cleanup on --dr…
Browse files Browse the repository at this point in the history
…y_run
  • Loading branch information
chameleon-ai committed Jan 2, 2025
1 parent facefbe commit 09b95d6
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 10 deletions.
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,8 @@ Automatically burn-in the first available subtitles, if any exist, with `--auto_
Print available built-in subtitles with `--list_subs`\
Print available audio tracks with `--list_audio`\
Specify arbitrary filters for ffmpeg with `-a`/`--audio_filter` and `-v`/`--video_filter`\
For fun, try `--first_second_every_minute`, inspired by the youtube channel FirstSecondEveryMinute (Warning: This can take a while)
For fun, try `--first_second_every_minute`, inspired by the youtube channel FirstSecondEveryMinute (Warning: This can take a while)\
Keep generated temp files with `-k`/`--keep_temp_files`

Type `--help` for a complete list of commands.

Expand All @@ -142,7 +143,7 @@ Type `--help` for a complete list of commands.
- Row based multithreading is enabled by default. This can be disabled with `--no_mt`
- You may notice an additional file 'temp.opus'. This is an intermediate audio file used for size calculation purposes. If normalization is enabled, 'temp.normalized.opus' will also be generated.
- With `--mp4`/`--codec libx264`, 'temp.aac' and 'temp.normalized.aac' are generated instead of .opus files.
- If any temp files already exist (such as when using `-k` or killing the script prematurely), a new one will be made with an incrementing number (temp.1.opus, temp.2.opus, etc.)
- If any temp files already exist (such as when using `-k`), a new one will be made with an incrementing number (temp.1.opus, temp.2.opus, etc.)
- Expect size overshoots much more often with `--mp4`/`--codec libx264`. This is a result of libx264's rate control accuracy being much more sloppy than libvpx-vp9.
- When using `-x`/`--cut` or `-c`/`--concat`, a lossless temporary file of the assembled segments called 'temp.mkv' gets generated.
- When using `-x`/`--cut` or `-c`/`--concat` it is currently not possible to burn-in subtitles or to specify an audio track besides the default.
Expand All @@ -153,7 +154,7 @@ Type `--help` for a complete list of commands.
- You may notice that rendering is significantly slower when burning in subtitles. I tried many different settings and ffmpeg is very fragile, this is the only method I could figure out that works consistently.

## Tips, Tricks, and References
- If you're unsure about your `-s`/`--start` and `-e`/`--end` timestamps, try a `--dry_run` and inspect temp.opus to see if the audio is the right slice that you want.
- If you're unsure about your `-s`/`--start` and `-e`/`--end` timestamps, try a `--dry_run -k` and inspect temp.opus to see if the audio is the right slice that you want.
- For long videos (over about 3 minutes), it's usually beneficial to add `-v spp`
- https://ffmpeg.org/ffmpeg-filters.html#spp-1
- Filter graph building for `-c`/`--concat` and `-x`/`--cut` were made possible through this valuable reference:
Expand Down
22 changes: 15 additions & 7 deletions webm_for_4chan.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@
import mimetypes
import os
import platform
import signal
import subprocess
import sys
import time
import traceback

Expand Down Expand Up @@ -1248,13 +1250,19 @@ def image_audio_combine(input_image, input_audio, args):
print('WARNING: Output size exceeded target maximum {}. You should rerun with --bitrate_compensation to reduce output size.'.format(int(size_limit/1024)))
return output

do_cleanup = True
def cleanup():
for filename in files_to_clean:
if os.path.isfile(filename):
os.remove(filename)
if do_cleanup:
for filename in files_to_clean:
if os.path.isfile(filename):
os.remove(filename)

def signal_handler(sig, frame):
cleanup()

if __name__ == '__main__':
try:
signal.signal(signal.SIGINT, signal_handler)
parser = argparse.ArgumentParser(
prog='4chan Webm Converter',
description='Attempts to fit video clips into the 4chan size limit',
Expand Down Expand Up @@ -1305,6 +1313,8 @@ def cleanup():
args, unknown_args = parser.parse_known_args()
if help in args:
parser.print_help()
if args.keep_temp_files:
do_cleanup = False
if args.mp4: # Use this shortcut flag to override the --codec option
args.codec = 'libx264'
input_filename = None
Expand All @@ -1322,8 +1332,7 @@ def cleanup():
raise RuntimeError("Couldn't identify audio source from input files.")
result = image_audio_combine(image_input, audio_input, args)
print('output file: "{}"'.format(result))
if not args.dry_run and not args.keep_temp_files:
cleanup()
cleanup()
exit(0)
elif len(unknown_args) > 2:
print("Too many input arguments. Please specify only 1 or 2 inputs.")
Expand Down Expand Up @@ -1369,8 +1378,7 @@ def cleanup():
print('duration:', duration)
result = process_video(input_filename, start_time, duration, args, full_video)
print('output file: "{}"'.format(result))
if not args.dry_run and not args.keep_temp_files:
cleanup()
cleanup()
else:
print('Input file not found: "' + input_filename + '"')
except argparse.ArgumentError as e:
Expand Down

0 comments on commit 09b95d6

Please sign in to comment.