July 31, 2011

Use Avisynth to batch enhance video clips.

 

More often than not, the video clips from your vacation turn out to have dull colors. You know the sky was blue on that wonderful day, but the video shows only a moderate grey-bluish color. You can't blame the camera. After all it's just a point-and-shot.

You can find a lot of tutorial online about how to make the sky bluer. However they mostly tell you how to do it one by one. With a few hundred clips from a single vacation, there's just too much trouble to repeat the process over and over. Don't even mention that if you have to change something a little bit, the whole process have to start over again.

In the search of automatic video enhancing, I discovered Avisynth. It uses it's own script language to handle video files. At first it seemed quite a daunting task: what? I have learn another computer language again? However, after spend some time explore it for a while, I gradually fall in love with its power and flexibility. It's more like a Swiss knife for video modification. Combined with two other also free programs, VirtualDub and Avisynth Batch Scripter, they enable me to do some amazing things much easier than before.

For example, my final script for my video clips does high quality de-interlacing, color enhancing, stabilizing, all in one pass. It took a long time to process all 400 clips my parents shot back in 2007, but it's just computer that's working hard, not me. Best of all, all the finished video clips have the same name and same length as the originals, so I can start video editing while they are being processed. After I am done with editing, I just over-write the old video with the new ones. ( One little thing though, because the finished ones are progressive instead of interlaced, I run a little script in Sony Vegas to change their attributes in one batch. This method of course should belongs to another blog.)

Ok, with so much ado, it's time to start the tutorial. In this tutorial I only show you how to enhance the color of the video. The purpose is to let you get familiar with how Avisynth, Avisynth Batch Scripter and VirtualDub work together. The rest is up to you to explore.

All the tools used here are free. You could donate something to those free projects. Each of them truly deserves something.

All the tools Tools require:


Avisynth's setup is straightforward, just click "next" all the way.

Virtualdub requires no setup, just extract the files to a folder, to make it simple, let's just extract the virtualdub files to this folder "C:\Virtualdub" and it's done.

Cedocida DV codec is also very easy to install. Just extract it to a folder, right-click on "cedocida.inf" then select "install".

Avisynth Batch Scripter should be installed last. There you need to delete everything in the "script template(.avt)" area, as seem on the image below.


Now click on "Settings", and specify the "scan folders", which is actually the original video clips located. In this example, I choose my video clips in the folder "D:\DV\2007-9 Parents to West Coast.

Create a new folder, in this example I name it "Video Scripts" and I choose to build scripts  in that folder, as shown below.



After the setting is done, you can start to write something in the script area. To make the sky bluer, I use an Avisynth build-in function called "Tweak". It works like magic.

Copy my code and paste them into that area:

AVISource("%PATH%\%CLIP%.%EXT%")
Tweak(sat=1.5)
Tweak(sat=0.7, startHue=90, endHue=180, maxSat=150, interp=32, dither=true) # Desaturate Red
The above code basically tell Avisynth the location of the video clip, increase the overall saturation, then decrease the saturation of the red. The reason I do it like this is that it is much more difficult to just enhance blue. Because the blue in video clips usually mixes with grey or white, if I just enhance blue, I will see edges all over the sky. On the other hand, red usually don't gradient with other colors, most of time it stands out in a scene. So most of the time the edges of red is not a problem. By de-saturating red, I make sure my video won't have a bloody crimson scene for the sunsets.

After that, click on "Scan", the program should find all the video clips inside the folder you selected, like below.


Please notice that the above picture has my old script of enhancing blue, which I found it unfit for some scenes. Of course you can use it if you found it works for you.

Now you are all ready for the batch script generation, click on "Build Scripts", and your new empty "Video Script" folder will be filled with scripts. Each script is corresponding to each video clip in your DV folder, as seem below.


Now you can close Avsynth Batch Scripter and go to "C:\Virtualdub" folder and run Virtualdub.exe
Click on "Video --> Compression..." then highlight "Cedocida DV Codec(SD format) v0.2.2", or any other video compression your want, then click on "OK".


Click on "Audio", you should see that "Direct Stream Copy" is checked.

Before the next step, create a folder to hold the output video clips. In this example, I create "D:\Out" as the output folder.

Click on "File-->Job Control..." or simple press F4. The job control windows should show up.


In "VirtualDub Job Control" windows, click on "Edit --> Process Directory..."
It will ask your source directory, which is the one with the generated scripts, in this example, is "D:\Video Scripts". The it will ask for the target folder, which is "D:\Out". You will see a list of jobs being created like below.


Now all is ready, you just simply click on "Start". Virtualdub will start processing each of your video, make the blue bluer, the green greener and save them to "D:\Out" folder. Don't wait for the whole thing to finish, whenever the first job is done, open it and see whether it is fine or not. If the video is ok, go out, have a cup of coffee or something.

(Because this is an re-compressing process, video degrade is certain. So I recommend you that you put as much modification you want as possible. You really don't want to do it more than once.)

This is just a tiny example of how Avisynth can do magic to your video clips. My finish video scripts are a little more complicated than that. If you are interested, you can try the following too.

AVISource("%PATH%\%CLIP%.%EXT%")
SmoothTweak(saturation=1.5, useMT=0, useOPT=1)
Tweak(sat=0.7, startHue=90, endHue=180, maxSat=150, interp=32, dither=true) # Desaturate Red

ConvertToYV12()

i=QTGMC(Preset="Slow")

FilmsYLevels(i, 1.5)

Function FilmsYlevels(clip clp, float amp)
       { wicked="x x 16 - 34.85493 / sin "+string(amp)+" * -"
         return( clp.mt_lut(Yexpr = wicked) ) 
         }

v=MSuper().MAnalyse()
mdata = MDepan (v , pixaspect=0.9091)
DePanStabilize( data=mdata, pixaspect=0.9091, dxmax=10, dymax=60, initzoom=1.1, mirror=15)

SelectEven()

 The above script will improve the color, convert the video clips to progressive in high quality, adjust the brightness level of the dark, then stabilize the shaking clips, all in one process. However, you need to install many plugins to Avsynth in order to run this script. I leave them to you to figure it out. Good Luck!

No comments:

Post a Comment