Skip to content

Instantly share code, notes, and snippets.

@dmacsuibhne
Last active November 27, 2024 19:48
Show Gist options
  • Save dmacsuibhne/d5a99788e4330b0a2aa83b9d57ce8681 to your computer and use it in GitHub Desktop.
Save dmacsuibhne/d5a99788e4330b0a2aa83b9d57ce8681 to your computer and use it in GitHub Desktop.
Compress videos with ffmpeg
#!/bin/bash
encoder=libx265
crf_value=35
preset_value="veryslow" # Possible presets: ultrafast superfast veryfast faster fast medium slow slower veryslow placebo
directory_original=./keep
directory_new="$directory_original/conv$crf_value$preset_value"
mkdir -p "$directory_new"
for filepath in ${directory_original}/*; do
if [ ! -d "$filepath" ]; then #exclude directories
filename_with_extension="${filepath##*/}"
filename_no_extension="${filename_with_extension%.*}"
new_path="$directory_new/${filename_no_extension}_${encoder}_crf${crf_value}_${preset_value}.mp4"
new_date=$(date -r "$filepath" +"%Y%m%d%H%M.%S")
( # Subshell so set -x only prints inside of loop
set -x
ffmpeg -i "$filepath" -c:v ${encoder} -crf ${crf_value} -preset ${preset_value} -vtag hvc1 "$new_path"
TZ=UTC touch -a -m -t "$new_date" "$new_path"
)
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment