Skip to content

Instantly share code, notes, and snippets.

@alecjacobson
Forked from yig/HandbrakeCLI-recursive
Last active June 28, 2017 13:17
Show Gist options
  • Save alecjacobson/60fb2e027e0d4aef60c6 to your computer and use it in GitHub Desktop.
Save alecjacobson/60fb2e027e0d4aef60c6 to your computer and use it in GitHub Desktop.
Give HandbrakeCLI a path to a file or a directory and it will generate better-compressed "{.}-handbrake.mp4" files next to each video.
#!/bin/bash
# brew install handbrake parallel ffmpeg
usage()
{
echo 1>&2 "Usage:" "$0" 'path/to/video/dir1 [path/to/video/dir2 ...]'
exit -1
}
if [ $# -lt 1 ]
then
usage
fi
find "$@" \( -iname '*.mov' -or -iname '*.mpg' -or -iname '*.mpeg' -or \
-iname '*.mp4' -or -iname '*.webm' -or -iname '*.avi' -or -iname '*.wmv' \
-or -iname '*.mkv' -or -iname '*.m4v' \) -print0 | \
parallel -0 --tty \
"ffmpeg -i '{}' -vcodec copy -acodec copy -metadata:s:v:0 rotate=0 \
'{.}-unrotated.mov' && \
HandBrakeCLI -i '{.}-unrotated.mov' -o '{.}-unrotated-handbrake.mp4' -O \
--preset 'Normal' --crop 0:0:0:0 && \
ffmpeg -i '{.}-unrotated-handbrake.mp4' -vcodec copy -acodec copy \
-metadata:s:v:0 rotate=\`mediainfo --Inform=\"Video;%Rotation%\" '{}'\` \
'{.}-handbrake.mp4' && \
rm '{.}-unrotated-handbrake.mp4' '{.}-unrotated.mov' "
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment