Skip to content

Instantly share code, notes, and snippets.

@wch
Created June 6, 2023 17:36
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save wch/892c1884ac9f666388e285aec14066c7 to your computer and use it in GitHub Desktop.
Save wch/892c1884ac9f666388e285aec14066c7 to your computer and use it in GitHub Desktop.
Video compression script
#!/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
@wch
Copy link
Author

wch commented Jun 6, 2023

Compression results:

2281519 May 28 18:49 screencap.mov              <-- Original file
 280541 Jun  6 12:35 screencap-compressed.mp4   <-- No size or speed change
 132959 Jun  6 12:35 screencap-half.mp4         <-- Half size
 145569 Jun  6 12:35 screencap-2x.mp4           <-- Double speed
  66790 Jun  6 12:35 screencap-2x-half.mp4      <-- Half size and double speed

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment