I've recently created a batch file to convert MKV (AVC) video files to standalone compatible AVI (Xvid). I thought I'd share my results with you since it took quite a bit of time and effort to get it working, and the method is readily adaptable for other formats.
- VSFilter (only required for hard-coded subtitles)
- Xvid codec
My first batch file is very simple. All it does is iterate through all the MKV files in a folder and then calls the second batch file with parts of each filename:
rem change R: to the drive letter of your source disk
rem change \cdtemp\cbb to the path to your source folder
rem change *.mkv to match the files that you want to process
for %%a in (*.mkv) do call "d:\cdtemp\batch\mkv2avi.bat" %%~da %%~pa %%~na
%%~da is the drive letter of the file (e.g. R: )
%%~pa is the path to the file (e.g. \cdtemp\cbb)
%%~na is the name of the file without the extension (e.g. episode1)
The second batch file does all the work:
rem %1 is drive letter, %2 is path, %3 is the name of the file minus the extension
if "%1"=="D:" set otherdrive=R:
if "%1"=="R:" set otherdrive=D:
rem demux video, audio and first subtitles (N.B. check stream ids with MediaInfo)
"D:\avtools\MKVtoolnix\mkvextract.exe" tracks "%1%2%3.mkv" 1:"%otherdrive%\cdtemp\cbb\%3.h264" 2:"%otherdrive%\cdtemp\cbb\%3.ac3" 3:"%otherdrive%\cdtemp\cbb\%3.ass"
rem dgindex video
"D:\avtools\dgavcdec\dgavcindex.exe" -i "%otherdrive%\cdtemp\cbb\%3.h264" -o "%1\cdtemp\cbb\%3.dga" -e
rem create avs file
echo loadplugin("D:\avtools\dgavcdec\DGAVCDecode.dll") > "%1\cdtemp\cbb\%3.avs"
echo loadplugin("D:\apps\AviSynth 2.5\plugins\VSFilter.dll") >> "%1\cdtemp\cbb\%3.avs"
echo AVCSource("%1\cdtemp\cbb\%3.dga") >> "%1\cdtemp\cbb\%3.avs"
echo TextSub("%otherdrive%\cdtemp\cbb\%3.ass") >> "%1\cdtemp\cbb\%3.avs"
rem xvid encoding
rem first pass
"d:\avtools\xvid_encraw.exe" -i "%1\cdtemp\cbb\%3.avs" -type 2 -o "%otherdrive% mp
ull" -pass1 -framerate 23.976 -progress 24
rem second pass
"d:\avtools\xvid_encraw.exe" -i "%1\cdtemp\cbb\%3.avs" -type 2 -o "%1\cdtemp\cbb\%3.avi" -pass2 -framerate 23.976 -bitrate 1250 -quality 6 -vhqmode 4 -bvhq -qtype 1 -nopacked -notrellis -imin 1 -bmin 1 -pmin 1 -progress 24
rem mux video and audio
rem thanks to celtic_druid for this script (http://forum.doom9.org/archive/index.php/t-124626.html)
setlocal ENABLEDELAYEDEXPANSION ENABLEEXTENSIONS
set TMPAMG=d: mp\%3.amg
echo CLEAR > "!TMPAMG!"
echo LOAD !SRCVFN!>> "!TMPAMG!"
echo LOAD !SRCAFN!>> "!TMPAMG!"
echo SELECT FILE 1 >> "!TMPAMG!"
echo ADD VIDEOSOURCE >> "!TMPAMG!"
echo SET OUTPUT OPTIONS >> "!TMPAMG!"
echo WITH SET OPTION >> "!TMPAMG!"
echo OVERWRITEDLG 0 >> "!TMPAMG!"
echo CLOSEAPP 1 >> "!TMPAMG!"
echo DONEDLG 0 >> "!TMPAMG!"
echo MAXFILES OFF >> "!TMPAMG!"
echo STDOUTPUTFMT AVI >> "!TMPAMG!"
echo AVI ADDJUNKBEFOREHEADERS 0 >> "!TMPAMG!"
echo AUDIO INTERLEAVE 4 FR >> "!TMPAMG!"
echo PRELOAD 200 >> "!TMPAMG!"
echo AVI HAALIMODE 0 >> "!TMPAMG!"
echo OPENDML 0 >> "!TMPAMG!"
echo LEGACY 0 >> "!TMPAMG!"
echo RECLISTS 0 >> "!TMPAMG!"
echo END WITH>> "!TMPAMG!"
echo START !MUXFLN!>> "!TMPAMG!"
- I've swapped between two hard drives (D: and R: ) for better performance. If you only have one, then set otherdrive=%1 at the start of the script. The script also assumes that the folder structure has been created on both drives.
- Use mp4box instead of mkvextract to demux mp4 files.
- The MKV files that I wanted to convert contain H264 video, hence the use of DGAVCDec. A simple change to DGMPGDec (DGIndex) and you can use mpeg2 video as your source.
- Since it uses an avs file as input to the Xvid encoding process, you can use the full power of AviSynth to modify your input video simply by adding lines to the script (Crop, LanczosResize etc.).
- I tried various tools to mux the final avi (mencoder, ffmpeg) but in the end I found that AVI-Mux_GUI was the only one that produced standalone compatible output. Thanks go to celtic_druid at doom9 for the muxing script.
I hope this might be of use to some of you; I'm between jobs at the moment so may well carry on tinkering with it and make it more general. Who knows, it might even turn into a distributable package...