For When You Can't Have The Real Thing
[ start | index | login ]
start > Videos From Images

Videos From Images

Created by dave. Last edited by dave, 15 years and 133 days ago. Viewed 3,359 times. #7
[diff] [history] [edit] [rdf]

Creating movies from a collection of images

Get: mencoder (it is a part of mplayer; rpmforge users will find it in a separate .rpm).

$ mencoder -mf fps=30 "mf://*.jpg" -ovc lavc -lavcopts vcodec=mpeg4:vhq:vbitrate=1500 -ffourcc DIVX -o out.avi

...will turn all the .jpg files in the current directory into a DIVX AVI video file (possibly pre-supposing that they are all 640x480 jpg files...)

A Bit More Automated

This is run every night at the end of the night. It makes a video from the day's pictures.

Y=`date +%Y`
M=`date +%m`
D=`date +%d`
if [ ! -d $SRC ] ; then
        echo "$SRC not mounted"
        exit 1;
cd $SRC
# If run out of cron, it is 23:55 or so.  We invoke it as $0 -
# So wait ten minutes for the day to roll over midnight.
if [ ! -z "$1" ] ; then
  sleep 600
# This perl script sorts the pictures
# encode
time mencoder -mf fps=30 "mf://$Y/$M/$D/*.jpg" -msglevel all=0 -ovc lavc -lavcopts vcodec=mpeg4:vhq:vbitrate=1500 -ffourcc DIVX -o /mnt/lobby_cam/video/$Y-$M-$D.avi
# show how big it is
ls -lh /mnt/lobby_cam/video/$Y-$M-$D.avi

Previous Degeneration

Get: mjpeg tools.

This is what I'm doing at the moment. It wants a supply of 640x480 .jpg files, and in return you get a nice DVD-quality (depending on your camera quality) video that Windows generally can't play unless a software DVD player is installed on it.

cd /output/location
if [ ! -z "$1" ] ; then
if [ ! -z "$2" ] ; then
find $SOURCE | grep $SPEC | sort > $OUTPUT.lst
COUNT=`wc -l $OUTPUT.lst | awk '{print $1}'`
echo $COUNT frames
time cat $OUTPUT.lst | jpeg2yuv -v 0 -f 30 -I p -n $COUNT | mpeg2enc -v 0 -f 9 -l high -o $OUTPUT.mpg
rm $OUTPUT.lst
ls -lh $OUTPUT.mpg

By default it takes all the images in $SOURCE and puts them in a video called output.mpg.

The first parameter is the sequence of characters to match. My pictures are all called cam1-YYYYMMDD… so I invoke like this:

$ makempg 20081010
4674 frames
   INFO: [jpeg2yuv] Reading jpeg filenames from stdin.

real 2m5.857s user 3m10.040s sys 0m14.230s -rw-r--r-- 1 dave users 57M Oct 11 00:08 20081010.mpg

Information that might prove useful in the future

<hobbs> mencoder -mf fps=30 mf://*.jpg -ovc lavc -lavcopts vcodec=mpeg4:vhq:vbitrate=1500 -ffourcc DIVX -o out.avi
<hobbs> adjust various stuff, maybe. :)
<hobbs> (AVI with MPEG-4 video in it is Windows Media Player friendly.
<hobbs> sadly MJPEG is not or you could just use that ;)
<xdroop> thanks, I'll use that as a starting point.
<hobbs> if you want to add a soundtrack that's fairly easy as well with -audiofile
<hobbs> (plus some -oac options, but if the audio file is mp3 and of a reasonable bitrate, you can just use -oac copy)
<hobbs> you can turn the quality up if you like. There's no reason you shouldn't be able to get the same or better
<hobbs> try replacing vbitrate=whatever with vqscale=2 -- and then if that's too big, vqscale=3
<hobbs> you can use keyint=1 in lavcopts to get pure-intra coding, that'll blow your bitrate way up but you won't get any motion artifacts at all
<xdroop> well for the most part it's only noticable at the beginning when most of the frames are shooting dark and this camera is crap in the dark.
<hobbs> but I doubt your mpeg2enc was going that far
<hobbs> anyway try vqscale in place of vbitrate. That's for "constant quality" mode
<hobbs> and you can try a lower -keyint if you notice the artifact where every few seconds the video "snaps" into sharpness because it hit an I-frame
<hobbs> the default is 250 which is very high compared to the MPEG-2 defaults (which would give you either 12 or 15)

Older, misleading information

# ls $SPEC.jpg | jpeg2yuv -f 25 -I p | mpeg2enc -M 3 -o $OUTPUT.mpg
  • there are limits on the value of -f. 24 or 25 appear to be the best values.
  • -M is only useful for systems with more than one cpu or core. The notes say that -M 3 can produce more in throughput, even though the threads are less efficient than they would be at, say -M 2. This is counter-intuitive to me, but whatever.
  • the above method gets lost in space somehow; this method, while clunkier, works. For each picture, we make a three-frame mpeg of the same picture, then we cat them all together into the final output. I use the "cat pipe into the for loop" because for one example of the time I did this I had over four thousand images, and the command line length got exceeded.
$ mkdir output
$ ls $SPEC.jpg > input.lst
$ cat input.lst | for i in `cat` ; do jpeg2yuv -f 25 -I p -n 3 -j $i | mpeg2enc  -o output/$i.mpg ; done
$ cd output
$ ls *mpg > input.lst 
$ cat input.lst | for i in `cat` ; do cat $i >> output.mpg ; done
no comments | post comment
This is a collection of techical information, much of it learned the hard way. Consider it a lab book or a /info directory. I doubt much of it will be of use to anyone else.

Useful: | Copyright 2000-2002 Matthias L. Jugel and Stephan J. Schmidt