Cryptic error message of the day

I love Linux, but sometimes you have to wonder just what was going through the minds of some of the developers. Take, for example the following error message that appeared when I (for all intents and purposes) clicked on my DVD drive:

Linux Error Messages Suck

Can you figure out what the problem was? No? Neither could I. And then I looked at the DVD drive, which was open…


Error messages should be expressed in plain language (no codes), precisely indicate the problem, and constructively suggest a solution – and I can see that it’s trying (it’s trying real hard) – but I really still would have preferred “No disc in drive. Please insert a disc.”

How to: Create an ISO image of directories in Linux

I ripped some DVDs I own the other day as I wanted to create a back-up for the kids travel DVD players (so the originals don’t get scratched and trashed on the road), only the DVDs themselves were DVD-9’s (so single-sided dual-layer with a capacity of up to 8.54GB) while I can only write DVD-5’s (single-sided single layer with a capacity of up to 4.7GB) – this isn’t a big problem as I compressed them down to fit using Handbrake and elements of Shetboy’s meticulously crafted AVI to DVD technique. However, both Brasero and GnomeBaker would flat out refuse to burn Video projects – they’d just choke on the folder containing the AUDIO_TS and VIDEO_TS folders – so why not covert the directory including the *_TS folders to an ISO and burn that? No reason why not! Let’s get it done! =P

Making the ISO

Once you’ve got a folder structure containing the AUDIO_TS folder (which is empty) and the VIDEO_TS folder (which contains your .BUP, .IFO and .VOB files), just run the following command to generate your ISO:


So, for example, you might use the following command to create an ISO of the directory containing the *_TS folders for the film Avatar:

mkisofs -o ~/AvatarDVD.iso -V Avatar -r ~/dvdprep/Avatar/

Once you hit return, you’ll see something like the following output:

I: -input-charset not specified, using utf-8 (detected in locale settings)
  0.22% done, estimate finish Wed Jul 27 12:30:27 2011
..... all the rest ......
 99.96% done, estimate finish Wed Jul 27 12:27:58 2011
Total translation table size: 0
Total rockridge attributes bytes: 1353
Total directory bytes: 4096
Path table size(bytes): 42
Max brk space used 21000
2275896 extents written (4445 MB)

Measure Twice, Cut Once

If you wanted to make sure you got it right, as in, you ONLY have the *_TS folders in the iso, not the top level folder containing those two folders also, then just open the created iso file with Archive Manager and take a look, or mount it to a folder with:

mkdir test
sudo mount -o loop -t iso9660 ./AvatarDVD.iso ./test
ls test
[ which should show "AUDIO_TS  VIDEO_TS" ]
sudo umount ./test

When you’re happy with the iso, burn it with your burning software of choice and you’re all sorted!


How-To: Install or Build a Working Version of Handbrake for Ubuntu

Update: To install a working version of Handbrake in Ubuntu 10.10 or 11.04, you’ll need to install from the PPA, and you’re probably best off doing so like this…

First, you’ll need to add the Handbrake PPA (personal package archive) to your Ubuntu system. Open up a Terminal window and use this command:
For the official builds:

sudo add-apt-repository ppa:stebbins/handbrake-releases

Or for the nightly builds:

sudo add-apt-repository ppa:stebbins/handbrake-snapshots

After the repository has been added, update your system’s listing of its repositories with this command:

sudo apt-get update

Once the repository listings have been updated, you can then install the graphical version of Handbrake with this command:

sudo apt-get install handbrake-gtk

You can also install the command line version of Handbrake with this command:

sudo apt-get install handbrake-cli

Props to Jonathon Moeller for his write up on this 10.10 technique.

If You’re Still Want to Build Your Own Copy However…

The currently available pre-packaged version of Handbrake (the Video/DVD transcoder/ripper) at the time of writing is 0.9.4 (actual filename for the 64-bit version: HandBrake-0.9.4-Ubuntu_GUI_x86_64.deb) – and it’s about as much use as a chocolate teapot on Ubuntu 10.04… You simply can’t endcode/transcode with it because it’s broken, with the Add to Queue and Start buttons permanently greyed out because the functionality behind them is mashed.

So let’s build a fresh version that works!

Step 1.) Get the Necessary Libraries

A quick trip to the command line will get everything you need (where everything you already have in this list is simply ignored):

