Created
November 16, 2023 13:17
-
-
Save clemlesne/6b5d3c36ed0d9c57ade20bbf3a3e5ab8 to your computer and use it in GitHub Desktop.
Easiliy optimize videos with FFMPEG from a folder
This file contains 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
# 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