Log in

View Full Version : DV to MP4 using command line only


raymod2
22nd December 2005, 07:05
This article describes my method to convert raw digital video into an MP4 file. It strictly uses command line utilities. I offer it up to invite comments and solicit suggestions for improvement. Also I thought it might give others a starting point if they have never done this before.

First you will need to collect and install the following tools:


AviSynth 2.5.6a
---------------
link: http://www.avisynth.org

EEDI2 v0.9
----------
compiled by: tritical
thread: http://forum.doom9.org/showthread.php?t=82264
link: http://bengal.missouri.edu/~kes25c/EEDI2v09.zip

TDeint v1.0 RC2
---------------
compiled by: tritical
thread: http://forum.doom9.org/showthread.php?t=82264
link: http://bengal.missouri.edu/~kes25c/TDeintv1RC2.zip

x264 CLI encoder v389
---------------------
compiled by: Sharktooth
thread: http://forum.doom9.org/showthread.php?t=89979
link: http://files.x264.nl/Sharktooth/force.php?file=./x264-Full_r389.exe

avs2wav v1.0
------------
compiled by: kassandro
thread: http://forum.doom9.org/showthread.php?t=70882
link: http://home.pages.at/kassandro/avs2wav.rar

FAAC v1.24.1
------------
thread: http://forum.doom9.org/showthread.php?t=68300
link: http://rarewares.org/aac.html

MP4Box v0.4.1-DEV
-----------------
compiled by: Celtic Druid
thread: http://forum.doom9.org/showthread.php?t=94874
link: http://www.aziendeassociate.it/cd.asp?dir=/gpac/dev

Haali Media Splitter
--------------------
thread: http://forum.doom9.org/showthread.php?t=89979
link: http://haali.cs.msu.ru/mkv/

ffdshow-20051129
----------------
compiled by: Milan
thread: http://forum.doom9.org/showthread.php?t=89979
link: http://ffdshow.sourceforge.net/tikiwiki/tiki-index.php?page=Getting+ffdshow
notes: when installing check only H.264,X264 video codec and AAC audio codec;
after installing go to the audio decoder configuration and select realaac
for the AAC decoder (libfaad2 results in garbled sound for me)


Next you need to create an AviSynth script. It should look something like this:


DirectShowSource("C:\Documents and Settings\Dan\My Documents\Adobe\Premiere Pro\1.5\distance.avi")
#AssumeBFF().SeparateFields() # use this to check if the video is BFF or TFF
#return last

mark = trim(849,1491)
jay1 = trim(3855,4481)
jay2 = trim(9331,10191)
last = mark + jay1 + jay2

# Deinterlace
LoadPlugin("C:\Program Files\AviSynth 2.5\plugins\EEDI2.dll")
LoadPlugin("C:\Program Files\AviSynth 2.5\plugins\TDeint.dll")
interp = separatefields().selecteven().EEDI2(field=0) # BFF
TDeint(order=0,field=0,edeint=interp) # BFF
#interp = separatefields().selecteven().EEDI2(field=1) # TFF
#tdeint(order=1,field=1,edeint=interp) # TFF

# Resize to square pixels
# FS source multiply horz by 10/11 (720x480 -> 656x480)
# WS source multiply vert by 33/40 (720x480 -> 720x400)
LanczosResize(720,400)

ConvertToYV12() # x264 only accepts YV12 colorspace


The first line loads your raw DV clip.

The next two lines are used to determine if your video is "top field first" (TFF) or "bottom field first" (BFF). Uncomment these lines and load the script into VirtualDub. If it plays smoothly it is BFF. If it stutters (moving objects jump back and forth) then it is TFF. When you are done make sure you comment these lines out again.

The next four lines select the parts of the clip that you want to include in your final output. You will need to view the raw DV clip in a video editor to determine the start and end frames of the ranges you want. I use VirtualDub for this. Uncomment "return last" and load the script into VirtualDub.

The next block of lines deinterlace the video (convert from interlaced to progressive). You need to uncomment the correct lines depending on whether your video is TFF or BFF (in this example the video was BFF).

The next block of lines resize the video so that the pixels are square in the final output. You need to know whether the source is fullscreen (FS) or widescreen (WS) to determine the output resolution (this example used widescreen video).

The last line converts the video to a colorspace that the x264 encoder can handle.

Now you need to create a batch file to encode (compress) the video. It should look something like this:


x264 -B 750 -f 0:0 -b 3 --b-pyramid -w -p 1 -r 1 -A none --me dia -m 1 --progress -o NUL filter.avs
x264 -B 750 -f 0:0 -b 3 --b-pyramid -w -p 2 -r 5 -A all --me umh -m 6 -8 --progress -o video.mp4 filter.avs
avs2wav filter.avs - | faac -o audio.mp4 -
mp4box -new -add audio.mp4 -add video.mp4 muxed.mp4


The first line does the first pass encoding of the video and the second line does the second pass. You might want to change the "-B 750" parameter. This sets the bitrate of the output (in kB/sec). The third line encodes the audio (around 100 KB/sec AAC). The fourth line multiplexes the audio and video streams into one MP4 file.

zambelli
22nd December 2005, 08:42
Some comments on the aspect ratio and resizing:

# FS source multiply horz by 10/11 (720x480 -> 656x480)
You're technically right about the 10:11 PAR, but 656x480 is a weird resolution and you'll probably hear some complaints about that. :) A compromise solution would be to crop to 704x480 first and then resize to 640x480. The 16 cropped pixels are technically outside the NTSC area defined in the ITU.601 spec, so no big loss there.

# WS source multiply vert by 33/40 (720x480 -> 720x400)
That's an unnecessary loss of vertical resolution. It would be better to resize to 852x480, thus preserving the full vertical resolution while interpolating the horizontal resolution.

However, considering that you're converting to MP4 and the format supports aspect ratio flags, the ideal solution would be to do no resizing at all but set the correct AR flags and let the decoder resize on the fly. The concept is no different than MPEG-2 or WMV anamorphic video.

raymod2
2nd January 2006, 06:45
What do you guys think about moving the ConvertToYV12() higher up in the script? It should make it run faster but would it affect the quality? Can the deinterlacing and resizing filters do a better job with the extra color information?