Created
June 6, 2023 17:36
-
-
Save wch/892c1884ac9f666388e285aec14066c7 to your computer and use it in GitHub Desktop.
Video compression script
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
#!/bin/bash | |
# Process command-line arguments | |
if [[ $# -eq 0 ]] || [[ "$1" == "--help" ]]; then | |
echo "Usage: compress-video [OPTIONS] [FILENAMES...]" | |
echo | |
echo "Options:" | |
echo " --speed-2x Output video at 2x speed" | |
echo " --size-half Scale output to half size" | |
echo " --help Display this help page" | |
exit 0 | |
fi | |
echo "1" | |
flag_speed2x=false | |
flag_half=false | |
# Array to hold file arguments | |
files=() | |
# Process command-line arguments | |
while [[ $# -gt 0 ]]; do | |
echo "2" | |
case "$1" in | |
--speed-2x) | |
flag_speed2x=true | |
shift | |
;; | |
--size-half) | |
flag_half=true | |
shift | |
;; | |
*) | |
# Add non-flag arguments to files array | |
files+=("$1") | |
shift | |
;; | |
esac | |
done | |
echo $flag_speed2x | |
echo $flag_half | |
for f in "${files[@]}" | |
do | |
echo "$f" | |
if $flag_speed2x && $flag_half; then | |
# The scaling to ceil(n/4)*2 is to ensure that the dimensions are | |
# divisible by 2, which is required by the codec. | |
ffmpeg -i "$f" -vf "scale=ceil(iw/4)*2:ceil(ih/4)*2,setpts=0.5*PTS" -af "atempo=2.0" "${f%.*}-2x-half.mp4" | |
elif $flag_speed2x; then | |
ffmpeg -i "$f" -vf "setpts=0.5*PTS" -af "atempo=2.0" "${f%.*}-2x.mp4" | |
elif $flag_half; then | |
ffmpeg -i "$f" -vf "scale=ceil(iw/4)*2:ceil(ih/4)*2" "${f%.*}-half.mp4" | |
else | |
ffmpeg -i "$f" "${f%.*}-compressed.mp4" | |
fi | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Compression results: