How to convert 50 episodes from DVD into 50 .mp4 with HandBrake, easily?

Solution 1:

You can write a shell script to invoke HandBrakeCLI for each title.

Linux (source):

$ for i in `seq 4`; do HandBrakeCLI --input /dev/dvd --title $i --preset Normal --output NameOfDisc_Title$i.mp4; done

Windows PowerShell:

for ($title=1; $title -le 4; $title++) {
    &"C:\program files\handbrake\HandBrakeCLI.exe" --input D:\ --title $title --preset Normal --output "$title.mp4"
}

Solution 2:

Based on the Answer from Grilse:

This script does not use a fixed number of titles, but lets handbrake determine them.

#!/bin/bash
rawout=$(HandBrakeCLI -i /dev/dvd -t 0 2>&1 >/dev/null)
#read handbrake's stderr into variable

count=$(echo $rawout | grep -Eao "\\+ title [0-9]+:" | wc -l)
#parse the variable using grep to get the count

for i in $(seq $count)
do
    HandBrakeCLI --input /dev/dvd --title $i --preset Normal --output $i.mp4
done

Solution 3:

Adding my little grain of salt, this is the Python script I came up with to split into several chapters. The number is extracted automagically.

Note that:

  1. You need Handbrake CLI (currently available at this address: https://handbrake.fr/downloads2.php)
  2. You need to have the installation folder of Handbrake CLI in your PATH

You just need to call the following Python script with the location of DVD as the argument of the script.

#!python

import os
import subprocess
import re
import sys

# Ugly but simple way to get first argument = folder with DVD
# We will get DVD name by removing all / and \
dvd = sys.argv[1]
dvd_name = re.sub(r'.*[/\\]', r'', dvd).rstrip('/').rstrip('\\')

s = subprocess.Popen(
        f'HandBrakeCLI -i "{dvd}" -t 0', stdout=subprocess.PIPE, stderr=subprocess.STDOUT
    )
count = 0
for line in s.stdout:
    if re.search(rb"\+ title [0-9]+:", line):
        count += 1
print(f'==Extracting {count} chapters from "{dvd}"==')


for i in range(1,count+1):
    output = f"{dvd_name}_{i}.mp4"
    cmd = f'HandBrakeCLI --input {dvd} --title {i} --preset Normal --output "{output}"'
    log = f"encoding_{output}.log"
    with open(log, 'wb') as f:
        s = subprocess.Popen(cmd, stdout=f, stderr=subprocess.STDOUT)
        s.communicate()
    if not os.path.isfile(output):
        print(f'ERROR during extraction of "{output}"!')
    else:
        print(f'Successfully extracted Chapter #{i} to "{output}"')