Content here is by:
Michael Still
mikal@stillhq.com

All my Open Source projects
Online CVS server
Extracted view of CVS
Home
Site map
July 2008
Sun Mon Tue Wed Thu Fri Sat
   
   

ImageMagick book
MythTV book





When I went to turn a series of JPEGs taken by my webcam into a MPEG movie using linux (and only using open source software), I found that it wasn't nearly as easy as I had hoped that I would be.

This page documents the solution I used and the steps I followed to get it all working. It is by no means the only, or even the best, answer. It is merely what worked for me.

I hope that someone out there finds this useful...


Tue, 21 Jun 2005



The new way of converting JPEGs to MPEGs

    I wrote the
    JPEG to MPEG howto a while ago, and used the best tools that I could find at the time. It was a complicated, evil process, but people still email me a couple of times a month and thank me for it. I think they'll like this way even more.

    For my PhD, I'm taking large numbers of images and displaying them one after another on a monitor so that I can capture the images with a webcam. The reason is a little convoluted, so let's settle for I'm trying to see what sort of image distortion the webcam will impose. I figured that the best way to do this was to simply have a MPEG2 movie that was the images, play it on the machine, and then point the webcam at the monitor. At about this time in a twist of wonderful fate I discovered I could do this in a simple command line with ImageMagick. Let's see how (all of these instructions are based on Debian unstable, but should work anywhere):

    Install ImageMagick

      apt-get install imagemagick
      


    Install mpeg2vidcodec

    This isn't packaged for Debian, but I've put a request for package bug in. Do this:

      wget ftp://ftp.mpegtv.com/pub/mpeg/mssg/mpeg2vidcodec_v12.tar.gz
      tar xvzf mpeg2vidcodec_v12.tar.gz
      cd mpeg2
      make
      cd src/mpeg2dec
      cp mpeg2decode /usr/local/bin
      cd ../mpeg2enc
      cp mpeg2encode /usr/local/bin
      


    Do the thing

    So, now we can do the thing. In this instance we have a bunch of TIFF files we want to turn into an MPEG2 video.

      convert *.tif foobar.mpg
      


    And we're done. Nice, isn't it?

    Technorati tags for this post:

posted at: 10:20 | path: /jpeg2mpeg | permanent link to this entry
There are 6 comments on this post, and 31 comments which didn't survive moderation. 3 were blocked by trained gerbils. Click here to see them.


Thu, 12 Sep 2002



