What is a HandBrake -?
The simplest way of making an mp4 file from a DVD that I've found to do this is with HandBrake. More information about HandBrake here https://handbrake.fr/docs/en/latest/introduction/about.html
HandBrake takes videos you already have and makes new ones that work on your mobile phone, tablet, TV media player, game console, computer, or web browser. Nearly anything that supports modern video formats.
HandBrakeCLI is a command-line interface version which allows me to use it on my Ubuntu server 16.10 as it does not have a graphical user interface.
HandBrake works with most common video files and formats, including DVD and Blu-ray discs. HandBrake leverages tools such as Libav, x264, and x265 to create new MP4 or MKV video files from these.
Installing HandBrake
sudo apt install handbrake-cli
Finding your dvd drive
Insert your disk
$ mount|grep ^'/dev'
/dev/mapper/your--server--vg-root on / type ext4 (rw,relatime,errors=remount-ro,data=ordered)
/dev/sda1 on /boot type ext2 (rw,relatime,block_validity,barrier,user_xattr,acl)
/dev/sr0 on /mnt type udf (ro,relatime,utf8)
typically your drive is sr and numbers start from zero, Lets check if your dvd device is /dev/sr0
$ ls -lt /dev | grep sr0
lrwxrwxrwx 1 root root 3 Jan 23 05:27 cdrom -> sr0
lrwxrwxrwx 1 root root 3 Jan 23 05:27 cdrw -> sr0
lrwxrwxrwx 1 root root 3 Jan 23 05:27 dvd -> sr0
lrwxrwxrwx 1 root root 3 Jan 23 05:27 dvdrw -> sr0
brw-rw----+ 1 root cdrom 11, 0 Jan 23 05:27 sr0
display the amount of available disk space for file systems memory using df in human readable form: df -h
$ df /dev/sr0 -h
Filesystem Size Used Avail Use% Mounted on
/dev/sr0 5.5G 5.5G 0 100% /mnt
What these two commands tell us is that both cdrom and dvd link to sr0 and its mounted on /mnt/ if its not already mounted you may need to mount your CD or DVD.
If your drive isn't sr0 or you have multiple drives then you can check:
$ ls -lt /dev/{cd,dvd,scd,sr}*
ls: cannot access '/dev/scd*': No such file or directory
lrwxrwxrwx 1 root root 3 Jan 24 05:36 /dev/cdrom -> sr0
lrwxrwxrwx 1 root root 3 Jan 24 05:36 /dev/cdrw -> sr0
lrwxrwxrwx 1 root root 3 Jan 24 05:36 /dev/dvd -> sr0
lrwxrwxrwx 1 root root 3 Jan 24 05:36 /dev/dvdrw -> sr0
brw-rw----+ 1 root cdrom 11, 0 Jan 24 05:36 /dev/sr0
To mount a CD or DVD you need to use the mount command.
/mnt/ is an accepted as a place to manually and temporarily mount external media.
There is also a /media directory created as a place under which multiple media, external or otherwise, may be automatically mounted.
Here I have mounted to /mnt/ but you can change this for /media/ or /media/dvd/ in you wish.
sudo mount /dev/sr0 /mnt/
The DVD will be mounted and you can navigate to the /mnt folder and perform a directory listing within the terminal window.
$ cd /mnt
/mnt$ ls -lt
total 4
dr-xr-xr-x 2 nobody nogroup 1860 Oct 1 2007 VIDEO_TS
dr-xr-xr-x 2 nobody nogroup 40 Oct 1 2007 AUDIO_TS
Your DVDs may be encrypted with the Content Scramble System (CSS).
You can install DVD support through free and open source software library for accessing and unscrambling using
libdvdcss2 via your ubuntu's apt package manager with the following command line:
sudo apt update && sudo apt install libdvdcss2
during this process you may be requested to reconfigure dpkg using:
sudo dpkg-reconfigure libdvd-pkg
Converting your dvd
sudo HandBrakeCLI -i /mnt/VIDEO_TS -o /path/to/output/your_dvd_title.mp4 -e x264 -q 20
Here I have used simple options
- -i /mnt/VIDEO_TS Set input device as VIDEO_TS on /mnt/
- -o <string> Sets output file name
- -e x264 Sets video library encoder
There are several available encoder options: x264/x265/mpeg4/mpeg2/VP8/theora the default is mpeg4.
I have chosen x264 which is a free software library developed by VideoLAN for encoding video streams into the H.264/MPEG-4 AVC format
as x265 is significantly more taxing on the processor than x264 and my linux hobby server is just a re-purposed
single core Intel(R) Celeron(R) CPU 2.93GHz 64 bits with 2G Memory.
Getting more chapter and track detail
When Title is not stipulated then Title 1 is used as default.
This is ok if there is only one "episode" but I only found that out when I got to disc 4 when converting my Band of Brothers box set, that only the first episode was being converted.
To overcome this I searched t'internet to work out how to use --title (-t) option
### Source Options-----------------------------------------------------------
-i, --input <string> Set input device
-t, --title <number> Select a title to encode (0 to scan only, default: 1)
-L, --longest Select the longest title
-c, --chapters <string> Select chapters (e.g. "1-3" for chapters 1 to 3, or "3" for chapter 3 only, default: all chapters)
--angle <number> Select the DVD angle
--previews <#:B> Select how many preview images are generated (max 30), and whether or not they're stored to disk (0 or 1). (default: 10:0)
--start-at-preview <#> Start encoding at a given preview.
--stop-at <unit:#> Stop encoding at a given frame, duration (in seconds), or pts (on a 90kHz clock)
Scanning each disc using the -t0 option, showed only 3 titles per disc in my case
HandBrakeCLI -i /mnt/ -t0
This allowed me to choose per title (Note title 3 was just a logo shot).
sudo HandBrakeCLI -i /mnt/VIDEO_TS --title 2 -o /var/www/html/films/BandofBrothers_disc4_Title2.mp4 -e x264 -q 20
this quickly became automated using the for and seq options.
for i in `seq 1 2`; do sudo HandBrakeCLI -i /mnt/VIDEO_TS --title $i -o /var/www/html/films/BandofBrothers_disc4_Title$i.mp4 -e x264 -q 20; done
Using sequence in this way allows for a number of options
seq --help
Usage: seq [OPTION]... LAST
or: seq [OPTION]... FIRST LAST
If you wanted to convert titles 2 to 3, 3 to 5, or whatever else, just alter the seq command: "seq 2 3", "seq 3 5", etc.
This produces separate files named, for title 1 for example, NameOfDisc_Title1.mp4.
Link to my reference source for seq titles here
On a few DVDs I came across error with handbrake and libdvdnav as shown below.
libdvdnav: DVD disk reports itself with Region mask 0x00fd0000. Regions: 2
libdvdnav: Language 'en' not found, using '��' instead
libdvdnav: Menu Languages available: ��
[08:56:27] scan: title angle(s) 2
ERROR: dvd: dvdnav_sector_search failed -
[08:56:27] scan: decoding previews for title 1
libdvdnav: DVD disk reports itself with Region mask 0x00fd0000. Regions: 2
libdvdnav: Language 'en' not found, using '��' instead
libdvdnav: Menu Languages available: ��
[08:56:27] scan: title angle(s) 2
[08:56:27] libhb: scan thread found 0 valid title(s)
No title found.
Sometimes this manifested with the replacement character (often a black diamond with a white question mark � or an empty square box)
this is a symbol found in the Unicode standard at codepoint U+FFFD in the Specials table.
It is used to indicate problems when a system is unable to render a stream of data to a correct symbol.
I found the quick win for this is to Specify --no-dvdnav option i.e.
sudo HandBrakeCLI --no-dvdnav -i /media/ubuntu/VIDEO_TS -t 01 -o IceAge_2.mp4 -e x264 -q 20
Extra HandBrake Options I have tried out and of note
- -2 Two pass to improve quality
- -T Tubo on first pass to speed up two pass
- -O Optimise for HTTP Streaming
Lets include Two pass & Turbo modes and lets make it more automated note use of sudo bash -c as I found the sudo was expiring due to the long handbrake conversion times
sudo bash -c 'for i in `seq 12`; do HandBrakeCLI -i /mnt/VIDEO_TS -t $i -o /var/www/html/Comedy/Peter_Kay/PhoenixNights_1_Episode$i.mp4 -2 -T -e x264 -q 20; done'
All this means I still have to search the DVD for names and titles and manually identify
I also had a diffictly with some of my DVD's from Sony which had multiple 90+ titles of varying length a number of which put the film tracks out of sequence.
Using in -t0 to list the titles and then outputting the list to a file which allows a grep to find if the Main Feature is listed
$ grep main -i -A35 -B1 startrek98titles.txt
+ title 20:
+ Main Feature
+ vts 10, ttn 1, cells 0->39 (2904289 blocks)
+ duration: 02:01:43
+ size: 720x576, pixel aspect: 64/45, display aspect: 1.78, 25.000 fps
+ autocrop: 74/74/0/2
+ support opencl: no
+ support hwd: not built-in
+ chapters:
+ 1: cells 0->10, 286733 blocks, duration 00:11:33
+ 2: cells 11->12, 174913 blocks, duration 00:07:29
+ 3: cells 13->14, 194969 blocks, duration 00:08:04
+ 4: cells 15->16, 170719 blocks, duration 00:07:06
+ 5: cells 17->18, 201123 blocks, duration 00:08:16
+ 6: cells 19->20, 190024 blocks, duration 00:07:51
+ 7: cells 21->22, 218969 blocks, duration 00:08:49
+ 8: cells 23->24, 234102 blocks, duration 00:09:42
+ 9: cells 25->28, 199588 blocks, duration 00:08:37
+ 10: cells 29->29, 147504 blocks, duration 00:06:39
+ 11: cells 30->31, 200850 blocks, duration 00:08:32
+ 12: cells 32->33, 189604 blocks, duration 00:07:45
+ 13: cells 34->35, 211051 blocks, duration 00:08:18
+ 14: cells 36->37, 107967 blocks, duration 00:04:55
+ 15: cells 38->39, 176173 blocks, duration 00:08:09
+ audio tracks:
+ 1, English (AC3) (5.1 ch) (iso639-2: eng), 48000Hz, 448000bps
+ 2, English (AC3) (Visually Impaired) (5.1 ch) (iso639-2: eng), 48000Hz, 448000bps
+ 3, English (AC3) (2.0 ch) (iso639-2: eng), 48000Hz, 192000bps
+ subtitle tracks:
+ 1, Dansk (iso639-2: dan) (Bitmap)(VOBSUB)
+ 2, English (iso639-2: eng) (Bitmap)(VOBSUB)
+ 3, Nederlands (iso639-2: nld) (Bitmap)(VOBSUB)
+ 4, Norsk (iso639-2: nor) (Bitmap)(VOBSUB)
+ 5, Suomi (iso639-2: fin) (Bitmap)(VOBSUB)
+ 6, Svenska (iso639-2: swe) (Bitmap)(VOBSUB)
+ 7, English (Closed Caption) (iso639-2: eng) (Bitmap)(VOBSUB)
+ 8, English (iso639-2: eng) (Bitmap)(VOBSUB)
This shows that the main feature is title 20
Including all Audio tracks and Subtitles
You can include additional audio tracks and subtitles using hyphen a or s followed by required track numbers. Additional track numbers where they are not present will just exclude them and doesn't create a problem
sudo bash -c 'for i in `seq 1 6`; do HandBrakeCLI -i /media/VIDEO_TS -t $i -o Peter\ Kay\ \(That\ Peter\ Kay\ Thing\)\ Episode\ $i.mkv -2 -T -e x264 -q 20 -s "1,2,3,4,5,6" -a "1,2,3,4,5,6"; done'
Getting more dvd disc info
lsdvd is a small application for reading the contents of a DVD and printing the contents to your terminal and dump for further processing in external applications
Lets test this on my Independance Day DVD which has both the theatrical and extended versions with the extended version having an extra 08 minutes 24 seconds
$ lsdvd /dev/sr0
Disc Title: INDEPENDENCE_DAY
Title: 01, Length: 02:27:16.440 Chapters: 57, Cells: 79, Audio streams: 03, Subpictures: 11
Title: 02, Length: 02:18:52.080 Chapters: 54, Cells: 76, Audio streams: 02, Subpictures: 11
Title: 03, Length: 00:00:07.880 Chapters: 01, Cells: 01, Audio streams: 03, Subpictures: 11
Title: 04, Length: 00:00:44.800 Chapters: 02, Cells: 02, Audio streams: 03, Subpictures: 11
Title: 05, Length: 00:00:00.480 Chapters: 01, Cells: 01, Audio streams: 03, Subpictures: 11
Longest track: 01
To extract the disk Title
lsdvd /dev/sr0 | head -1 | awk '{print $3}'
With film DVDs The longest track is commonly the required Title
lsdvd /dev/sr0 | grep Longest | awk '{print $3}'
Now it gets interesting!
The more I learn the more there is to know. Having ripped lots of DVDs now. I have found out about lots of things that affect my choices.
putting lots of this together
HandBrakeCLI --main-feature -i /media/VIDEO_TS/ -o Lord\ of\ the\ Rings\ 5\ -\ Return\ of\ the\ King.mkv -2 -T --audio-lang-list eng --aencoder faac,copy:ac3 --native-language eng --subtitle scan,1,2,3,4,5,6,7,8,9,10 --subtitle-default=1 -e x264 -q 22
Breaking these options down.
- --main-feature Detects and selects the main feature title.
- -i (--input ) Sets input file or "source".
- -o (--output ) Sets destination file name.
- -2 --two-pass Use two-pass mode.
- -T (--turbo) Using 2-pass use "turbo" on the first pass to improve speed.
- --a--audio-lang-list eng First audio matching each language will be added to output.
- --aencoder faac,copy:ac3 Select audio encoder(s) to add stereo and Surround options
- --native-language eng Specify your language preference to work with the following subtitle scan
- --subtitle scan,1,2,3,4,5,6,7,8,9,10 scan the source for 'eng' subtitles that appear only 10% (or less) such as forced subtitles
- --subtitle-default=1 sets the subtitle default to auto show forced subtitles
- -e x264 (--encoder ) Select video encoder: x264
- -q 22 (--quality ) Set video quality (e.g. 22.0)
Shrinking Video!
Sometimes downloaded videos are very large occasionaly upto 10Gb, The simplest way I have found so far is to use ffmpeg to re-encode whick in the following changed the film from 7Gb to 1.6Gb
sudo ffmpeg -i Frozen\ 2\ \(2019\).mkv -vcodec libx264 -crf 24 Frozen\ 2a\ \(2019\).mkv
Making Thumnails
when using vlc on my amazon tv I wanted to add a thumbnail instead of the cone. To do this I have employed ffmpeg which is part of the libav library (not sure this worked as I think It need to be a specific size and naming convention i.e. cover.png)
sudo ffmpeg -i matrix.mp4 -r 30 -ss 00:00:37 -t 00:00:01 -f image2 matrix%03d.png
I think the ideas for this came from something like https://stackoverflow.com/questions/25308594/using-avconv-to-get-a-single-frame-from-h264-video-at-set-time
Further reading https://matroska.org/technical/cover_art/index.html and https://mkvtoolnix.download/doc/mkvpropedit.html
How To Unmount a DVD Using The Command Line
To unmount the DVD all you have to do is run the following command:
A few issues arise when unmounting or ejecting drives. Sometimes they don't want to because they are mounted.
Linux / UNIX will not allow you to unmount a device that is busy. There are many reasons for this (such as program accessing partition or open file),
but the most important one is to prevent the data loss. Try the following command to find out what processes have activities on the device/partition.
If your device name is /dev/sdb1, enter the following command as root user:
# lsof | grep '/dev/sda1'
How To Eject a DVD Using The Command Line
To eject the DVD using the command line use the following command:
To close the tray, type:
to toggle (if open, close and if closed, open) type:
I know I can use ffmpeg to convert .mp4 to .webm format
sudo bash -c 'for f in /var/www/html/films/*.mp4; do ffmpeg -i $f -c:v libvpx -crf 10 -b:v 1M -c:a libvorbis /var/www/html/films/matrix.webm'; done
Just looking to see if I can rip to webm but haven't tested yet.
sudo bash -c 'for i in `seq 1 2`; do HandBrakeCLI -i /mnt/VIDEO_TS --title $i -o /var/www/html/films/BandofBrothers_disc4_Title$i.webm -e v9 -q 0; done'
When I needed to copy files to usb stick
plug in your USB stick then check dev folder for new entries here my stick is sdb1
$ ls -lt /dev/ | head -n28
total 0
drwxr-xr-x 2 root root 720 Feb 1 11:50 block
brw-rw---- 1 root disk 8, 17 Feb 1 11:50 sdb1
brw-rw---- 1 root disk 8, 16 Feb 1 11:50 sdb
drwxr-xr-x 2 root root 3660 Feb 1 11:50 char
crw-rw---- 1 root disk 21, 2 Feb 1 11:50 sg2
drwxr-xr-x 2 root root 100 Feb 1 11:50 bsg
crw-rw---- 1 root tty 7, 130 Jan 31 23:10 vcsa2
crw-rw---- 1 root tty 7, 2 Jan 31 23:10 vcs2
crw--w---- 1 root tty 4, 2 Jan 31 23:10 tty2
lrwxrwxrwx 1 root root 3 Jan 29 21:03 cdrom -> sr0
lrwxrwxrwx 1 root root 3 Jan 29 21:03 cdrw -> sr0
drwxr-xr-x 7 root root 140 Jan 29 21:03 disk
lrwxrwxrwx 1 root root 3 Jan 29 21:03 dvd -> sr0
lrwxrwxrwx 1 root root 3 Jan 29 21:03 dvdrw -> sr0
brw-rw----+ 1 root cdrom 11, 0 Jan 29 21:03 sr0
brw-rw---- 1 root disk 8, 1 Jan 28 14:59 sda1
brw-rw---- 1 root disk 253, 1 Jan 28 14:59 dm-1
crw-rw---- 1 root lp 99, 0 Jan 28 14:59 parport0
crw-rw---- 1 root video 246, 0 Jan 28 14:59 lirc0
crw-rw----+ 1 root video 81, 2 Jan 28 14:59 radio0
crw-rw----+ 1 root video 81, 0 Jan 28 14:59 video0
drwxr-xr-x 3 root root 60 Jan 28 14:59 v4l
crw-rw----+ 1 root video 81, 1 Jan 28 14:59 vbi0
crw------- 1 root root 89, 1 Jan 28 14:59 i2c-1
Now I can mount it
sudo mount /dev/sdb1 /mnt/myusbstick/
and unmount with:
sudo mount /dev/sdb1 /mnt/myusbstick/
Jump to Ubuntu, Linux and me on....