mummybot / convert-videos-for-plex

Converts all videos in nested folders to h264 and audio to aac using HandBrake with the Fast 1080p30 preset.
54 stars 9 forks source link

Convert videos for Plex

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.

Prerequisites

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

Usage

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

Things to consider

Running multiple machines

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.

Caveats

Disclaimer

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.

Credit

Stole the core of HandBrake CLI usage from http://pastebin.com/9JnS23fK.