Howto

    Abstract

    Recently, I went to a conference and was keen enough to setup a webcam. Now I have 27,000 JPEG images, which I want to turn into a simple MPEG movie. This document details the process I went through to make this happen...

    This was all done on a pretty vanilla Redhat 7.1 box, for those who care about such things.


    MPEG tools

    The project I found which did the conversion is called MJPEG tools. The URL for this project is http://mjpeg.sourceforge.net. There were lots of dependancies, which we should talk about first...


    Dependancies

    libavifile

    This library can be found at http://avifile.sourceforge.net. It is needed by MJPEG tools to be able to handle AVI files. The version I used was 0.7-0.7.15. It follows the standard autotools install process:

    ./configure
    make
    make install

    libsdl

    "Simple DirectMedia Layer is a cross-platform multimedia library designed to provide fast access to the graphics framebuffer and audio device. It is used by MPEG playback software, emulators, and many popular games, including the award winning Linux port of "Civilization: Call To Power." Simple DirectMedia Layer supports Linux, Win32, BeOS, MacOS, Solaris, IRIX, and FreeBSD." -- http://www.libsdl.org/index.php

    libsdl can be found at http://www.libsdl.org, and is needed for some of the MJPEG playback routines. I used version 1.2.4. Again, installing is done by simply:

    ./configure
    make
    make install

    nasm

    NASM is an assembler, and the authors are none to modest. The web page is http://sourceforge.net/projects/nasm and to quote from that page:

    "This is NASM - the famous Netwide Assembler. Back at SourceForge and in intensive development!"

    It can't be that famous, I've never heard of it before.

    An explaination of where NASM came from can be found in the documentation:

     

    The Netwide Assembler grew out of an idea on comp.lang.asm.x86 (or possibly alt.lang.asm - I forget which), which was essentially that there didn't seem to be a good free x86-series assembler around, and that maybe someone ought to write one.

    • a86 is good, but not free, and in particular you don't get any 32-bit capability until you pay. It's DOS only, too.

    • gas is free, and ports over DOS and Unix, but it's not very good, since it's designed to be a back end to gcc, which always feeds it correct code. So its error checking is minimal. Also, its syntax is horrible, from the point of view of anyone trying to actually write anything in it. Plus you can't write 16-bit code in it (properly).

    • as86 is Minix- and Linux-specific, and (my version at least) doesn't seem to have much (or any) documentation.

    • MASM isn't very good, and it's expensive, and it runs only under DOS.

    • TASM is better, but still strives for MASM compatibility, which means millions of directives and tons of red tape. And its syntax is essentially MASM's, with the contradictions and quirks that entails (although it sorts out some of those by means of Ideal mode). It's expensive too. And it's DOS-only.

    So here, for your coding pleasure, is NASM. At present it's still in prototype stage - we don't promise that it can outperform any of these assemblers. But please, please send us bug reports, fixes, helpful information, and anything else you can get your hands on (and thanks to the many people who've done this already! You all know who you are), and we'll improve it out of all recognition. Again.

     
    --http://home.attbi.com/~fbkotler/nasmdoc1.html#section-1.1 

    Install it by doing the autotools thing once more. Note that the make install step is broken, and that I had to do this to get it to install:

    mkdir bin
    mkdir man
    mkdir man/man1
    make install
    cp bin/nasm /usr/local/bin/
    cp man/man1/n* /usr/local/man/man1/

    jpeg-mmx

    We'll also need the MJPEG people's MMX JPEG library. I am not sure how this differs from the standard JPEG library, which also seems to be an option here... I downloaded version 0.1.4, and did the autotools thing.

    For some reason they have disabled make install. I did:

    [root@localhost jpeg-mmx-0.1.4]# make install
    Make install has been disabled
    If you really want to install libjpeg-mmx, please use make install_real
    [root@localhost jpeg-mmx-0.1.4]# make install_real 
    /usr/bin/install -c -m 644 jconfig.h /usr/local/include/jconfig.h
    /usr/bin/install -c -m 644 ./jpeglib.h /usr/local/include/jpeglib.h
    /usr/bin/install -c -m 644 ./jmorecfg.h /usr/local/include/jmorecfg.h
    /usr/bin/install -c -m 644 ./jerror.h /usr/local/include/jerror.h
    /usr/bin/install -c -m 644 ./jinclude.h /usr/local/include/jinclude.h
    /usr/bin/install -c -m 644 libjpeg-mmx.a /usr/local/lib/libjpeg-mmx.a
    [root@localhost jpeg-mmx-0.1.4]# 

    And annoyingly, I also had to manually copy a file:

    cp jpegint.h /usr/local/include/

    movtar

    movtar is actaully written by the MJPEG people. You can download it from http://download.sourceforge.net/mjpeg/. I used the 1.3.0 release. The install was auttooled once more. I am sure by now I don't have to explain how to do that...


    Compiling MJPEG tools itself

    I downloaded version 1.6.0 from the sourceforge website. Yet again, the install process was:

    This wouldn't make out of the box:

    g++ -g -O2 -o .libs/divxdec divxdec.o lav_common.o -Wl,-rpath -Wl,/usr/local/lib  
    -L/root/mjpegtools-1.6.0/utils -lmjpegutils -L/usr/local/lib 
    /usr/local/lib/libaviplay.so -lz -L/usr/X11R6/lib -lSM -lICE -lXxf86dga -lXxf86vm 
    -lXi -lXft -lXv -lXext -lX11 -lpthread -lnsl -ldl -lm ./.libs/liblavfile.so -L/usr/lib 
    /usr/lib/libglib.so -lmovtar -L/root/mjpegtools-1.6.0/../jpeg-mmx-0.1.4 
    ./.libs/liblavplay.so ./.libs/liblavjpeg.so -ljpeg-mmx -Wl,--rpath -Wl,/usr/local/lib
    ./.libs/liblavplay.so: undefined reference to `SDL_Quit'
    ./.libs/liblavplay.so: undefined reference to `SDL_WM_SetCaption'
    ./.libs/liblavplay.so: undefined reference to `SDL_CreateYUVOverlay'
    ./.libs/liblavplay.so: undefined reference to `SDL_LockYUVOverlay'
    ./.libs/liblavplay.so: undefined reference to `SDL_EventState'
    ./.libs/liblavplay.so: undefined reference to `SDL_UnlockSurface'
    ./.libs/liblavplay.so: undefined reference to `SDL_LockSurface'
    ./.libs/liblavplay.so: undefined reference to `SDL_DisplayYUVOverlay'
    ./.libs/liblavplay.so: undefined reference to `SDL_UpdateRect'
    ./.libs/liblavplay.so: undefined reference to `SDL_Init'
    ./.libs/liblavplay.so: undefined reference to `SDL_GetError'
    ./.libs/liblavplay.so: undefined reference to `SDL_UnlockYUVOverlay'
    ./.libs/liblavplay.so: undefined reference to `SDL_FreeYUVOverlay'
    ./.libs/liblavplay.so: undefined reference to `SDL_SetVideoMode'
    collect2: ld returned 1 exit status

    Until I had applied this patch.

    Then we can go ahead and make the project:

    ./configure
    make
    make install

    For those who are interested, my build configuration at the end of the configure script looked like:

     MJPEG tools 1.6.0 build configuration :
    
        - X86 Optimizations:
          - MMX/3DNow!/SSE enabled      : true
          - cmov support enabled        : true
       ***********************************************************************
       * NOTE:                                                               *
       *   The resultant binaries will ***NOT*** run on a K6 or Pentium CPU  *
       ***********************************************************************
        - video4linux recording/playback: true
        - software MJPEG playback       : true
        - movtar playback/recording     : true
        - Quicktime playback/recording  : false
        - AVI MJPEG playback/recording  : true (always)
        - libDV (digital video) support : false  
        - AVIFILE divx encoding support : true
        - Gtk+ support for glav         : true
        - glibc support for >2GB files  : true

    Encoding the JPEGS into the MPEG

    Finally, we can use MJPEG tools to convert the JPEGs into a MPEG stream.

    "You can use jpeg2yuv to create a yuv stream from separate JPEG images. This stream is sent to stdout, so that it can either be saved into a file, encoded directly to a mpeg video using mpeg2enc or used for anything else."

    "Saving an yuv stream can be done like this: jpeg2yuv -f 25 -j image%05d.jpg > result.yuv"

    "Creates the file result.yuv containing the yuv video data with 25 FPS. The -f option is used to set the frame rate. Note that image%05d.jpg means that the jpeg files are named image00000.jpg, image00001.jpg and so on. (05 means five digits, 04 means four digits, etc.)"

    "If you want to encode a mpeg video directly from jpeg images without saving a separate video file type: jpeg2yuv -f 25 -j image%05d.jpg | mpeg2enc -o mpegfile.m1v" -- https://sourceforge.net/docman/display_doc.php?docid=3456&group_id=5776#Images

    Note that this documentation is wrong, and you also need to include an interlacing option...

    jpeg2yuv -f 25 -j frame-%05d.jpg -I p | mpeg2enc -o mpegfile.m1v

    The frames also have to count from 00000 in this example. I used the following little script to convert to the required naming format:

    count=0
    for item in `find . -type f`
    do 
      mv $item frame-`printf "%05d" $count`.jpg
      count=$(( $count + 1 ))
    done

    Note that the conversion is also quite slow, I walked away and came back later.



posted at: 00:00 | path: /jpeg2mpeg | permanent link to this entry
There are 3 comments on this post, and 94 comments which didn't survive moderation. 112 were blocked by trained gerbils. Click here to see them.