Automate re-encoding numerous videos in a folder

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
imthenachoman
Posts: 7
Joined: Fri Jul 29, 2011 1:39 pm

Automate re-encoding numerous videos in a folder

Post by imthenachoman »

Description of problem or question:
I have a folder with a lot videos. I want to use HandBrakeCLI to re-encode all of them using the "H.265 MKV 720p30" preset. I also want to:
  • include all of the audio tracks in the source video and, if necessary, down-mixed them to Stereo
  • include all of the sub-title tracks in the video and, if there is an SRT file in the folder with the same name as the source, then add that as an English subtitle track
I am looking through the command line options but not able to figure out which ones I need. Any help is greatly appreciated.

HandBrake version (e.g., 1.0.0):
1.0.1

Operating system and version (e.g., Ubuntu 16.04 LTS, macOS 10.13 High Sierra, Windows 10 Creators Update):
Ubuntu 18.04
User avatar
BradleyS
Moderator
Posts: 1860
Joined: Thu Aug 09, 2007 12:16 pm

Re: Automate re-encoding numerous videos in a folder

Post by BradleyS »

You'll probably want to bash script this since HandBrake won't automatically detect an SRT sidecar. Something like:

Code: Select all

for FILE in *.mkv; do
    SRT=""
    if [[ -e "${FILE%mkv}srt" ]]; then
        SRT="--srt-file=${FILE%mkv}srt --srt-codeset=utf-8 --srt-lang=eng"
    fi
    HandBrakeCLI -i "${FILE}" --preset-import-gui --preset="H.265 MKV 720p30" --audio-lang-list=und --all-audio --audio-copy-mask=mp3 --audio-fallback=mp3 "${SRT}" -o "${FILE%mkv}-encoded.mkv"
done
Untested, but should get you started.
imthenachoman
Posts: 7
Joined: Fri Jul 29, 2011 1:39 pm

Re: Automate re-encoding numerous videos in a folder

Post by imthenachoman »

Thank you. This worked for the most part. Goal was to have 2 hour movies be roughly 2 GB and it worked for like 70% of the videos. Need to figure out why the rest are still like 4-6 gigs.

I wish there were some documented "best" settings for HandBrakeCLI to get ~2 GB videos for archival purposes. :/
User avatar
BradleyS
Moderator
Posts: 1860
Joined: Thu Aug 09, 2007 12:16 pm

Re: Automate re-encoding numerous videos in a folder

Post by BradleyS »

Quality-based encoding causes the bit rate to vary, sometimes dramatically increasing in the case of complex, high motion, and/or noisy/grainy video. Most likely your larger encodes could benefit from some noise reduction.

You can also specify a target bit rate and 2-pass encoding if you want to achieve a specific size, but we generally do not recommend this except in limited scenarios. Storage is cheap.
imthenachoman
Posts: 7
Joined: Fri Jul 29, 2011 1:39 pm

Re: Automate re-encoding numerous videos in a folder

Post by imthenachoman »

How do I do noise reduction?

And I know storage is cheap but I'm trying to keep some very special videos as backup in a limited storage area as a worst-case scenario in case all my main backups fail.
User avatar
BradleyS
Moderator
Posts: 1860
Joined: Thu Aug 09, 2007 12:16 pm

Re: Automate re-encoding numerous videos in a folder

Post by BradleyS »

I'm working on a restoration section for the documentation, but it's going to be awhile. In the meantime, check the CLI --help: https://handbrake.fr/docs/en/1.1.0/cli/cli-guide.html

Probably want something like --nlmeans=light --nlmeans-tune=film (or tune high motion / animation / whatever your source content is).
imthenachoman
Posts: 7
Joined: Fri Jul 29, 2011 1:39 pm

Re: Automate re-encoding numerous videos in a folder

Post by imthenachoman »

Thank you. I will give it a try. Thanks!
cerickso88
New User
Posts: 1
Joined: Thu Dec 27, 2018 4:51 pm

Re: Automate re-encoding numerous videos in a folder

Post by cerickso88 »

I'm confused about the loop. What is the *.mkv?

My ISO rips are all in my z: drive. This scripts just drops out and does nothing.

Thanks
imthenachoman
Posts: 7
Joined: Fri Jul 29, 2011 1:39 pm

Re: Automate re-encoding numerous videos in a folder

Post by imthenachoman »

The script loops through all of the mkv files in a folder. If all of your movies are in Z then you'll either need to update the script to look in Z or change to Z before you run the script.

Also, this script is for Linux and it sounds like you're on Windows. It won't work on Windows.
Woodstock
Veteran User
Posts: 4614
Joined: Tue Aug 27, 2013 6:39 am

Re: Automate re-encoding numerous videos in a folder

Post by Woodstock »

If you're dealing with ISO files of DVD or Bluray disks, they contain multiple titles on each, which will make it harder for a script to "just" do the "important" files. That is why most people extract the important video to other containers, with MKV being rather popular.

On the MakeMKV forum, users have contributed scripts to read ISO files and create MKV files, which would probably be the best first step in your process. You can specify a minimum length for titles to convert.
Post Reply