As the title suggested, HandBrake can be used to extract chapters from the DVD movie (assuming only one title exists on DVD) and write them to the data storage in M4V format using HandBrake's default preset, Fast 1080p30
.
By default, HandBrake would copy the entire DVD and write them to a single M4V file. I've developed a wrapper script, leveraging the HandBrakeCLI
command to perform the automation of reading a specific chapter from the DVD and writing it to an .m4v
file.
The files would be written in a directory named after the filename of a required chapter markers file, which is an actual CSV file with .csv
extension. Each chapter file with the .m4v
extension would get its name from the string listed in the second column of the CSV file.
For the sake of demonstration, let's say we have a DVD movie called The Brief & Empty with only three chapters.
Because of the movie's name, we're going to create a chapter markers file with the name, The_Brief_and_Empty.csv
.
You may noticed I didn't use the ampersand (&
) here as it can do unexpected things on the command line if we are not careful. To remove the headaches of which special characters need to be escaped by backslash (\
), it's far easier to have the filenames without those characters. Also, the underscore (_
) is more convenient than spaces as spaces would have to be escaped as well.
Please keep in mind that you are not always able to extract chapter names directly from the DVD as not every DVD has them (at least not the 20 DVDs that I worked on). Assuming you only have one CD/DVD drive and it's set as your primary CD/DVD drive, run the command HandBrakeCLI --input /dev/sr0 --title 0
to see if there are chapter titles included in the output. You'd need handbrake-cli
package installed to run the command.
In case you don't have the chapter titles included, you may would need to view the actual movie on the DVD and identify the chapter's name associated with the chapter's number. You may be fortunate to have a physical DVD case with the information available to you. Also, you can use the chapters option from the DVD's main menu. I was not as fortunate to have those options as the custom movies I worked on only had embedded title slides at the beginning of each chapter.
Here's the content of The_Brief_and_Empty.csv
file.
1, The Great Beginning
2, Peaking Climax
3, Closure
As you can see, the file contains a blank new line at the end, which is very important. Otherwise, the last chapter (3) wouldn't be read.
Also, you would want to be mindful of the chapter names. It's perfectly okay to have spaces here. Maybe apostrophes ('
) and double quotes ("
) are fine, but absolutely no comma (,
) should appear in the chapter names as it'd break the CSV formatting due to the comma being used as a delimiter between columns, which should only appear once on each line. Yes, you can technically use commas enclosed in quotes, but let's not introduce complications.
After running the wrapper script, we would have a directory named The_Brief_and_Empty/
. In that directory, there would be three files:
The Great Beginning.m4v
Peaking Climax.m4v
Closure.m4v
Of course, you'd need to install HandBrake if it doesn't come pre-installed with your OS. I used Ubuntu MATE. Using Debian-based Linux distribution is highly recommended here as the specific instruction for Red Hat/Fedora isn't included in this document.
To install all required files:
sudo apt-get install handbrake handbrake-cli libdvd-pkg
The libdvd-pkg
package is used to read encrypted DVDs. Please see this article (don't follow the instruction yet) for more information.
When done installing the above packages and answering all the prompts, you'd also need to install libdvdcss and have it auto-updated:
sudo dpkg-reconfigure libdvd-pkg
The article also said to install and run the regionset
command, which I believe you can completely disregard to avoid permanently modify your CD/DVD drive firmware. The implication of the command is terrifying.
To run the handbrake_automation.sh
script:
./handbrake_automation.sh The_Brief_and_Empty.csv 2>./handbrake.log
You would not see any output beside the encoding due to 2>
STDOUT redirection so that you can review the handbrake.log
after it's done (or you can just tail -f
it). Feel free to remove the 2>./handbrake.log
from the command.
Please remember to review the handbrake.log
before re-running the script or else it'd be overwritten. I've thought about including the current timestamp in the log file's name, but decided against it to avoid building up log files and filling up storage space.
You may see in the output or in the log file that there are lines starting with ERROR: dvdnav: Read Error, Expected NAV packet but none found.
No worries! It happens. The best part about the script is that you can add a second argument denoting only the chapter files that were not successfully written for various reasons (e.g. was truncated or malformed) so that you do not have to go through the entire process again. Please see below for instruction.
The script accepts a second argument, which can be multiple numbers and can be separated by commas but are not required as spaces are acceptable. You would be able to write only the specific chapters from the DVD rather than writing all the chapters in the CSV file which would overwrite the existing .m4v
files that were successfully written.
Let's suppose The Brief and Empty* has twelve consecutive chapters.
The following are acceptable:
# Only chapter 2, 9, and 12 will be read
./handbrake_automation.sh The_Brief_and_Empty.csv 2, 9, 12 2>./handbrake.log
# Only chapter 2, 9, and 12 will be read
./handbrake_automation.sh The_Brief_and_Empty.csv 12 9 2 2>./handbrake.log
# Only chapter 4 and 10 will be read anyway, but not 20.
# 20 is skipped as it doesn't exist.
./handbrake_automation.sh The_Brief_and_Empty.csv 4, "10,20" 2>./handbrake.log
The following are not acceptable as they would not run:
# Chapter 2 through 10 must be explicitly listed instead.
# (e.g. 2,3,4,5,6,7,8,9,10)
# As a result no chapter was read.
./handbrake_automation.sh The_Brief_and_Empty.csv 2-10 2>./handbrake.log
# Chapter must be numbers only. Non-digit aren't accepted.
# As a result no chapter was read.
./handbrake_automation.sh The_Brief_and_Empty.csv 3 one 2>./handbrake.log