Create Side by Side or Top-Bottom 3D Video With ffmpeg and Imagemagick or Mencoder by Converting Alternate-Frame Sequencing 3D Video

These are some commands for top-bottom or side-by-side 3D video transcoding with mencoder or ffmpeg and imagemagick.

1. Extract the mjpegs to single jpeg files:
ffmpeg -i Some_mjpeg_file.mjpeg -vcodec copy %09d.jpg
This creates 2020 images for my short film. If you have another format, omit vodec copy. If you want to encode without loss, use .png as file type.

2. Extract the audio:
ffmpeg -i [any audio file readable by ffmpeg] -ab 384k Sound.ac3
Of course you could also use aac or vorbis.

3. A. Create side-by-side 1080p images:
for (( i=1 ; i < 2020 ; i=$i+2 )) ; do
montage $(printf "%09d" $i).jpg  $(printf "%09d" $(($i+1))).jpg -geometry '960x1080!+0+0' combined/$( printf "%04d" $(( ($i-1)/2 )) ).jpg;
3. B. Create top-bottom 1080p images:

for (( i=1 ; i < 2020 ; i=$i+2 )) ; do
convert -append $(printf "%09d" $i).jpg $(printf "%09d" $(($i+1))).jpg -geometry '1920x540!' -mosaic combined/$( printf "%04d" $(( ($i-1)/2 )) ).jpg;

You can either do
convert -append top.jpg bottom.jpg -geometry '1920x540!' -mosaic top_bottom.jpg or
convert -page +0+0 top.jpg bottom.jpg -geometry '1920x540!' -mosaic top_bottom.jpg
I don't know if there's a speed difference.

4. Encode them into a video:
ffmpeg -f image2 -i combined2/%04d.jpg -i Sound.ac3 -acodec copy -vcodec copy -r 24 test.avi

3.+4. A. Mencoder Top-Bottom:
mencoder mf://*.jpg -vf tile=1:2:2:0:0,scale=1920:1080 -fps 48 -ofps 24 -aspect 16:9 -o top-bottom.avi -ovc x264 -x264encopts crf=23 -audiofile Sound.ac3
This should be faster
mencoder mf://*.jpg -vf scale=1920:540,tile=1:2:2:0:0 -fps 48 -ofps 24 -aspect 16:9 -o top-bottom.avi -ovc x264 -x264encopts crf=23 -audiofile Sound.ac3

3.+4. B. Mencoder Left-Right:
mencoder mf://*.jpg -vf tile=2:1:2:0:0,scale=1920:1080 -fps 48 -ofps 24 -aspect 16:9 -o left-right.avi -ovc x264 -x264encopts crf=23 -audiofile Sound.ac3

mencoder mf://*.jpg -vf ,scale=960:1080,tile=2:1:2:0:0 -fps 48 -ofps 24 -aspect 16:9 -o left-right.avi -ovc x264 -x264encopts crf=23 -audiofile Sound.ac3

Interlaced to Top-Bottom and Left-Right
This can be achieved with mencoder -vf ilpack,il=d (top-bottom)
and mencoder -vf ilpack,fil=d (left-right).
Of course Interlaced to top-bottom makes much more sense, as otherwise you may lose a lot of pixels.

I wish ffmpeg would support this out of the box, which I'm sure would be *much* faster, as ffmpeg usually is greatly optimized for speed. The encode with ffmpeg to x264 video from mjpeg is actually at least 10 times faster than the merge operation with imagemagick. I bet you can make this run much faster by scaling the images with ffmpeg while extracting them. If you're lucky, encoding 3d side by side with mencoder will work for you and you can just use mplayer, which is probably much, much faster. The link also has some more command examples if you like. Enjoy!

If you run into troubles with mencoder, try ffmpeg + imagemagick, they're much better at supporting any time of file, but they will cost you more time.

If you like this post, share it and subscribe to the RSS feed so you don't miss the next one. In any case, check the related posts section below. (Because maybe I'm just having a really bad day and normally I write much more interesting articles about theses subjects! Or maybe you'll only understand what I meant here once you've read all my other posts on the topic. ;) )


  1. gstreamer can throw together sidebyside ( and probably top bottom ) 3d videos in near real-time.

    I put up my current wrapper script here:

    I suggest using the -720p -mp3 options.

    It currently uses both files' audio to create stereo which can be a bit odd if they are slightly out of synch.

  2. I would like to do the reverse of this process- that is put two streams of video into alternate frame format. Any ideas?

  3. Hey, you are doing this from one single video file. Is it possible to do it from two video files (filmed with two cameras), to achieve true 3D?


I appreciate comments. And I do read them.