November 08, 2012

Convert a H-SBS video to ColorCode3D using free tools.

In the last tutorial I talked about how to convert a 3D SBS video to ColorCode3D video. It turned out that it produced not true ColorCode, only Yellow/Blue anaglyphic stereo videos. ColorCode3D is a proprietary  way to present 3D in a more colorful and vivid way. It was based on Yellow/Blue anaglyphic arithmetic but much improved. I thought there were no way to make ColorCode3D video with free tools, but I was wrong. Recently I found a way to do it with a free AviSynth plugin.

AviSynth is a powerful tool to manipulate the video any way you want. It has a lot of plugins to extend its already powerful features. There is a 3D Toolbox here which let you do the job already, but I found it too difficult to understand. So in this tutorial I will try to simplify the whole process.


The movie sample I used is the SpiderMan 3D trailer from YouTube. If you are a smart guy, you can find some tool to download the 1080p mp4 video to your hard drive. It comes as a HSBS video, 79mb. If you don't have it, just use any of those short MP4 or MKV 1080p videos. For simplicity's sake, I named this file "spiderman.mp4" under "d:\video" folder.


Now you need to download some tools:
  • Avisynth of course. Get the 32bit one.
  • MediaCoder to convert the MP4 or MKV from HSBS to ColorCode3D anaglyphy, resulting with a MKV file with an AVC video stream. Again, get the 32bit program, not the 64bit one.
  • Anaglyph conversion plugin for AviSynth. This is the plugin that makes the whole thing possible.
  • FFMpegSource I am sick and tired of Avisynth's poor support of media types. With this plugin I can use any MP4 or MKV video as the source. No more headaches.
  • MKVtoolnix for the nice old mkvMerge GUI tool. It stitches everything back to a proper MKV. If you need a MP4, just use Avidemux to re-mux the MKV file. We only change the video here, so the audio is completely untouched.
First install Avisynth, MKVtoolnix and MediaCoder. The first two should be straightforward. MediaCoder has a wizard interface. You just ignore it and the real program should come out.
If you see a different interface, click on Options->User Interface Mode->Normal Mode.
Click on File->Settings or press Ctrl-S. You will see a huge settings collection, as shown.
Extend Video Sources->AVISynth, you will see their AVISynth plugin Path, as shown:
Noted the plugin path, that's where you are going to put the 2 plugins. In this case, it's
"C:\Program Files (x86)\MediaCoder\codecs"

Extract the Anaglyph.dll from AviSynth_Anaglyph_plugin_V1.0.zip, which you downloaded from here, and put the file to that codecs folder.
Extract the  ffms2.dll from ffms-2.17.7z, which you downloaded from here, and put the file to that folder too.
So you will end up with the codec folder like this:
Now you are almost ready. Head straight to the D:\Video folder which I put my previously downloaded spiderman trailer. As you can see here:
Right Click on the white-space and choose New->Avisynth Script, and rename the file to "spiderman colorcode.avs", as shown
Double-click on "spiderman colorcode.avs". A notepad should open. Now copy and paste the following in it:

LoadPlugin("Anaglyph.dll")
LoadPlugin("ffms2.dll")

clip = FFVideoSource("D:\Video\spiderman.mp4", colorspace="RGB24")

type = "blue-amber"
matrix = "colorcode3d"

w=clip.Width
h=clip.Height

left = clip.BilinearResize(w, h, 0, 0, (w/2)-1, 0)
left.Levels(0, 1.25, 255, 0, 255)

right = clip.BilinearResize(w, h, w/2, 0, 0, 0)
right.Levels(0, 0.8, 255, 0, 255)

return Anaglyph( left, right, matrix, type, 0)

Note: the colorcode matrix is good, but the retina contest effect is too much. So I used the Level function to brighten the left and dim the right, so the contest effect is reduce and the video will be brighter to compensate the loss of light through eyeglasses. If you don't like the change, remove the two lines with "Levels" in them.

Now open MediaCoder and add the "spiderman colorcode.avs" to it. Note: it might take a short while to import because it will create an index file called  "spiderman.mp4.ffindex" first. For longer movies, you should give it a minute or two.

Right next to "Preview", you need to click on "Video" to check the settings, as following:
Noted that you can set your own video quality, method or bit-rate in it. I don't recommend using GPU to do the job but it's totally up to you.

Then check "Audio", the "Enable" should be unchecked.
Click on "Container" tab, it should be "MKV" for this tutorial. Of course you can choose any other one you like.
Also check on the "Picture" tab, make sure you uncheck the "Resize" and "Frame Rate". I recommend using the RGB24 color space.

Now you are truly ready, click on "Start" button and get the video rendering started. Even my i7-2600 can only encode it at the speed about 10 fps. So you have to wait for a long time.

After that you will get the file "spiderman colorcode.mkv", which contains only 1 video stream. You can mux this video stream with the orignal audio by using "mkvMerge Gui" from mkvtoolnix.
The result file, "spiderman colorcode (1).mkv" contains the new colorcoded video and original audio. All the frame should be perfectly in sync. Enjoy!

Optional: if you want MP4 files instead of MKV files, use AviDemux to do the job. It will simply copy the Video and Audio and create the file, no re-encoding required, as shown.


The final video is posted in YouTube.



P.S.  Naturally VirtualDub should be used with Avisynth, which is much simpler than using MediaCoder, but somehow the H264 video stream generated by VirtualDub is not in-sync with audio. With MediaCoder you might met some strange errors like error 7 or error 9, which are hard to figure out why, but once it works, it always works, and all the video and audio are synced. I heard that another encoder "Super" also fully support Avisynth. So if MediaCoder doesn't work for you, you can try "Super" in the same way.

A free 3D player called Bino can play SBS files with optimized (Dubois) anaglyph method. If you use computer to watch 3D movies. I highly recommend using this program. No need for time-consuming transcoding. The latest KMPlayer also support full-color or half-color stereo anaglyph viewing of video or photos. Their half-color method is adequate for casual 3D viewing. I hope they will add the Dubois method soon.

The free Stereo Movie Maker can render full-color or half-color H264 video streams too. However, in order to use it, you need to mux the MKV into AVI first, then after the transcoding you have to mux it twice to make it a normal MKV. Too many hoops to jump. So I'd rather using MediaCoder to do the job, and MediaCoder is proved to be faster and more responsive than the old Stereo Movie Maker.

If AviDemux crashes whenever you put your MKV or MP4 in it, then you need to install x264vfw to fix it.

If your media player cannot play the generated MKV files, it may be the "header stripping" problem from mkvMerge GUI. You need to open mkvMerge GUI, click on File->Options and check the "Disable header removal compression for audio and video tracks by default". Then you re-mux the MKV file again.