Skip to content

Instantly share code, notes, and snippets.

@aaronpk
Created December 27, 2018 00:38
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 aaronpk/d6d8a5d02d61aaa878ea1ba15ba6886e to your computer and use it in GitHub Desktop.
Save aaronpk/d6d8a5d02d61aaa878ea1ba15ba6886e to your computer and use it in GitHub Desktop.
Make previews for a folder of .mov or .mp4 files that MacOS can open. Normalizes to 480x270 at 30fps and 96kbps audio.
#!/bin/bash
# Make previews for a folder of .mov or .mp4 files that MacOS can open.
# Normalizes to 480x270 at 30fps and 96kbps audio.
current_file=none
# Catch ctrl-c and stop the whole batch, otherwise ctrl-c just stops ffmpeg and the loop continues
trap ctrl_c INT
function ctrl_c() {
echo "** aborting $current_file"
if [ -e "$current_file.preview.mp4" ]
then
# Delete the current file since ffmpeg will have left it there after being interrupted
rm "$current_file.preview.mp4"
fi
exit
}
for filename in `ls *.{MOV,MP4,mov,mp4}`
do
# Check if the preview file already exists
if [ -e "$filename.preview.mp4" ]
then
echo "$filename.preview.mp4 already exists"
else
# Don't convert .preview.mp4 files
if [[ $filename != *.preview.mp4 ]]
then
current_file=$filename
# ffmpeg flags
# -c:v libx264 -- encode using h264
# -b:a 96k -- set audio to 96kbps
# -s 480x270 -- scale video to 480x270 (1/4 of 1920x1080)
# -r 30 -- set frame rate to 30fps
# -movflags faststart -- move the index to the beginning of the file
# -pix_fmt yuv420p -- this is needed for the resulting video to be understood by some apps like Quicktime and Quick Look
ffmpeg -i $filename -c:v libx264 -b:a 96k -s 480x270 -r 30 -movflags faststart -pix_fmt yuv420p $filename.preview.mp4
fi
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment