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.


0. Update: If you just want to convert from one 3D-format to another, you may now use the stereo3d filter in mplayer. E.g.
mplayer -vf stereo3d=sbsl:abl for side by side to top bottom.
You'll often want -vf stereo3d=sbsl:ab2l for half height. Anaglyph formats are also supported, check out the man page.

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;
done
or
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;
done

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
faster:

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.

Comments:
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. ;) )

6 comments:

  1. Hello,
    We have gone through your blog spots and found your posts informative. We would like to have the opportunity to have you writing some guest posts for us. We run a website named www.technograte.com. We currently don't have Linux writers at hand and hence intending to group up with writers.

    If you are interested then please let us know via email to
    admin@technograte.com
    --
    - Kind Regards
    Sameer Manas

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

    I put up my current wrapper script here:
    http://sourceforge.net/projects/videomerge3d/

    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.

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

    ReplyDelete
  4. 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?

    ReplyDelete
  5. hello. thank for your tips. i have one question. i have one 2D file (Normal video with .avi or .mp4 or mkv or anythink)
    Now i want convert it to SIDE by SIDE video with ffmpeg. Can you hlep me how to do that. Thank you
    Ex: https://www.youtube.com/watch?v=4Zooh30RXGM

    ReplyDelete
  6. now it is supported

    http://ffmpeg.org/ffmpeg-filters.html#stereo3d

    ReplyDelete

I appreciate comments. Feel free to write anything you wish. Selected comments and questions will be published.