Announcement: - Bash script to encode videos using HandBrakeCLI

Discussion of the HandBrake command line interface (CLI)
Forum rules
An Activity Log is required for support requests. Please read How-to get an activity log? for details on how and why this should be provided.
Post Reply
New User
Posts: 1
Joined: Mon Feb 06, 2023 2:25 am

Announcement: - Bash script to encode videos using HandBrakeCLI

Post by encode2m4v »

Hello, I have been working on learning Bash shell scripting and have written a shell script which uses HandBrakeCLI to encode video files to a format which works better for my old slow video hardware (primarily WD TV Live). I have added a few options useful for mass encoding, but it only encodes one file at a time (unless run in multiple instances.) I realize there are a lot of these which have been written over the years, but I thought I would continue since I was just learning Bash. I believe it should work on Mac, Linux and Windows (under WSL), but have primarily used it on Mac.

I realize the script is coded in a very inelegant way, since I am just learning Bash, but I hope that provides an easy way to see what it is attempting to do...

Anyway, more information is below, and I hope it will be useful. I am happy to respond to questions and appreciate any comments.

Regards, Mitch


Script name:

Description: Transcodes files and recurses folders in current directory "." (or
"root directory/".) Finds all .mp4 (or "--inext") files and transcodes
them into an .m4v (or "--ext") file using the "HQ 1080p30 Surround"
(or --preset) HandBrakeCLI preset. Originally meant to be copied to the
directory to start the recursive search (root) and run from there, it
also has command line switches (arguments) which can be passed in to set
the "root" folder and other switches to set processing options.


If run without switches:
  • searches in current directory and subdirectories for all video files with a .mp4 extension
  • will prompt for a subtitle extension to add or no subtitle.
  • will prompt whether or not to upconvert to 1080
  • will start transcoding all found video files to .m4v files using the HandBrakeCLI "HQ 1080p Surround" preset
  • Will place these transcoded files in the same directory as the source (input) video file.
  • Will not move or remove any files.
  • Will not display the HandBrakeCLI encoding details.
Command line switches - provide ability to:
  • specify a "transcode directory/" to start search for video files and subtitle files.
  • specify input video file extension to search for (.mp4, .mov, .mkv, ...) or all video files (".*")- (-i / --inext)
  • specify output transcoded video file extension (.mp4 , .m4v, .mkv ...) - (-e / --ext)
  • specify subtitle file extension (?.srt / "none") - (-s / --subtitle=)
  • specify whether to stop encoding (-b / --break) if a subtitle file is not found or continue (-c / --continue)
  • specify a HandBrake preset (-p / --preset=)
  • specify specific HandBrake transcoding options (-t / --transopt)
  • use a HandBrake .json settings file (-j / --json=)
  • upconvert the video to 1080 (-u / --upconvert) or not (-n / --noupconvert)
  • specify an upconvert level (720, 1080, 2160, 4320) (-x / --xlevel)
  • output the transcoded file(s) to a specified directory (-o / --output=)
  • re-create the input directory structure when output to a different directory (-r / --recreatedirs)
  • move input video and subtitle files once they have been transcoded (-m / --move=)
  • delete input file(s) after they have been transcoded (-k / -kill)
  • show details of HandBrakeCLI encoding or not (-d / --details)
  • display a "manpage" like information (-g / --guide)
Post Reply