View Full Version : [Other] Avisynth for total newbies - extended edition as case study

01-12-2010, 07:14 PM
How I made an extended edition in Avisynth (and lived to tell the tale): A tutorial using only freeware/open source tools without sacrificing quality

What I did:
Made an extended edition with video and audio editing with Avisynth, Virtualdub and Audacity, and converted to DVD with Aften and HC and DVD styler. (Only freeware)

I rarely care for extended editions. Less usually is more. But then one day I watched the deleted scenes for one of my favourite films, Martin Scorsese's wonderful After Hours. They were uniformly excellent, adding even more infuriatingly funny plot twists to the whole. A couple of them lacked crucial starting frames (meaning that including them would have taken you out of the experience), or had other problems like lacking music or being dependent on other scenes that had been cut and not included on the DVD. But four of them could be included. And they were great.

So, where to begin? I don't even have iMovie anymore, and I'm broke, so I decided to learn Avisynth, the scriping language from hell, movie editing for programmers (which I'm not by a long shot). There were two reasons I wanted to do this: (1) I like free and open software, and (2) Avisynth has some amazingly powerful functions and plugins (like the two best resizing algorithms currently available), and is currently the only way to edit videowhile not sacrificing quality. I got VirtualBox and an old XP CD out, and got ready.

1) Ripping.
Tools used: DVD fab decrypter.
I started with the only closed source application in this guide - DVD Fab Decrypter. I ripped titles only: main film first, deleted scenes (they were one title) second. I chose to omit unnecessary audio tracks (the film only has a mono English soundtrack). You get folders with VOB files.

2) Index the MPEG2 video for editing.
Tools used: DGIndex
For MPEG2 (DVD-video) to be readable in Avisynth I need to demux and index the files. This is done with DGindex, an open source application. Load the file into the app and take it from there. The main film shows up with a squeezed image (too slim characters on screen) and no black bars, this is good since it shows that the video is anamorphic. Choose demux all. It will output a small .d2v file (dependent on the vob files, so don't delete them) and a larger .ac3 file for the audio (non-dependent).

Next I load up my deleted files. Here I run into my first problem. I notice that the deleted scenes are letterboxed, not anamorphic. After cursing the DVD authoring company - these scenes look beautiful and they were obviously scanned recently (in the noughties, probably) for the sole purpose of being released on DVD and they couldn't even author them anamorphic?? - I decide to crop the black bars in DGindex now to save me the extra Avisynth work later on. Choose demux all. After Hours only has a mono track, and so I'm set for the time being.

3) Preparing the sources for Non-linear editing using Avisynth
Tools used: Avisynth (with DGdecode.dll and Nicaudio.dll), AvsP, Audacity (if necessary)
Now I'm pretty sure this is not a necessary a separate step. However, to avoid an Avisynth script long as your arm under point 5 (much longer than your arm, on second thought) I like to convert the MPEG2/AC3 sources to Lagarith lossless AVI with WAV audio in one script, and make another for the actual editing. It will make the non-linear editing process A LOT simpler to avoid having fifty lines of video conversion script gibberish in the beginning, especially if the sources differ (which these do).

First of all, install Avisynth. Next: copy-paste the DGdecode.dll file from the DGindex folder to the Avisynth plugins folder (should be in program files\avisynth). After that: download the NICAudio Avisynth plugin, and place the .dll in the Avisynth plugins folder. Lastly, install AvsP, the best script editor for Avisynth. Now you have an Avisynth setup that can decode MPEG2 with AC3 audio, with a script editor/error checker.

Next we're going to make the whole thing one file (with the deleted scenes coming after the credits), and in the process smoothing out the differences in quality. Open AvsP. This might take a couple of seconds, as this will start Avisynth in the background, with all plugins from the plugins folder.

Now you have a text window. This is where the fun begins. Choose Insert source from the edit menu and insert the d2v file. Next, Insert source again, view «all files» and insert the AC3 file. Change the prefix from «DirectShowSource» to «Nicac3Source». Do the same thing for the deleted scenes. Add prefixes for the sources as per this example script (#-lines are comment/notes lines that are not a part of the script:

###script begins
##main film
#mount video source
video1=MPEG2Source("PATH\after_hours\mainfilm.d2v", cpu=0)

#mount main audio source
audio1=Nicac3Source("PATH\after_hours\mainfilm T80 1_0ch 192Kbps DELAY -8ms.ac3")

#the function audiodub dubs video and audio together. Use «clip1=» to define this as the first part of a larger whole

##deleted scenes

#mount video
video2=MPEG2Source("PATH\after_hours\delscenes_vob\delscenes_crop_3.d2 v", cpu=0)

#mount audio
audio2=Nicac3Source("PATH\after_hours\delscenes_vob\delscenes_crop_3 T80 2_0ch 192Kbps DELAY 0ms.ac3")

#second audiodub

##Splice the whole thing together. «Alignedsplice» aligns clips after one another while preserving sync.
AlignedSplice(clip1,clip2)Now press the bottom left Preview button. You will get a very discouraging error message, prompting you to - if you're like me - give up editing for at least a week. However, the answer is simple. Check the desired aspect ratio, i.e. the ratio of the main clip. In my case this is standard PAL anamorphic 720x576. This means this is the target size for the deleted scenes. Replace the second video line with this one:

video2=MPEG2Source("PATH\after_hours\delscenes_vob\delscenes_crop_3.d2 v", cpu=0).Lanczos4Resize(720,576)This will resize the deleted scenes clip to match the main film. Lanczos4resize is so good, I can honestly not tell the quality difference in this case, even though I really should. (This can be used to crop and zoom in other cases too.)

Now I try again, and I get another discouraging error message, this time concerning number of audio channels. Obviously, the above-mentioned geniuses who authored the DVD decided to encode the mono track as stereo - only for the deleted scenes. Arrrgh. There are more than one ways to do this. If you convert the AC3 files to WAV, you can fold the stereo to mono with open source audio editor Audacity (export as mono WAV). This is a good chance to check if the volume is consistent too, since Audacity features a really simple gain/volume adjustment tool. (There are ways to get one channel in Avisynth out of two channels, but you need to use some arcane commands that are beyond the scope of this guide.) Fix it with Audacity and in Avisynth replace «Nicac3Source» with «WAVSource» and the new files and you're good to go. Check with preview. If it worked, save the script. (If not, find the error.)

4) Converting to AVI/Lagarith lossless
Tools used: VirtualDub (with AC3 and MPEG2 plugins), Lagarith lossless installer
The best tool for checking, converting etc. video and audio is VirtualDub. Download the latest package. Find the MPEG2 (and the AC3 if you didn't convert to WAV) plugins for VDub by googling. Make a folder called plugins32 in the VirtualDub folder, and put the DLLs there. Next, install the Lagarith lossless codec. Now open VirtualDub and all plugins should load. Open the avs file you saved. If it didn't work, you probably did something wrong with the plugins.

Check that the main video and the deleted scenes look consistent. If not, you have more work to do. (Colour correction, perhaps? There are avisynth colour correction guides online; do a search.) Choose direct stream copy if you converted to WAV under audio conversion settings (convert to WAV if not), and Lagarith under video. Choose to export to AVI. This will take a long time.

5) Non-linear editing.
Tools used: VirtualDub, Avisynth, AvsP
This is where the real fun begins. Not even open source fanatics would claim that Avisynth is a good non-linear editor. However, it gets the job done extremely well if you invest a little effort.

Open the AVI file in VirtualDub. Note down (on paper) where each transition begins and ends. For instance, my first deleted scene comes after frame 60346. Open AvsP. Then I use a dot and then the Trim command with the start and end frame in a paranthesis. Clip2 is the deleted scene and it's situated near the end of the lossless AVI file. Again to/from frame numbers do the job. Then after the deleted scene, the film follows with frame 60347 and up to the next deleted scene is due to be inserted. My next deleted scene illustrates some of the things you can do with Avisynth; I need audio from one scene and video from another. This can be accomplished with the Killvideo and Killaudio commands. Note that the frame count must be identical.

This produces a script like this:

#each clip is an individual scene




#where 119300 is the last frame of the end credits

AlignedSplice(clip1,clip2,clip3,clip4,clip5)And you're done!

6) Polish up your WAV
Tools used: VirtualDub, Audacity
Open the avs file in VDub again. This time you choose Save WAV. This will export only the audio part of the project. Polishing up the audio can be done with your sequencer/audio editor of choice. I like Audacity, since it's simple and powerful and I know the interface. You may like something else. Just be sure that you keep the original as the first track as a guide right until the very end, or you will screw up the sync.

Check the transitions. You may need crossfades or added music. You may also have to use music or sound that you cut earlier with Avisynth. You'll know what you need. I actually like to edit without video, since it's even easier to tell when something sounds wonky that way.

7) Convert the soundtrack to AC3
Tools used: Wav to AC3 Encoder (an Aften frontend)
The best tool for this is called, unsurprisingly, «Wav to AC3 Encoder» and is an implementation/frontend/GUI of the best freeware AC3 conversion algorithm, Aften.

8) Convert the video to MPEG2 with HCEnc/HCGUI
Tools used: HCGUI
Now I know most people swear by CCE. However, an increasing amount of geeks are coming to the conclusion that HC is more than good enough, and in many cases better. I trust them, as I think my results were great.

Open HCGUI. Load the latest avs file. Type in the target size of the project. You need some room for the AC3 file and for the menu. Calculate this the best you can. Save some megabytes as headroom for the DVD. Choose «best» and press the «make compliant» button. Make sure it's the same settings you used. Make chapters if you want. This is basically all unless you know what a preset matrix really does. Press encode. This will take a while.

9) Make DVD menu.
Tools used: DVD Styler
I use DVD styler since it's powerful and free, but I don't like it all that much. Use the one you like best that supports m2v+ac3 input. Just make sure you don't re-encode the video. The video/audio should be done and DVD-video compliant.

10) That was about it!
I hope you found some useful tips here. I imagine the video file preparation section would be handy for people wanting to use the Lanczos4resize command but have little scripting knowledge.

I hope it's newbie-friendly enough to make more people use Avisynth. It's daunting, but far from impossible. And Google is your friend, as always.

And I hope that the Avisynth-savvy will add to this guide, and suggest improvements! :)

PS: My edit turned out good, but the sound is kinda wonky in places. The sound editing was basically impossible with several things happening in the mono mix at once - and the deleted scenes were not scored. Also, there is no released soundtrack LP release with Howard Shore's music that I know of. I won't share it here in its present state.

Captain Khajiit
01-12-2010, 07:55 PM
This guide seems great. I'll not be brave enough to give it a go for a long while yet, if ever, but I am really impressed that you persevered with Avisynth. I took one look and ran! :smile:

Now I know most people swear by CCE. However, an increasing amount of geeks are coming to the conclusion that HC is more than good enough, and in many cases better. I trust them, as I think my results were great.

In many cases better? :shock: It's extremely interesting if a free encoder can out-perform CCE. Can you post any links or explain further? I'm intrigued.

01-12-2010, 07:57 PM
Thank you.

01-12-2010, 08:56 PM
I didn't know about HC until very recently - when I fixed the AR problem on The Mist Novella Cut.

HC and a couple of other encoders come bundled with DVD-Rebuilder and didn't know which was best.
After some googling, the consensus seemed to be that HC is on par with CCE.

The best free encoder available - especially at low bitrates.
The only negative is that it is slow but who cares... as long as it does a good job.

I did end up using CCE anyway but thought I would throw in my 2 cents ;)

edit: About the guide itself... so you're converting from mpeg2 to avi and then back to mpeg2 again?
I realize that you do lossless when converting to avi, but it seems like a lot of converting back and forth.

It should be possible to use avisynth without converting to avi, since DVD Rebuilder uses avisynth scripting to convert DVDs, or in my case, just fix the aspect ratio (using avisynth script)

edit 2: HEY! Am I the only one to realize that MPC can preview avs files!? I just realized this while installing a program that installs avisynth and there's an option to associate avs files with Media Player :oops:

Captain Khajiit
01-13-2010, 10:16 AM
After some googling, the consensus seemed to be that HC is on par with CCE. The best free encoder available - especially at low bitrates. The only negative is that it is slow but who cares... as long as it does a good job. I did end up using CCE anyway but thought I would throw in my 2 cents ;)

I for one appreciate your two cents. I don't care about speed either. Thanks!

The Faceless One
01-13-2010, 10:44 AM
I'm a big fan of Avisynth and use it to do most of my editing.

You guide is good but as Zepplinrox said there is no need to re-encode to Lossless Avi in the middle, it's possible to combine your 2 scripts into one which has a couple of advantages, firstly it means you can watch your completed edit (in Virtualdub or MPC) before spending time doing any re-encoding secondly when you do your final encode it will be more efficient due to MPEG having much lower bitrates than lossless Avi.

Here's a quick attempt at refining you code, it should also fix the mono/stereo audio problem by using the channels parameter when importing the audio, I also added an audio delay on clip1 to account for the -8ms reported in the file name (this script will not actually work because I had to guess the frame count of original clips):

###script begins
##main film
#mount video source
video1=MPEG2Source("PATH\after_hours\mainfilm.d2v", cpu=0)

#mount main audio source
audio1=Nicac3Source("PATH\after_hours\mainfilm T80 1_0ch 192Kbps DELAY -8ms.ac3",1)

#the function audiodub dubs video and audio together. Use «clip1=» to define this as the first part of a larger whole

##deleted scenes

#mount video
video2=MPEG2Source("PATH\after_hours\delscenes_vob\delscenes_crop_3.d2 v", cpu=0).Lanczos4Resize(720,576)

#mount audio
audio2=Nicac3Source("PATH\after_hours\delscenes_vob\delscenes_crop_3 T80 2_0ch 192Kbps DELAY 0ms.ac3",1)

#second audiodub



AlignedSplice(clip1.Trim(0,60346),scene1,clip1.Tri m(60347,68434),scene2,clip1.Trim(68435,119300))

01-14-2010, 08:48 AM
Am I the only one to realize that MPC can preview avs files!?

Nope. :)

It was actually in the guide until the last minute, but I cut it since the VirtualDub portion covered previewing. I wanted to keep this short and sweet. But I agree, Media Player Classic is very cool and handy, indeed. Also, it's possible to set up AvsP to fire up the open script in MPC with the click of the F6 button. Very cool. It's in the settings.

01-14-2010, 09:37 AM
Thanks for the nice and helpful posts.

Especially thanks the Faceless One for helping us all out with big script improvements. That script was a lot simpler than I thought it'd be, and you're all right: it's pointless with an extra encoding step, especially when the resulting script is so short and, well, comprehensible. The "scene" marker is very helpful in this case.

As you can all easily glean from the guide, I'm no authority on this AT ALL, and I'm grateful for your help in making the guide better. I'll update the script with the relevant comments later on.

Still, part of the raison d'être of this guide was helping out non-Avisynth users with quality resizing (Lanczos4resize) for use with GUI non-linear editors who are happy with converting to Lagarith for editing. So I'll keep that part too.

07-03-2010, 11:45 AM
Thanks for this guide. I've been around this community for a little bit here and there. But this time, I'm actually starting out putting together a fan edit now. And I have no $ to speak of, so i was looking to freeware. This helps knowing that it can be done (may be a little difficult) but still possible. Thanks again.

07-20-2010, 07:17 PM
I think I now would use MeGUI for step 7. The WAV to AC3 Encoder Aften frontend seems a bit redundant since MeGUI does the exact same thing. MeGUI is also a great tool to encode the video and audio to something else than MPEG2 for workprint purposes (such as anamorphic Xvid in MKV container, a nice and quick way of getting acceptable results for test versions). If you want to release your edit as Xvid or x264 as a finished product, it's also a good idea to encode directly from source using MeGUI, not using the finished DVD.