Converts all videos in nested folders to h264 and audio to aac using HandBrakeCLI with the 'Fast 1080p30' preset. This saves Plex from having to transcode files which is CPU intensive and not possible on certain underpowered NaSs (I'm looking at you Seagate PersonalCloud). This script is only tested on Max OSX High Sierra, although it should work on Linux.
Shameless plugs for search error messages:
This library is for batch transcoding an existing bunch of movies with minimal effort. For the fully featured gold plated whizz bang all singing and all dancing video transcoding pipeline check out the awesome sickbeard_mp4_automator.
Requires HandBrakeCLI (for transcoding) and media-info (for interrogation). Install with Homebrew.
$ brew install handbrake
$ brew install media-info
Packages are also available on Linux, here's an example using Arch, package names may vary per repo:
$ sudo pacman -S handbrake-cli mediainfo
This script uses glob patterns for traversing directories, which requires Bash 4+ and globstar enabled.
$ bash --version
Instructions on how to install bash 4+ on Mac OSX
Command line options:
-a Select an audio track to use.
-b Select a subtitle track to burn in.
-c Codec to modify. Default is MPEG-4
-d Delete original.
-f Force overwriting of files if already exist in output destination.
-o Output folder directory path.
Default is the same directory as the input file.
-p The directory path of the movies to be tidied.
Default is '.', the location of this script.
-q Quality of HandBrake encoding preset. Default is 'Fast 1080p30'.
For a full list of presets in CMD line run:
HandBrakeCLI --preset-list
https://handbrake.fr/docs/en/latest/workflow/select-preset.html
-r Run transcoding. Exclude for dry run.
-s Skip transcoding if there is already a matching file name in the output destination.
Force takes precedence over skipping files and will overwrite them if both flags present.
-w Workspace directory path for processing. Set a local directory for faster transcoding over network.
Examples:
Dry run all movies in the Movies directory
.convert-videos-for-plex.sh -p Movies
Transcode all movies in the current directory force overwriting matching .mp4 files.
.convert-videos-for-plex.sh -fr
Transcode all network movies using Desktop as temp directory and delete original files.
.convert-videos-for-plex.sh -rd -p /Volumes/Public/Movies -w ~/Desktop
-r
switch. Make sure everything looks right and fix anything that doesn't manually. For example if you run without the force switch (-f) and have films with .mp4 extension then it is going to be prompting you Y/N to overwrite each and every one.-d
, you cannot get the original films back once you delete them.incomplete frame
, Header missing
, marker does not match f_code
. The total playable length may end up shorter and file size signifcantly lower. This will be due to a less than ideal file (maybe slightly corrupted) but most video players can compensate so it is not obviously noticable when watching. After running on a folder, I use the tree -h
command (brew install tree
) to output file names and size, then do a manual compare in excel to alert me to any files which seem erroneous. Using the HandBrake GUI application appears to work around many of the issues, otherwise you may need to try another converter e.g. ffmpeg. If someone asks, I could add a before/after file size comaprison with percentage tolerance and option to alert or not perform transformation. However currently I don't need it..lock
files. If you do quit out early, be sure to manually remove that file. If you run again without deleting, the script will jump over this file.So. You've got yourself a few computers lying around. Good for you. Now - time to put that distributed CPU power to good use!
This setup is especially effective on shared NAS drives, where video files can be accessed from multiple devices at once.
To avoid multiple machines stepping on each other's toes, a .lock
file is created to let other computers know that the given file is already been looked at. When the script is iterating over the files, it will first check if a .lock
file exists. If it does find one, it will move over to the next file. As soon as it starts analysing the file, it creates the lock file. At the point that the script is complete, it will remove the lock file and move on.
With this process, multiple machines can be working on the same directory, leapfrogging over each other to get the job done faster.
.lock
file may not be removed. To start the process again, you will need to manually delete the file.This is an automated script for modifying your home movie collection. Please be aware that things may go wrong or unexpected, my code could be awful or the software update gods decide to change the environment from when this was written. You may be holding your tongue out of the left side of your mouth while I'm holding mine out the right. I bear no responsibility for any loss of priceless "first steps" videos, use this software at your own risk! At least I hope this can be useful for someone else out there.
Stole the core of HandBrake CLI usage from http://pastebin.com/9JnS23fK.