sudo apt-get install subversion yasm build-essential autoconf libtool zlib1g-dev libbz2-dev intltool libglib2.0-dev libdbus-glib-1-dev libgtk2.0-dev libgudev-1.0-dev libwebkit-dev libnotify-dev libgstreamer0.10-dev libgstreamer-plugins-base0.10-dev

Step 2.) Get the Source Code

Create a folder for it, move to it, then grab the latest source code via subversion like this:

mkdir handbrake
cd handbrake
svn checkout svn:// hb-trunk

Step 3.) Build It!

Once we’re in the right place, this configure line with the given switch will configure and make Handbrake in one fell swoop:

cd hb-trunk
./configure --launch

Step 4.) Test It!

After a successful build, you’ll see the executable HandBrake-CLI in the build folder – but you’re probably after the GUI version, which is tucked away in build/gtk/src and called ghb – just go to the right folder, launch it and give it a try out – should be absolutely mint.

There are a stack of different options for the encoding process which can slow down the encode/transcode process and increase the quality – I went with these final settings to get high encoding quality without the encode process taking all week:

HandBrake - Working
HandBrake - Now Working! Woo-Hoo!


How To: Transcode AVI Videos to Video DVDs in Linux

It’s easy enough to rip DVDs to .AVIs or .MKVs – you can just use nice, GUI-enabled tools like HandBrake. But what about when you want to transfer an AVI back to DVD format so it can be played on a standalone DVD player? Well, the Shetboy sent me his command-line technique a while back (which we can automate to some extent) – and I’ve got to say it works flawlessly.

1.) Get the Right Tools for the Job

If you’ve ever had a look at a video DVD on your PC, you’ll notice is has a bunch of .VOB files broken up into roughly 1GB sections which contain the pieces of your movie (if it’s a DVD of TV episodes you’re more likely to have a greater number of VOBs split up into one per episode on the disc). To deal with all the transcoding we’re going to do, we need the right tools. Some of these you’re likely to already have, others maybe not.

Package pre-requisites for transcoding:
mencoder, mplayer, libxvidcore, libxvidcore4, xvid4conf, xvidtune, ffmpeg, libavcodec0d, libmpeg*, w32codecs, transcode, mjpegtools, dvdauthor

You can install them all via the following command (where any you already have will just be skipped):

sudo apt-get install mencoder mplayer libxvidcore libxvidcore4 xvid4conf xvidtune ffmpeg libavcodec0d  w32codecs transcode mjpegtools dvdauthor libmpeg*

2.) Find Out Your .AVI’s Crop Area

To do a nice transcode, best results are gained by not transcoding any black bars in the avi. This step also helps in getting a perfect re-size every time without messing up the aspect ratio.

To do this run:

mplayer -vf cropdetect <file.avi>

An mplayer window will open and the avi will play. Press the right arrow about 10 times and leave it running for a few seconds. The reason for this is that different resolutions can sometimes be used for the intro to TV shows, or company logos and things, and we want the main feature being played. After it has been showing the body of the show for a few seconds kill the mplayer window.

Output from the above command will be shown in your console window, and should look something like this:

mplayer -vf cropdetect Star.Wars.The.Clone.Wars.S01E01.HDTV.XviD-0TV.avi
MPlayer 2:0.99+1.0pre8-0ubuntu8.3 (C) 2000-2006 MPlayer Team

— A whole bunch of other stuff we don’t care about here —

Opening video filter: [cropdetect]
Opening video decoder: [ffmpeg] FFmpeg’s libavcodec codec family
Selected video codec: [ffodivx] vfm: ffmpeg (FFmpeg MPEG-4)
alsa-init: using device default
alsa: 48000 Hz/2 channels/4 bpf/65536 bytes buffer/Signed 16 bit Little Endian
AO: [alsa] 48000Hz 2ch s16le (2 bytes per sample)
Starting playback…
VDec: vo config request – 624 x 352 (preferred colorspace: Planar YV12)
VDec: using Planar YV12 as output csp (no 0)
Movie-Aspect is 1.77:1 – prescaling to correct movie aspect.
VO: [xv] 624×352 => 624×352 Planar YV12
[CROP] Crop area: X: 623..0 Y: 351..0 (-vf crop=-608:-336:618:346).
[CROP] Crop area: X: 623..0 Y: 351..0 (-vf crop=-608:-336:618:346). *** This is the crop-area of the pre-show company logos etc. – don’t use it! ***
[CROP] Crop area: X: 623..0 Y: 351..0 (-vf crop=-608:-336:618:346).

