Sample commands are shown with line wrapping for readability, a backslash indicates a continued line just like in the shell (if you see two backslashes it should be one). The example command lines are described, but as always the documentation should be consulted. These options are only a small subset of what is available.
- ImageMagick - http://www.imagemagick.org/
ImageMagick runs on most UNIX-like platforms, as well as Windows and Mac OS 9. ImageMagick provides both command-line tools and a programming library. Bindings are available for several languages, including C and Perl.
- MJPEG Tools - http://mjpeg.sourceforge.net/
MJPEG Tools should run on most UNIX-like platforms, including Mac OS X and cygwin32 (a UNIX compatibility layer for Windows). It is the back-end used by VCDEasy for creating MPEG stills.
For more information on aspect ratio correction, see the "MPEG Still images" guide on http://www.michaeltam.com/ (http://www.vcdimager.org/guides/). That guide covers more of the "why", this one concentrates on the "how".
Maintaining Aspect Ratio and Compensating for Overscan
Pixels on your computer screen are square. Pixels on your TV are rectangular. A JPEG will appear distorted on a TV screen unless it is scaled to correct for that difference. ImageMagick includes utilities that can perform image scaling quickly and easily from the command line.
You should already have a still image with no aspect ratio correction. It does not have to be a 4:3 rectangle, any shape can be used. ImageMagick supports many common (and not-so-common) image formats, and can convert any of them to JPEG for use with MJPEG Tools. ImageMagick can output raw YUV data, but it is not the correct type of YUV for MJPEG Tools.
First, the still needs to be in the right size and shape to be corrected. For PAL, resizing the image to 768x576 and then reducing it to 704x576 will maintain the aspect ratio on your TV. For NTSC, either 768x576 or 640x480 can be scaled to 704x480 with the correct aspect ratio. Use whichever makes more sense - a 1280x1024 digital camera photo would probably be slightly better resized to 768x576 and then reduced further than it would if you reduced it to 640x480 and then stretched it back out to 704x480. In truth, this is probably less important than the quality of your television.
ImageMagick's montage program is designed to create an image by scaling and tiling several images. A single image with a border is a montage with one column and one row. Since scaling defaults to maintaining the aspect ratio, montage can also be used to convert images with odd (non-4:3) resolutions without distortion (it adds borders to fill the space). montage allows you to either expand small images to fill a given size or leave them at the original size. The general syntax for using montage in this way is:
(You may see two backslashes in the example, there should only be one.)Code:montage -geometry WIDTHxHEIGHT+WBORDER+HBORDER +label +shadow \\ +frame -background BGCOLOR -quality 100 \\ INPUTFILE OUTPUT.jpg
WIDTH and HEIGHT are the width and height of your image area, the area to which your source file will be scaled. WBORDER is the border to be added at the left and right sides, and HBORDER is the border added at the top and bottom. (WIDTH + (2 x WBORDER)) and (HEIGHT + (2 x HBORDER)) should be the width and height of your uncorrected target resolution, 768x576 or 640x480.
If you want to force the source image to be scaled to WIDTH and HEIGHT regardless of aspect ratio, add a "!" after the HBORDER (no space). If you want to keep a small image small (as mentioned above), add a ">" character after the HBORDER. If you use one of these you will need to either enclose it in quotes or escape it with a backslash to prevent the shell from interpreting the character.
HEIGHT and WIDTH can be used to scale your image to fill the TV screen, with HBORDER and WBORDER set for overscan correction. BGCOLOR is the color for the background and border, and is specified using an X11 color name (black and white are probably the names you'll use most). +label, +shadow, and +frame turn off those options, since they are generally not appropriate for this use.
If you wish to do the final aspect ratio correction to 704x480 without recompressing to JPEG, you can do so by piping the output of montage to ImageMagick's convert command. Input and output file formats are normally determined by file extension or magic values, but can be specified as "format:filename". filename can be "-" for standard input and output.
(You may see two backslashes in the example, there should only be one.)Code:montage -geometry 640x480+64+48 +label +shadow +frame \\ -background black INPUTFILE miff:- \\ | convert -geometry '704x480!' -quality 100 miff:- OUTPUT.jpg
"miff" is ImageMagick's lossless compression format, which uses zlib. "-quality 100" sets the JPEG quality to 100%. OUTPUT.jpg is now ready to be converted.
Creating MPEG Stills from JPEGs
Convert a JPEG to YUV for MPEG
The first step is to convert the image to YUV with jpeg2yuv. It writes the YUV to standard output, so you'll either want to pipe it in to the next step or write it to a file.
FRAMERATE should be 29.97 for NTSC and 25 for PAL. "-I p" specifies that the image should be progressive, not interlaced. "-n 1" specifies the number of frames to encode, which is 1 for a still image. "-v 0" sets the verbosity to the minimum, all of the MJPEG Tools commands produce a great deal of mostly-useless information by default.Code:jpeg2yuv -v 0 -f FRAMERATE -j IMAGE.jpg -I p -n 1
PPM files (an uncompressed format) may also be converted, with ppmtoy4m. The general procedure is the same, but the commands are slightly more complex. This approach will not be be covered in this guide, but an understanding of the procedure outlined here should make it easy to learn to use PPMs.
Resize YUV (Optional)
A program named yuvscaler scales YUV images. If your source is not already at the right resolution (704x480 for NTSC, 704x576 for PAL), you should pipe the YUV data through yuvscaler. This is example is for NTSC, for PAL make the appropriate changes to the SIZE resolution and frame rate. Again, yuvscaler will write to standard output, so you should either pipe it to the next step or write it to a file.
"-v 0" reduces the excessively verbose output. "-O SIZE_704x480" tells yuvscaler to do output scaling with a new size of 704x480 (use 576 for PAL). For VCD 2.0 stills (not SVCD stills) you need both a high-resolution and a low-resolution version. To obtain the low-resolution version use "-O SIZE_352x240" for NTSC, "-O SIZE_352x288" for PAL.Code:yuvscaler -v 0 -O SIZE_704x480
Encode YUV to an MPEG Video Stream
mpeg2enc creates MPEG-1 and MPEG-2 video streams from YUV data.
SIZE is the size in kb, acceptable ranges are 30-200 for VCD/SVCD high-resolution stills and 20-42 for low-resolution VCD stills (larger size is higher quality). STANDARD is the TV standard to be used, "n" for NTSC and "p" for PAL. FORMAT should be 6 for VCD and 7 for SVCD. "-a 2" sets the aspect ratio to 4:3. "-v 0" makes the command quiet.Code:mpeg2enc -v 0 -a 2 -T SIZE -n STANDARD -f FORMAT -o OUTPUT_FILE
Multiplex Stream(s) to a VCD/SVCD Still
mplex multiplexes streams into finished MPEGs (including, but not limited to, still images). For SVCD stills, the command is
VCD stills require both a high-resolution version and a low-resolution version multiplexed in to one file.Code:mplex -v 0 -f 7 -o SVCDSTILL.mpg STILL.m2v
"-f 6" and "-f 7" are the same -f as for mpeg2enc, 6 for VCD and 7 for SVCD. Again, "-v 0" makes the command run quietly.Code:mplex -v 0 -f 6 -o VCDSTILL.mpg HIGHRES.m1v LOWRES.m1v
Putting Everything Together
If you were paying attention to how the above programs read and write data, you probably realized that jpeg2yuv, yuvscaler, and mpeg2enc can be chained together in a pipeline.
To convert IMAGE.jpg to NTSC SVCD still format, something like this would be appropriate:
For VCD, something like this:Code:jpeg2yuv -v 0 -f 29.97 -j IMAGE.jpg -I p -n 1 \\ | yuvscaler -v 0 -O SIZE_704x480 \\ | mpeg2enc -v 0 -a 2 -T 200 -n n -f 7 -o IMAGE.m2v mplex -v 0 -f 7 -o IMAGE.mpg IMAGE.m2v
(You may see two backslashes in the examples, there should only be one.)Code:jpeg2yuv -v 0 -f 29.97 -j IMAGE.jpg -I p -n 1 \\ | yuvscaler -v 0 -O SIZE_352x240 \\ | mpeg2enc -v 0 -a 2 -T 42 -n n -f 7 -o IMAGE-LOW.m1v jpeg2yuv -v 0 -f 29.97 -j IMAGE.jpg -I p -n 1 \\ | yuvscaler -v 0 -O SIZE_704x480 \\ | mpeg2enc -v 0 -a 2 -T 200 -n n -f 7 -o IMAGE-HIGH.m1v mplex -v 0 -f 7 -o IMAGE.mpg IMAGE-HIGH.m1v IMAGE-LOW.m1v
Addendum: VCD Photo Albums
As mentioned above, the montage program in ImageMagick can create an image with labeled thumbnails of other images, as well as labeling and framing a single image. This is ideal for creating menus for a basic VCD photo album. The documentation covers the options, perhaps at a later date I will add information about it to this guide.
+ Reply to Thread
Results 1 to 8 of 8
Thanks a lot for this unix guide, there aren't many around here. Although I am currently using windows, I plan on setting up a full linux image processing station; your guide will help a lot!
I was always wondering how VCDEasy does that magic behind the GUI window. I went to the command-prompt of Windows and ran all those mjpegtools commands without a clue to knowing how to make/resize an still image.
Thanks for the code!
Do you have any pointers about using GIMP's scripting engine to make still images or other graphic goodies?
-mjpegtools 1.6.0 for Win32/Windows download link-
I use The Gimp as my main image editing program, but I really never have messed with its scripting.A man without a woman is like a statue without pigeons.
(I realize this is an old post, but my inquiry is related)
It seems that the mjpeg tools website is not being kept up todate. And
(I guess) various files for D/L are no longer availble or to be found,
including those that are posted in this guide above, though I've spent most
of the day searching (which lead me to this page, by the way) and reading,
all on my slow dial-up.
MJPEG TOOLS webpage:
Now, the files that mjpeg tools linked to (vcdeasy modules) here:
..list a bunch of "mjpeg tools" archives, but none of them seem to have
any .EXE files listed above, and only have source files.
Mjpeg Tools - SOURCES (v1.6.2, 1.19 KB)
Mjpeg Tools v1.6.2 source files
Mjpeg Tools - SOURCES (v1.6.1, 953 KB)
Mjpeg Tools v1.6.1 source files
Mjpeg Tools - SOURCES (v1.6.0 rc2, 901 KB)
Mjpeg Tools v1.6.0 rc2 source files
Mjpeg Tools - SOURCES (v1.6.0 beta1, 1.06 MB)
Mjpeg Tools v1.6.0 beta1 source files
** 2005-09-25 MJPEGTools 1.8 released !
** 2004-02-08 mjpegtools 1.6.2 released
** 2002-05-06 1.6.0 released!
- The Purpose..
Ok. My purpose of all this, is to give these tools various kinds of
test run's with some ideas I had. And, after reviewing some of the
examples on this page and other pages regarding "mjpeg tools", I thought
that some of the ideas could be enhanced through some helper-GUI tools
that I might develope, if the various mjpeg tools proov useful
Does anyone have any information on the location of the mjpeg tools
files (above, in the guide) for D/L ??
Thanks and cheers,
I took a trip to Europe and wanted to add audio commentary to each picture. I based my process on this guide (thanks to author), and I thought it only fitting for me to contribute a few extras to the guide that others might be interested in.
Note about resizing pictures
I wanted to mention that I believe it is not necessary to resize the image to 768x576 or 640x480 before resizing to 704x480. ImageMagick will happily convert from 3072x2304 to 704x480 in one step with only one interpolation operation on the image. This should improve quality somewhat. The important thing is the aspect ratio of the image. 3072/2304 = 768/576 = 640/480 = 1.33333. So anything with a 1.33 aspect ratio will be correct for resizing to 704x480. If you have to rotate and pad a picture because it was taken sideways, then you can do the following. Rotate the picture and add padding with montage so that you achieve the 1.3333 aspect ratio. Then do the resizing all in one call to convert. (See my scripts at the bottom).
Adding Audio to Stills
It is very simple to add audio to mpeg stills, though I could not find any information on it anywhere (using linux command line tools). I was unsure whether this would work, but it did. Simply add the audio file into the mplex command. Use of the svcdstl program should not be necessary (it wasn't for me).
mplex -v 0 -f 7 -o IMG_0002.mpg IMG_0002.m2v IMG_0002.mp2
Solving MPEG-2 Compression Quality Problems
I had problems with the compression on certain images. For example, I had a picture from the gardens of Versailles that was mostly green. The hedges were trimmed in square shapes with a small grass field on the inside of the hedge. The hedge became blocky, and the edge between it and the field was hard to pick out.
The cause of this problem is the DCT threshold matrix, also known as the Quantization Matrix. Basically, the mpeg2 encoding process gets rid of high frequency detail in the image, which is where similar colors next to each other would fall in my case. The resulting filesize was only 16K no matter what quality I told it to use, even with the -H "preserve high frequency data" flag set. The quality was unacceptable.
The solution was to use a custom quantization matrix. In QM's higher values mean that more information is ignored, so I changed all the numbers to 1 to preserve all the data.
The way to force mpeg2enc to use a custom quantization matrix is not documented. Looking at the source code, you will find that it is straight-forward, however. There are two matrices in the process, and I don't really understand exactly the difference. They are both described in a single text file. The file should be a comma delimited list with 8 entries per line and 16 lines (i.e. 2 8x8 matrices).
This is the matrix that I used.
1,1,1,1,1,1,1,1 ..... repeated another 15 times
mpeg2enc -v 0 -a 2 -T 200 -n n -f 7 -K file=custom-qm -o out.m2v
The filesizes were slightly larger. The smallest files (16K) were increased to 44K. The largest files (150K) were mostly unchanged. Re-encoding 386 pictures with the custom qm resulted in an increase of about 4MB, but the quality difference was vast. If I'm not mistaken, there should be no data lost between the source 704x480 JPG and the m2v (and subsequent mpg). That statement may not be entirely accurate, but it appears that way.
Creating the SVCD
I cannot speak for making VCD's, but it may be similar. The tool I used was vcdimager for linux. I learned about how to construct the xml file from the following link
It is not difficult to decipher the xml. I did write a script to generate most of the xml file by parsing a file that lists the mpgs in the order that they should be shown.
I've included that script and some others that I wrote along the way in the attached file. They are far from general, but you may be able to adapt them to your situation. I recommend the "Advanced Bash Scripting" guide, which is easily found on google, if Bash scripting is new to you.
I hope that this extra information has been helpful. Sterno, feel free to edit what I've written and add it to the first post if you like.
I thought I should mention that your mileage will very A LOT getting these to play on DVD players. I know that the Magnovox that is the cheapest at Wal-mart right now will not play them. There is an RCA that will.
I think that most of the problems are with lack of cdr support. I'm not 100% sure. However, the RCA that Wal-mart currently sells lists CDR and SVCD under its features, and it works perfectly.
Well, only very recently I had to do some VCD-stuff again,
and for the first time, I created a menu picture (nothing fancy, just
a white background and some colored lines of text) which generated
unacceptable blurring artifacts when converted to .mpg with the "ALL 8s"
intra-matrix. Even though I already knew that mpeg2enc supports
intra-matrices which do not begin with an "8", I was afraid that the firmware
of most DVD players (mine included) wouldn't support intra-matrices whose
first coefficient is less than 8. In this way, I tried the intra-matrix with
one "8" and sixty-three "1"s, and this time the resulting .m2v frame
(encoded by HCenc, fed with a 1-frame ImageSource .AVS ) is "almost-perfect".
\ \ \