Skip to content

Instantly share code, notes, and snippets.

@clemlesne
Created November 16, 2023 13:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save clemlesne/6b5d3c36ed0d9c57ade20bbf3a3e5ab8 to your computer and use it in GitHub Desktop.
Save clemlesne/6b5d3c36ed0d9c57ade20bbf3a3e5ab8 to your computer and use it in GitHub Desktop.
Easiliy optimize videos with FFMPEG from a folder
# Iterate over MP4, MOV and M4V files in the directory
find "." -type f \( -iname "*.mp4" -o -iname "*.MP4" -o -iname "*.mov" -o -iname "*.MOV" -o -iname "*.m4v" -o -iname "*.M4V" \) -exec sh -c '
for file do
echo "Optimizing: $file"
# Extract the file extension
EXT="${file##*.}"
# Generate a random file name for the temporary file, preserving the extension
TMP_FILE="/tmp/optimized.$RANDOM.$EXT"
# Use ffmpeg to optimize the video
ffmpeg -i "$file" \
-c:a copy \
-c:v hevc_videotoolbox \
-map_metadata 0 \
-movflags use_metadata_tags \
-preset slow \
-q:v 50 \
-threads 0 \
-vf "scale=''min(1920\,iw)'':-1" \
"$TMP_FILE" < /dev/null
# Preserve the original timestamps
touch -r "$file" "$TMP_FILE"
# Replace the original file with the optimized video
mv "$TMP_FILE" "$file"
echo "Optimized: $file"
done
' sh {} +
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment