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

Automate re-encoding numerous videos in a folder

Post by imthenachoman » Tue Aug 07, 2018 10:10 pm

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: 1397
Joined: Thu Aug 09, 2007 12:16 pm

Re: Automate re-encoding numerous videos in a folder

Post by BradleyS » Sun Aug 12, 2018 4:10 am

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

Re: Automate re-encoding numerous videos in a folder

Post by imthenachoman » Wed Aug 15, 2018 2:06 am

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: 1397
Joined: Thu Aug 09, 2007 12:16 pm

Re: Automate re-encoding numerous videos in a folder

Post by BradleyS » Wed Aug 15, 2018 3:23 am

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

Re: Automate re-encoding numerous videos in a folder

Post by imthenachoman » Thu Aug 16, 2018 1:59 am

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: 1397
Joined: Thu Aug 09, 2007 12:16 pm

Re: Automate re-encoding numerous videos in a folder

Post by BradleyS » Thu Aug 16, 2018 2:03 am

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

Re: Automate re-encoding numerous videos in a folder

Post by imthenachoman » Thu Aug 16, 2018 2:08 am

Thank you. I will give it a try. Thanks!

Post Reply