====== Batch processing ======
This page tries to explain how batch processing works with Avidemux.
This tutorial shows how one can process multiple video files with same settings. This is useful for cases where you have to e.g. convert multiple video files to certain format.
===== Short introduction to different methods =====
Since AVIdemux supports both Command-line processing ([[using:command_line_usage]]) and JS scripting (ECMAScript) ([[using:Scripting]]) possibilities, there are multiple ways to batch process your files:
- Command-line only processing with bash script/.bat file or similar.
- JS scripting (ECMAScript) only processing
- Combination of command-line and JS scripting
==== Command-line only batch processing ====
AVIdemux command-line support doesn't allow you to change all possible options. But it usually works OK for simple format conversions. Since it allows you to use additional command-line tools, you can for example automate YouTube uploads after AVIdemux has processed the files. Because different operating systems and shells have different kind of filename limits, commands etc. some command-line samples presented below won't work in all systems/shells.
Win32 example (.bat file) that will convert all .mp4 files in current folder to .avi (XviD+MP3). Original files aren't modified. New files are named like **something.mp4.avi**
set avidemux="C:\Program Files\Avidemux 2.5\avidemux2.exe"
set videocodec=Xvid
set audiocodec=MP3
for %%f in (*.mp4) do %avidemux% --video-codec %videocodec% --audio-codec %audiocodec% --force-alt-h264 --load "%%f" --save "%%f.avi" --quit
(you should change "C:\Program Files\Avidemux 2.5\avidemux2.exe" in case your AVIdemux installation is located in different folder)
You can copy that text to new text file, then rename the text file to **something.bat** and move it to the folder where you want to process the files. Then just double click the **something.bat** and processing should start.
For Linux/Unixes using Bash shell similar script would be
#!/bin/bash
VIDEOCODEC="Xvid"
AUDIOCODEC="MP3"
for FIL in `ls *mp4 | sort` ; do
avidemux2 --video-codec $VIDEOCODEC --audio-codec $AUDIOCODEC --force-alt-h264 --load "$FIL" --save ${FIL%.*}.avi --quit
done
(this will create **something.avi** from **something.mp4**)
If you want to force certain bitrate for audio and video with Win32, do following
set avidemux="C:\softa\avidemux_r6854\avidemux2.exe"
set videocodec=Xvid
set videobitrate=cbr=512
set audiocodec=MP3
set audiobitrate=64
for %%f in (*.mp4) do %avidemux% --video-codec %videocodec% --video-conf %videobitrate% --audio-codec %audiocodec% --audio-bitrate %audiobitrate% --force-alt-h264 --load "%%f" --save "%%f.avi" --quit
For Linux/Unixes using Bash shell similar script would be
#!/bin/bash
VAR="batchfiles.txt"
VIDEOCODEC="Xvid"
AUDIOCODEC="MP3"
VIDEOBITRATE="cbr=512"
AUDIOBITRATE="64"
for FIL in `ls *mp4 | sort` ; do
avidemux2 --video-codec $VIDEOCODEC --video-conf $VIDEOBITRATE --audio-codec $AUDIOCODEC --audio-bitrate $AUDIOBITRATE --force-alt-h264 --load "$FIL" --save ${FIL%.*}.avi --quit
done
(this will create **something.avi** from **something.mp4**)
==== JS scripting (ECMAScript) only batch processing ====
Since AVIdemux supports JS scripting (ECMAScript), you can create scripts with it also. These scripts can be loaded from command-line (**--run**) or from GUI (**File -> Load/Run project...**). Because environment is same on all platforms, the scripts are quite portable. Only problematic part is slashing since some operating systems assume **/** is the proper one and few systems think **\** is the right one.
With JS scripts (ECMAScripts) one can also use additional GUI elements (file and folder select dialogs) which helps if you must make newbie friendly scripts.
First the actual script you can save as **something.js**, when you run it, it will ask input folder and filetype (first dialog) + output folder and filename (second dialog). Script picks all files of same type from first folder and saves them as XviD+MP3 AVI to second folder.
//AD <-
/*
Simple script that scans the orgDir directory
and loads all .xyz files and encodes them to Xvid+MP3 AVI
The resulting file is put in destDir directory
Using new directorySearch API
*/
var app = new Avidemux();
var ds = new DirectorySearch();
var srcDir;
var dstDir;
var reg = new RegExp(".$");
var extReg = new RegExp(".*[.](.+)$");
//this is the directory separator char for WINDOWS! for *nix, it should be: sep = "/";
var sep = "\\";
var fileExt;
var pickedExt;
var dstPath;
var filesDone = 0;
var filesSkip = 0;
var filesErrd = 0;
displayInfo("Pick any file in the source directory. All files of that type (.xyz) will be processed. Filename will be ignored but the file extension will be used! \r\n\r\ne.g. If you want to process AVIs, be sure to pick any .avi file!");
//pop up a dialog asking for a source file. We will remove the file name leaving just the dir
srcDir = fileReadSelect();
//run the extReg regex designed to pull out just the extension of this file
pickedExt = srcDir.replace(extReg, "$1").toLowerCase();
displayInfo(pickedExt + "Pick the destination directory and enter a random filename. Filename will be ignored");
//pop up a dialog asking for a destination file. We will remove the file name leaving just the dir
dstDir = fileWriteSelect();
//extract just the path part of each picked file
srcDir = pathOnly(srcDir);
dstDir = pathOnly(dstDir);
//if the path ends with a directory separator characters, remove them
//it shouldnt be more than one, but a while loop will cater for any number of them
//so c:\temp\\\\ becomes c:\temp
while(srcDir.charAt(srcDir.length-1) == sep){
srcDir=srcDir.replace(reg,"");
}
while(dstDir.charAt(dstDir.length-1) == sep){
dstDir=dstDir.replace(reg,"");
}
//initalise the directory search by telling it the directory
if(ds.Init(srcDir))
{
//while we havent reached the end of the files list in the directory
// --> note that every call to ds.NextFile() moves it on by one
// --> until ds.NextFIle() returns false meaning it finished the list
while(ds.NextFile())
{
//pull the filename out (no path)
dstPath=ds.GetFileName();
//only process valid files that are not directories
if(!ds.isNotFile && !ds.isDirectory && !ds.isSingleDot && !ds.isDoubleDot)
{
//we want to do some work with the extension so pull it out now
ext=ds.GetExtension();
//is the extension of this file the same as the one we picked?
if(ext.toLowerCase() == pickedExt)
{
//build the output file name. To avoid ovwriting source file, if dir is the same prefix a _
if(srcDir.toLowerCase() == dstDir.toLowerCase()){
dstPath = dstDir + sep + "_" + ds.GetFileName() + ".avi";
}else{
dstPath = dstDir + sep + ds.GetFileName() + ".avi";
}
//build the source file path
srcPath = srcDir + sep + ds.GetFileName();
//we set this option first. If Avidemux loads a file it thinks need unpacking/time mapping
//then setting this option now will ensure that operation is done when we load
app.forceUnpack();
//load the file. this action also purges all the video filters and codec settings
app.load(srcPath);
//see if the audio is VBR. if it is, this builds a time map. if it is not, this does nothing
app.audio.scanVbr();
//this might not need doing if forceUnpack has been stated. I dont think it can be done
//twice though so its safe to repeat here
app.rebuildIndex();
//all the code in this section was generated by the app. if you want to write a script and use filters
//you can get the app to do the hard work by:
// load one of the files you will script
// set all the video and audio options you want
// on file menu, pick Save Project as (note: NOT save avi file!!)
//the job file you save will have a section detailing the filters. copy it
//re-encode to xvid 2 pass, bitrate = 1000 (plus 128 for audio = 1128 kbps, 507.6 megs per hour)
//all options are set to simple, no GMC, no Qpel etc so that standalone simple players are ok
app.video.codecPlugin("92B544BE-59A3-4720-86F0-6AD5A2526FD2", "Xvid", "2PASSBITRATE=1000", "0false11highdctfalsetruefalsefalsefalsefalsefalsefalsefalsenone030020falsefalse111313131150100h.26388888888888888888888888888888888888888888888888888888888888888881111111111111111111111111111111111111111111111111111111111111111true161001001020155500000");
app.audio.reset();
app.audio.codec("Lame",128,20,"80 00 00 00 00 00 00 00 01 00 00 00 02 00 00 00 00 00 00 00 ");
app.audio.normalizeMode=0;
app.audio.normalizeValue=0;
app.audio.delay=0;
app.audio.mixer="STEREO";
app.setContainer("AVI");
//write that file
app.save(dstPath);
} else{ //we decide not to process this file
//some diagnostic info incase you wonder why a whole dir of files arent processing
whyNot = dstPath + " wasnt processed because: ";
if(ds.isNotFile)
whyNot += "it is not a file, ";
if(ds.isDirectory)
whyNot += "it is a directory, ";
if(ds.isHidden)
whyNot += "it is hidden, ";
if(ds.isArchive)
whyNot += "it is marked for archive, ";
if(ds.isSingleDot)
whyNot += "it is a meta-ref to the current dir (.), ";
if(ds.isDoubleDot)
whyNot += "it is a meta-ref to the parent dir (..), ";
//whats that reason now?
print(whyNot);
}
}
}
ds.Close();
}
You can create scripts quite easily by yourself. You can apply needed settings via GUI and then use **File -> Save Project As...** to create text file that contains all settings. Then you can open that file in text editor and copy+paste needed stuff to new script (or replaces parts of other script).
==== Combination of command-line and JS scripting ====
Last option is to combine best of both worlds. Usually the best situation for this is to create automated script (no dialogs) that you will run for all files.
Short Win32 example below, first the script (again save it as **something.js**)
//AD <- Needed to identify//
var app = new Avidemux();
//** Postproc **
app.video.setPostProc(3,3,0);
//** Filters **
//** Video Codec conf **
app.video.codecPlugin("92B544BE-59A3-4720-86F0-6AD5A2526FD2", "Xvid", "2PASSBITRATE=1000", "0false11highdctfalsetruefalsefalsefalsefalsefalsefalsefalsenone030020falsefalse111313131150100h.26388888888888888888888888888888888888888888888888888888888888888881111111111111111111111111111111111111111111111111111111111111111true161001001020155500000");
//** Audio **
app.audio.reset();
app.audio.codec("Lame",128,20,"80 00 00 00 00 00 00 00 01 00 00 00 02 00 00 00 00 00 00 00 ");
app.audio.normalizeMode=0;
app.audio.normalizeValue=0;
app.audio.delay=0;
app.audio.mixer="STEREO";
app.setContainer("AVI");
setSuccess(1);
//app.Exit();
//End of script
Win32 example (.bat file) that will convert all .mp4 files in current folder to .avi (XviD+MP3). Original files aren't modified. New files are named like **something.mp4.avi**
set avidemux="C:\Program Files\Avidemux 2.5\avidemux2.exe"
for %%f in (*.mp4) do %avidemux% --force-alt-h264 --load "%%f" --run something.js --save "%%f.avi" --quit
For Linux/Unixes using Bash shell similar script would be
#!/bin/bash
for FIL in `ls *mp4 | sort` ; do
avidemux2 --force-alt-h264 --load "$FIL" --run something.js --save ${FIL%.*}.avi --quit
done
(this will create **something.avi** from **something.mp4**)
If you create your own combine batch settings, make sure order of command-line parameters is always --load something, --run something and --save something (AVIdemux will run these options in given order).
==== Tips ====
* You can replace **avidemux2** with **avidemux2_cli** if you want to process files without GUI
* You can use **--nogui** option in case you want to suppress all dialogs (it must be first option!)