Issue #1—May 15, 2003
Video CDs and FreeBSD
Introduction
A few weeks ago, I bought a standalone DVD player. I was very pleased with it, but I missed being able to play various videos I had on my computer's hard drive: holiday movies, music videos, trailers, etc... Most (if not all) DVD players can play Video CDs (VCDs) and support CD-R media. My solution to the problem was to create a VCD on a computer. In this article, I'll explain what a VCD is and everything you need to do to create one on a FreeBSD system.
What is a VCD?
A VCD is a CD-ROM media with several mode 2 tracks. One or more of these tracks contain a realtime MPEG-1 video stream. There are two Video CD specifications. The latest one is the Video CD 2.0 standard, which was published in 1995 and is the one covered by this article. Most standalone DVD players should support this standard. (I will not talk about the SVCD (Super Video CD) standard in this article, but the creation scheme is the same as for VCD.)
To sum up the important features of Video CD 2.0:
For PAL format:
- Video:
1150 kbit/sec MPEG-1
352 x 288 pixels @ 25 frames/second - Audio:
224 kbit/sec MPEG-1 Layer2
For NTSC format:
- Video:
1150 kbit/sec MPEG-1
352 x 240 pixels @ 29,97 frames/second - Audio:
224 kbit/sec MPEG-1 Layer2
Most of the time it is possible to reduce the audio or video bitrate, but the resulting VCD will not be Video CD 2.0 standard compliant and some players could fail playing the VCD. A lower video or audio rate sometimes allows a video to fit on one CD-R instead of two, but I will cover this problem later.
Getting Ready
The creation of a VCD requires two steps: the conversion of the video in the right MPEG-1 format and then burning the stream on the CD-R media. These steps will be accomplished by various programs.
All tests were done on FreeBSD-CURRENT and FreeBSD 4.8-RC machines with a fresh ports tree. You are strongly encouraged to use a recent system.
For the first step, I will use two applications: MPlayer and transcode.
For the CD creation, I will use VCDImager and cdrdao.
In the case of MPlayer and transcode, the best thing to do is to use the ports tree to install them. This will allow you to do a customized installation (with optimizations and/or with the needed modules).
First be sure to have an up-to-date ports tree, then as root type:
# cd /usr/ports/multimedia/transcode && make WITH_OPTIMIZED_CFLAGS=yes \ WITH_LIBA52=yes WITH_LAME=yes WITH_XVID=yes WITH_OGG=yes \ WITH_MJPEG=yes WITHOUT_QT=yes install distclean # cd /usr/ports/multimedia/mplayer && make WITH_DVD=yes \ WITH_LIBDVDREAD=yes WITHOUT_RUNTIME_CPUDETECTION=yes install distclean
For VCDImager and cdrdao, the process is more straight forward:
# cd /usr/ports/multimedia/vcdimager && make install distclean # cd /usr/ports/sysutils/cdrdao && make install distclean
If you want more information about the compilation flags used, refer to the respective Makefiles (the flags chosen suit multi-purpose usage). Note that you can directly use packages if they are available.
Video Stream Creation
The MPEG video stream creation is a two step operation: first, create the video and the audio streams, and then multiplex them in one final MPEG file.
I will illustrate the operations through one example.
In this example, I will show how to convert a DivX file (file.avi) into a PAL MPEG file ready to be burned. The operations will remain the same for any video format. To accomplish this, use transcode with the help of MPlayer:
% transcode -i file.avi -V -x mplayer -y mpeg2enc,mp2enc \ -F 1 -Z 352x288 --export_asr 2 -E 44100 -b 224 -o filevcd
This will create two files: filevcd.m1v and filevcd.mpa, which are the video and the audio streams, respectively.
The -V option tells transcode to use YUV as internal video colorspace, which is much faster than RGB processing and saves CPU/PCI bandwidth.
The -x mplayer option tells transcode to use MPlayer as audio/video import module. Audio and video of the input file are rendered by MPlayer, which supports a lot of codecs and can deal with broken files.
The -y mpeg2enc,mp2enc option to use MJPEG-tools as audio and video export modules. These modules give good quality results.
The -F 1 option sets the output encoder profile to the Video CD 2.0 specifications. Settings that would result in a non-standard video stream are simply ignored. For example, if you want to reduce the video bitrate to 1000 kBits/s (with the option -w 1000), you have to use -F 2, which selects a user VCD profile. For more information, refer to mpeg2enc(1) manual page, especially the part about the -f option.
The -Z 352x288 option tells transcode to resize the video to 352x288 (PAL format, for NTSC use 352x240).
The --export_asr 2 option sets aspect ratio to 4:3.
The -E 44100 option tells transcode to use an audio output samplerate of 44100Hz.
The -b 224 option sets the audio encoder bitrate to 224 kBits/s. You may want to use a lower value to save space, but the resulting video will not be Video CD 2.0 standard compliant and your standalone DVD player could refuse to play it.
For more details, read the transcode(1) manual page.
Now the video and audio streams must be multiplexed to obtain the MPEG file. For that job, mplex (which comes with MJPEG-tools) can be used, or tcmplex (which is part of the transcode package). I used the latter because it gave me better results when it was time to split a video through multiple VCDs.
Now let's create a file named filevcd.mpg:
% tcmplex -i filevcd.m1v -p filevcd.mpa -o filevcd.mpg -m v -F ./size.txt
tcmplex takes as inputs the both video and audio streams and produce the MPEG file(s) ready to be burned on a CD-R media.
The -m v option tells tcmplex to use the predefined settings for VCDs. In the case the streams are not VCD standard compliant (different bitrates etc...), you should use -m 1 instead, which is the default setting for tcmplex. Use tcmplex -h and tcmplex -B for some more information on options and settings.
Before I explain the -F ./size.txt option, remember how data is organized on CD media. First, the CD was designed with audio data in mind, i.e., the total capacity of a sector comes from the way the audio is coded. To be simple, there are 75 sectors per second of audio and a sector is 2352 bytes large.
2352 bytes is the maximum number of bytes that can be put on a CD-R sector. Audio data uses the entire sector, and there are no error correction codes (an error can be corrected with an interpolation from adjacent audio data). When it is time to use the CD for data storage, error correction codes are mandatory, so a part of each sector is used for that purpose. Finally only 2048 bytes on the 2352 bytes of the sector are used for the data.
Video CDs tracks are burned using mode2 format with 2324 bytes for the data and the rest for the error correction. A video does not require the same error protection as a binary file, for example. So with a larger data sector size, more data can be stored than on a typical data CD.
Let's look at general purposes CD-R capacities:
There 75 sectors per second which give 333,000 sectors on a 74 minute CD, and 360,000 sectors on an 80 minute CD, which means that we can now determine the capacity for data mode and for a VCD:
A 74 min CD in data mode is 333000x2048= 681984000 bytes, or 650MB in size.
A 74 min CD for VCD is 333000x2324= 773892000 bytes, or 738MB in size.
An 80 min CD in data mode is 360000x2048= 737280000 bytes, or 703MB in size.
An 80 min CD for VCD is 360000x2324= 836640000 bytes, or 797MB in size.
Now it's understandable why a 700MB VCD file can fit on a vanilla 74 minute CD-R. Most of the time people prefer to limit a 74 minute CD-R to 735MB max and a 80 minute CD-R to 795MB max.
The -F ./size.txt option tells tcmplex the maximal filesize of a video file. tcmplex will cut the video file into another part (another MPEG file). Once the maximum size is reached, you will need a second CD-R to burn your video. The size.txt file contains the following line:
maxFileSize = 795
In my case, I limit the filesize to 795MB (80 minute CD-R).
DVD to VCD
Until now, a video file was used as input. But what about doing a direct DVD to VCD conversion?
There are two possibilities: direct conversion from a DVD or converting from a .vob file. The former will use less hard drive space but will be slower than the latter. In this section, I will explain both ways.
Let's see an example of direct conversion:
# transcode -i /dev/acd0c -V -x dvd -T 1,-1 -a 1 -s 4 -y mpeg2enc,mp2enc \ -F 1 -Z 352x288 --export_asr 2 -E 44100 -b 224 -o filevcd
The command used is very close to the previous one. This time the program takes the DVD-ROM device as input (/dev/acd0c), -x dvd tells transcode the input will be a DVD, -T 1,-1 specifies that all chapters (-1) from the main title should be added (the title 1, which may be different in your case).
The option -a 1 is needed to extract the second audio track (0 is the default audio track, the english track most of time). I also added -s 4 to increase the volume by 4 since I often noted the volume from a DVD was too low for my taste.
Once the conversion is over, just use tcmplex as in the previous example.
It can often be more interesting to work on a .vob file than directly on the DVD. For that, the .vob has to be "ripped" from the DVD. To do so, use tccat, which is part of transcode package:
# tccat -i /dev/acd0c -T 1,-1 > movie.vob
-T 1,-1 tells tccat to add all chapters (-1) from the main title (the title 1, which may be different in your case).
Now it's time to convert the movie.vob file:
# transcode -i movie.vob -V -x vob -a 1 -s 4 -y mpeg2enc,mp2enc \ -F 1 -Z 352x288 --export_asr 2 -E 44100 -b 224 -o filevcd
Here, use the vob import module, the rest of the command line is similar to previous examples, except the title and the chapters we want do not need to be specified.
Burning the VCD
We are going to use VCDImager to make Video CD bin/cue-style CD images from MPEG files and, then, write the images onto a CD-R(W) with cdrdao.
Using VCDImager is quite straight forward. For example, to create the images from our filevcd.mpg file that was created ealier, just type:
% vcdimager -t vcd2 -c filevcd.cue -b filevcd.bin filevcd.mpg
The -t vcd2 option selects the Video CD type, in this case, the Video CD 2.0 standard.
To add many MPEG files on the same Video CD, just have to add them on the command line:
% vcdimager -t vcd2 -c image.cue -b image.bin file1.mpg file2.mpg file3.mpg
Once the images are done, it's time to burn them. The burning tool will be cdrdao, which only supports SCSI burners.
If you are the lucky owner of a SCSI burner, you can safely skip this part. For ATAPI burner owners, it is still possible to use cdrdao with the ATAPI/CAM module (atapicam(4)). This driver allows ATAPI devices (CD-ROM, CD-RW, DVD drives etc...) to be accessed through the SCSI subsystem, and so allows the use of applications like cdrdao or cdrecord. To use it, you'll need to add the following lines to your kernel configuration file:
device atapicam device scbus device cd device pass
You also need the following lines in your kernel configuration file:
device ata device atapicd
Both of which should already be present.
Then rebuild, install your new kernel, and reboot your machine.
Please be aware that the ATAPI/CAM module is not available on releases prior to 4.8-RELEASE and 5.0-RELEASE.
During the boot process, your burner should show up, like so:
acd0: CD-RW <MATSHITA CD-RW/DVD-ROM UJDA740> at ata1-master PIO4 cd0 at ata1 bus 0 target 0 lun 0 cd0: <MATSHITA CDRW/DVD UJDA740 1.00> Removable CD-ROM SCSI-0 device cd0: 16.000MB/s transfers cd0: Attempt to query device size failed: NOT READY, Medium not present - tray closed
As root, you can run the following command to get the SCSI address of the burner:
# camcontrol devlist <MATSHITA CDRW/DVD UJDA740 1.00> at scbus1 target 0 lun 0 (pass0,cd0)
To burn the previous CD image, use a command line, like this one:
# cdrdao write --device "1,0,0" --driver generic-mmc --speed 4 filevcd.cue
For more information about the command above, please refer to the cdrdao(1) manual page.
You now have a VCD ready to be played on your standalone DVD player.
You can test the VCD on your computer with MPlayer. For example:
% mplayer -vcd 2 -cdrom-device /dev/acd0c
The video is located on the second track in the case of a VCD with only one movie. For more informations about MPlayer usage, please refer to the mplayer(1) manual page.
Conclusion
I am happy with the resulting Video CDs, the quality is, from my point of view, the same as a VHS tape. Of course things can be improved, but the aim of this document is to be a good starting point, not a reference. Once again all this article shows the world that FreeBSD is not only a server OS...
Marc Fonvieille