[CROP] Crop area: X: 0..623 Y: 0..351 (-vf crop=624:352:0:0).7 0
[CROP] Crop area: X: 0..623 Y: 0..351 (-vf crop=624:352:0:0).7 0
[CROP] Crop area: X: 0..623 Y: 0..351 (-vf crop=624:352:0:0).7 0 *** This is the crop-area of the main presentation which we’re going to use! ***
[CROP] Crop area: X: 0..623 Y: 0..351 (-vf crop=624:352:0:0).7 0
[CROP] Crop area: X: 0..623 Y: 0..351 (-vf crop=624:352:0:0).7 0

The last bunch of lines showing the crop area for the main presentation are what’s important here. We’ll use this info shortly.

3.) Transcode the Video File

We’ll be doing the transcoding using mencoder, which takes an un-holy number of parameters to do a great job.

An example of a command in the proper format is …

mencoder -oac lavc -ovc lavc -of mpeg -mpegopts format=dvd:tsaf -srate 48000 -ofps 25 \
-lavcopts vcodec=mpeg2video:vrc_buf_size=1835:keyint=15:vrc_maxrate=9800:vbitrate=4900:aspect=16/9:acodec=ac3:abitrate=192 \
-af lavcresample=48000 \
-vf crop=624:352:0:0,scale=720:576,expand=720:576,harddup \
-o ~/transcoded/ep01.mpg \ 

To figure out what’s going on so you can tweak the numbers appropriately, I’ve separated out the switches into a table to best explain how it works:

Unholy Amount of Command Line Switches and Parameters Needed To Transcode .AVIs to DVDs
Switch Should I Tweak It? Explanation
mencoder Haha, No. Fire up mencoder to do our bidding
-oac lavc No. Output audio using libavcodec’s audio codecs
-ovc lavc No. Output video using libavcodec’s video codecs
-of mpeg No. Output our file in MPEG format – expect large file sizes!
-mpegopts No. Tell mencoder that from now on all switches are to control the MPEG output
format=dvd:tsaf No. Create our output with timestamps on every frame to keep everything in sync.
Note that we don’t put a dash before this option because it’s the first option after specifying -mpegopts
-srate 48000 No. Output our MPEG audio at 48Khz so it’s as high quality as we can make it
-ofps 25 Up to you. Output our MPEG video at 25fps for a PAL DVD. Change this to 23.97fps for a NTSC DVD.
-lavcopts No. Start our section where we specify libavcodec’s options
vcodec=mpeg2video No. Specify we want to create MPEG2 video. Note the lack of a prefix dash and the colon on the end to specify all further options will be specifying the details of our MPEG2 video output
vrc_buf_size=1835: No. Specify our buffer size. Note the colon at the end which is necessary to combine multiple parameters to our -lavcopts switch.
keyint=15: No. Specify our keyframe interval as 15 i.e. One frame in every 15 will be a keyframe which is encoded at high quality. Note the colon.
vrc_maxrate=9800: Yes. Specify the maximum data rate for our video. Note the colon.

It’s best to use a bitrate calculator to get this value as outlined in the row below on vbitrate

vbitrate=4900: Yes The average bitrate for our variable bitrate for the video. Note the colon.

If you’re planning to burn the video you’re transcoding to a DVD (i.e. a standard, single-layer DVD5) then it’s worthwhile using a bitrate calculator to get the best number to plug in here. The bitrate calculator will just ask you for the length of the video, the audio bitrate (as specified below), and it’ll give you values to plug in for the average and maximum (see above) video bit-rates.

