Last active
April 11, 2025 05:14
-
-
Save smmr0/a9dc134273e2fbf9f673305be7942cb5 to your computer and use it in GitHub Desktop.
ffmpeg subtitles
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env sh | |
| input="$1" | |
| output="$2" | |
| subtitle_format="$3" | |
| subcommand="$4" | |
| start="$5" | |
| end="$6" | |
| extension() { | |
| file="$1"; shift | |
| echo "$file" | grep -o '\.[[:alnum:]_.]*$' | |
| } | |
| temp_dir="$(mktemp -d)" # Note: `mktemp` is not POSIX | |
| temp_input="$temp_dir/input$(extension "$input")" | |
| temp_output="$temp_dir/output$(extension "$output")" | |
| ln -s "$input" "$temp_input" | |
| # Reset positional args | |
| set -- | |
| set -- "$@" \ | |
| -i file:"$temp_input" | |
| # Set input opts | |
| if [ -n "$start" ]; then | |
| set -- "$@" \ | |
| -ss "$start" | |
| fi | |
| if [ -n "$end" ]; then | |
| set -- "$@" \ | |
| -to "$end" | |
| fi | |
| # Set overlay opts | |
| case "$subtitle_format" in | |
| text) | |
| set -- "$@" \ | |
| -vf subtitles="$temp_input:force_style='Fontname=Circe Rounded,Outline=2,Shadow=0,PrimaryColour=&H00FFFFFF,SecondaryColour=&H00FFFFFF,OutlineColour=&H00000000'" | |
| case "$subcommand" in | |
| screencap) | |
| ;; | |
| *) | |
| set -- "$@" \ | |
| -map 0 \ | |
| -map '-s:0' | |
| ;; | |
| esac | |
| ;; | |
| bitmap) | |
| set -- "$@" \ | |
| -filter_complex '[v:0][s:0]overlay=shortest=1[vs_0]' | |
| case "$subcommand" in | |
| screencap) | |
| set -- "$@" \ | |
| -map '[vs_0]' | |
| ;; | |
| *) | |
| set -- "$@" \ | |
| -map 0 \ | |
| -map '-v:0' \ | |
| -map '-s:0' \ | |
| -map '[vs_0]' | |
| ;; | |
| esac | |
| ;; | |
| esac | |
| set -- "$@" \ | |
| -map_metadata -1 | |
| # Set output opts | |
| case "$subcommand" in | |
| overlay) | |
| set -- "$@" \ | |
| -c copy \ | |
| -c:v h264 \ | |
| -crf 0 \ | |
| file:"$temp_output" | |
| ;; | |
| screencap) | |
| set -- "$@" \ | |
| -frames:v 1 \ | |
| -q:v 1 \ | |
| file:"$temp_output" | |
| ;; | |
| esac | |
| ffmpeg "$@" | |
| exit_code="$?" | |
| mv "$temp_output" "$output" | |
| rm -r "$temp_dir" | |
| exit "$exit_code" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment