Batch script to convert all ?

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
theUnknown
Posts: 3
Joined: Fri Aug 14, 2020 12:08 pm

Batch script to convert all ?

Post by theUnknown »

Hello,

I am looking for some help to convert all my mp4 in a folder and it's subfolders to mkv . Converted files should be in the same folder and preset should be H.264 720p.

I tried GUI but it's very time consuming as I have thousands of video source folder.

Thanks in advance.
theUnknown
Posts: 3
Joined: Fri Aug 14, 2020 12:08 pm

Re: Batch script to convert all ?

Post by theUnknown »

Btw I use MacBook.
theUnknown
Posts: 3
Joined: Fri Aug 14, 2020 12:08 pm

Re: Batch script to convert all ?

Post by theUnknown »

This app solved my problem if anyone interested.
http://hbbatchbeast.io
indiedoroid
New User
Posts: 1
Joined: Fri Sep 25, 2020 8:10 pm

Terrible --- Batch Folder is broken

Post by indiedoroid »

It doesn't work. I select the folder, but handbrake still just converts 1 file.

Thank you for the free software... but it's really frustrating to use. I have 1000 videos to convert... I have to drag in one by one ...
Woodstock
Veteran User
Posts: 4103
Joined: Tue Aug 27, 2013 6:39 am

Re: Batch script to convert all ?

Post by Woodstock »

You're looking in the wrong section if you're expecting the command line version to do "batch conversion" without scripting.

Typically, when someone raises this lament about the GRAPHIC user interface, the issue is found by looking at Tools->Preferences->Output Files, and finding that they have a template that automatically give the same output file name for every input file, so only the last one remains.

What are you using on that page?
LuisC
New User
Posts: 1
Joined: Tue Nov 24, 2020 11:04 am

Re: Terrible --- Batch Folder is broken

Post by LuisC »

indiedoroid wrote: Wed Sep 30, 2020 7:02 pm It doesn't work. I select the folder, but handbrake still just converts 1 file.

Thank you for the free software... but it's really frustrating to use. I have 1000 videos to convert... I have to drag in one by one ...
Yes that's the biggest drawback of this compressor. Very good by the way. But the fact you have to add every single video clip one by one to the queue really sucks.
I have no clue about coding, so I don't know how difficult it would to make that batch conversion (all files in a folder) happen.
Thanks to developers for the effort
Mac version 1.3.3 by the way
Woodstock
Veteran User
Posts: 4103
Joined: Tue Aug 27, 2013 6:39 am

Re: Batch script to convert all ?

Post by Woodstock »

Handbrake can be automated, but it does take some effort on the user's part.

For the GUI, you have to have your output file template configured to make unique file names for each input file. That isn't really that hard.

For the CLI, your batch script will have to list all the files you want to process, or pick that information up from the directory you're trying to convert. The CLI expects ONE input file on the command line, with a corresponding output file. You can select a preset to handle most of the other parameters, or detail them on each line of the script.

The CLI can do things that the GUI does not allow you to do, but it also requires more knowledge to make it happen.
mduell
Veteran User
Posts: 7438
Joined: Sat Apr 21, 2007 8:54 pm

Re: Batch script to convert all ?

Post by mduell »

Use HBBatchBeast, a third party GUI for batch encoding with HB as the backend.
xochipilli
New User
Posts: 1
Joined: Sat Apr 03, 2021 3:37 pm

Re: Batch script to convert all ?

Post by xochipilli »

As he asked for a bash script ...

You're welcome to cannibalise my one - doesn't do exactly what you want, but it's easy to look at the HandBrakeCLI man page and change up the params. Note the use of audio passthrough for friendly audio types and re-encode for unfriendly.

run with e.g. enc_dir.sh ./ 26 to run on current directory with qp of 26 .. it will convert anly media types it recognises and you may need to add new file extensions. If you stop it .. simply re-run and it will continue from where it left off with no need to worry about deleting partial encodes (it encodes into the tmp dir and moves them on completion .. make sure it's big enough... or change where it is!). Note QP should be higher for higher resolution .. and this isn't automated. Currently the script stops if handbrake fails so you know which file is causing problems .. but you could easily use mediainfo from the script to check the ouput is valid (length / res / etc.) and/or simply remove the exit command and re-run the script to check if all files are encoded (it should do nothing if they are).

I don't advise encoding into the same directory or overwritting the originals as you're asking for confusion / mistakes as it's not uncommon for something not to work well or audio / subs to go missing .. especially if they are poorly supported.

Code: Select all

#!/bin/bash
set -e
set -x

mkdir -p x265

DIRN=$(dirname '"$1"')
if [ -n $DIRN ]; then
  DIRN="${DIRN}/"
fi;

for fname in ${1}/*; do
    filename=$(basename "$fname")
    FNAME="${filename%.*}"
    EXTIN="${filename##*.}"
    EXTOUT=".mkv"
    OUTFNAME="${FNAME}${EXTOUT}"
    
    case ${EXTIN} in
        mkv|mp4|m4v|avi|flv)
            if [ -f "${DIRN}x265/${OUTFNAME}" ]; then
                echo "Skipping ${filename} as already in x265 directory"
            else
                if [ -f "${DIRN}${filename}" ]; then

                    echo "Converting ${filename} to ${FNAME}${EXTOUT} in directory ${DIRN}"
        # --audio-lang-list "jpn,eng,und"   --audio-lang-list="jpn"
                    # --subtitle-lang-list eng --subtitle-default=1
                    #  --all-subtitles --subtitle-default=1 -e x265_10bit
                    # --encoder-tune animation 
                    nice -n 0 HandBrakeCLI -i "${DIRN}${filename}" -o "/tmp/${OUTFNAME}" -e x265 --encoder-preset slow -q $2 --vfr --crop 0:0:0:0 --no-loose-crop -x "pools=20:weightb=1:force-cqp=1" --first-audio --aencoder copy --audio-copy-mask aac,ac3 --audio-fallback fdk_aac --mixdown none --no-comb-detect --no-deinterlace --no-decomb --no-detelecine --no-hqdn3d --no-nlmeans --no-chroma-smooth --no-unsharp --verbose=10  --all-subtitles --subtitle-default=1 --no-markers
                    
                    echo "exitval=${exitval}"
                    if [ $? -eq 0 ]
                    then
                        echo "Success .. moving from tmp to x265 dir"
                        mv "/tmp/${OUTFNAME}" "${DIRN}x265/"
                    else
                        echo "Failure in handbrake .. exiting"
                        exit 1
                    fi
                fi
            fi;;
    esac
done
Post Reply