aspect=16/9: Up to you. The aspect ratio of our output video. Options are 16/9 for widescreen and 4/3 for old-school anamorphic. Note the colon.
acodec=ac3: No. Set the audio codec to use to AC3 (also known as Dolby Digital). Note the colon.
abitrate=192 No. Set our auto bitrate to 192 kbit/sec. Note – No colon this time as we’re at the end of our -lavcopts section! Finally!
-af lavcresample=48000 No. Resample our audio to 48Khz for maximum quality
-vf crop=624:352:0:0, Yes! Set our crop ratio to be what we retrieved from our mplayer -vf cropdetect command [eg. 624:352:0:0]. Note the comma.
scale=720:576, Yes! Set our scaling: x-scale (horizontal) is set to 720 pixels, and the y-scale (vertical is set to 576 pixels. 720×576 is the resolution of a PAL DVD, pick.

This is the important part: We set the x-scale to be the full width of our screen, and then we use the following formula to generate what our y-scale should be: yscale = (y-resolution * aspect ratio * y-crop-area) / x-crop-area [So for this example it’s (576 * (16/9) * 352) / 624 = 578 ==> If more than 576, set it to 576! Or if it’s more than 480 set it to 480 for a NTSC DVD]. Note the comma!

expand=720:576,harddup Up to you. Set our video expansion to be the resolution we finally want to use: For PAL it’s 720:576, for NTSC you’ll want to use 720:480. This might be the same as the scale details above, but they don’t have to be. The harddup video filter is used to keep the audio and video in sync when changing frame rates. Note the comma between the expanded resolution and calling the harddup filter!
-frames 7500 Use only to test. OPTIONAL: Only encode first 5 minutes to test. 7500 = 60 seconds per minute * 5 minutes * 25 frames per second.
-o <output-MPEG-filename.mpg> Yes. Your MPEG output path and filename, in this example I’m using: ~/transcoded/ep01.mpg
<input-file-name> Yes. The avi we’re using for input, in this example I’m using: ./Star.Wars.The.Clone.Wars.S01E01.HDTV.XviD-0TV.avi

So, even though it looks like a bit of a nightmare, you only need to change a few things in the entire command:

1.) Choose your output fps by setting -ofps 25 for PAL or -ofps 23.95 for NTSC.
2.) Set whether we want to output in widescreen 16/9 or old-school 4/3 by setting -aspect=16/9 or -aspect=4/3
3.) Feed in the result of running our mplayer cropdetect so we end up with something like -vf crop 624:352:0:0,
4.) Feed in the result of running our scale calculation so we end up with something like scale=720:576,
5.) Give it an input source AVI and an output file to dump the transcoded MPEG to
6.) Optionally, only transcode a couple of minutes to make sure it’s all running alright. Once you’re happy with the transcode take out the -frames line and it’ll transcode the whole video file for you.

If you really want to dig into the switches more, you could always take a look at this page which shows how to use mencoder in significantly more detail.

4.) Generate the DVD

Now we’ve done the hard part and we’ve got a big ‘ol .mpg file of our avi, it’s time to generate the DVD structure. If you’re putting multiple mpegs to a DVD, keep the file limit to about 4.2GB otherwise it won’t fit on a single-sided DVD5. If you’ve got a DVD9 writer you might be alright up to about 8GB, but I don’t have one so I haven’t tested this.

Word of Warning: When you’re generating large MPEG files for DVDs do NOT work on a FAT32 file system – FAT32 has a 4GB file limit, and if you end up with a 4GB+ file, things are going to fail.

You need to run two commands to generate a DVD:

dvdauthor -t -o newfolder file1.mpg [file2.mpg] [file3.mpg] ...
dvdauthor -o newfolder -T

The first command instructs dvdauthor to make a title out of each file (think chapter so we can skip through them easily), and can take a while to run. The second command generates our table of contents and completes near instantly. Read more via the mighty man dvdauthor if you’re that way inclined…

So, an example of the command usage, assuming I’d created the dvdprep folder in my home folder, might be:

dvdauthor -t -o ~/dvdprep/starwars1 ~/transcoded/ep01.mpg ~/transcoded/ep02.mpg ~/transcoded/ep03.mpg ~/transcoded/ep04.mpg
dvdauthor -o ~/dvdprep/starwars1 -T

Important: In the above example ~/dvdprep MUST exist but the directory starwars1 MUST NOT!

With this done, the start of each new file can be jumped to within the DVD by hitting chapter next or previous on your DVD software or player. Before burning to disc you should check that it works ok by checking with a video player like VLC and select ‘Play DVD from Directory’ or similar.

Workaround for ERR: no video format specified for VMGM error: If you get the error just mentioned, then it’s an issue with the DVDauthor v0.7.0 package. To fix things, run the command export VIDEO_FORMAT=PAL or export VIDEO_FORMAT=NTSC then run the second line (ending with -T) again and you should be fine.

5.) Burn the DVD!

That’s it! We’re done! Burn the DVD directory (i.e. the contents of ~/dvdprop/starwars1 now containing the AUDIO_TS and VIDEO_TS folders in his example) using Brasero, K3b or your burning software o’ choice. Whatever you use, make sure you burn the disc as a New Video DVD though!!

Happy transcoding!

An extra-special shout-out to the Shetboy for sending me this how-to ages back – apols. for not getting around to posting it sooner! I just knew it’d be a bugger to nail perfectly (i.e. unholy table of doom!) so I was kind putting it off =P