Home > Handy/Mobile, Ubuntu > How to convert a directory containing videos to .mp4 for mobile phones with one click

How to convert a directory containing videos to .mp4 for mobile phones with one click

Recently I described how to convert videos to .mp4 for your mobile phone using the avidemux2 gui.

But there is a faster, easier and more comfortable way to do this. With the following solution, you are able to convert a whole directory containing video files automatically to a phone-readable .mp4 format with just one click.

As in the previous Post, we still are using avidemux but now are invoking a job script to avidemux, so that we don’t need to configure the different settings manually. Along with a bash script, the conversion is done just with one double-click.

I have taken this script from the avidemux forum, credits go to the user gruntster.
Here is an example of a jobscript for a mobile phone having a display resolution of 320x240px, for instance the Sony Ericsson K770i.


var app = new Avidemux();
var dirSearch = new DirectorySearch();
var srcDir = "/directory/of/videos"; // pathOnly(fileReadSelect());
var dstDir = "/directory/for/videos"; // pathOnly(fileWriteSelect());

if (dirSearch.Init(srcDir))
{
while (dirSearch.NextFile())
{
if (!dirSearch.isNotFile && !dirSearch.isSingleDot && !dirSearch.isDoubleDot)
{
app.forceUnpack();
app.load(srcDir + “/” + dirSearch.GetFileName());
app.audio.scanVBR();
app.rebuildIndex();

// Resizing
var targetX = 320;
var targetY = 240;

var rX = app.video.getWidth() / targetX;
var rY = app.video.getHeight() / targetY;
var newX;
var newY;

if (rX > rY)
{
// resize by X
newX = targetX;
newY = Math.round(app.video.getHeight() / rX);
}
else
{
// resize by Y
newY = targetY;
newX = Math.round(app.video.getWidth() / rY);
}

// resize to multiple of 4
newX -= newX % 4;
newY -= newY % 4;

if (newX != app.video.getWidth() || newY != app.video.getHeight())
app.video.addFilter(“mpresize”, “w=” + newX, “h=” + newY, “algo=0”);

// Black bars
var barX = targetX – newX;
var barY = targetY – newY;

if (barX || barY)
app.video.addFilter(“addblack”, “left=” + (barX >> 1), “right=” + (barX >> 1), “top=” + (barY >> 1), “bottom=” + (barY >> 1));

//** Video Codec conf **
app.video.codec(“XVID4″,”CBR=250“,”500 06 00 00 00 01 00 00 00 fa 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 01 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 01 00 00 00 01 00 00 00 00 00 00 00 02 00 00 00 02 00 00 00 02 00 00 00 1f 00 00 00 1f 00 00 00 1f 00 00 00 00 00 00 00 01 00 00 00 01 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 05 00 00 00 05 00 00 00 05 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 96 00 00 00 64 00 00 00 01 00 00 00 01 00 00 00 00 00 00 00 00 00 00 00 01 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 “);

//** Audio **
app.audio.reset();
app.audio.codec(“aac”,64,4,”80 00 00 00 “);
app.audio.normalizeMode=0;
app.audio.normalizeValue=0;
app.audio.delay=0;

if (app.audio.getNbChannels(0) != 2)
app.audio.mixer(“STEREO”);

app.setContainer(“MP4”);

// Clip off source extension and save to destination with mp4 extension
var srcFile = dirSearch.GetFileName();

app.save(dstDir + “/” + srcFile.substring(0, srcFile.length – dirSearch.GetExtension().length) + “mp4”);
}
}
}

Copy and paste this code snippet into a file called for instance “k770.js” and make sure you are changing the red marked entries for your system. Especially the var srcDir =" and the "var dstDir =" have to be changed according to your directories, where srcDir should be the directory, where your raw videos are stored (those which have to be converted) and dstDir ist the directory where the converted videos are stored to.

Change targetX and targetY to the resolution of your mobile phone, typically 320 and 240 for phones with 320x240px resolution, or for the K530i, 176×220.

var targetX = 320;
var targetY = 240;

The last thing you could change is the bitrate of the video, the higher it is set, the better the quality, but also the size of the video. I found out, that 250-300 is more than plenty for the small screens of mobile phones. It depends on the quality of the source video too.


CBR=250

Same for the audio quality:

64 means 64kbit/s, you could choose a higher bitrate, like 96 or 128.

When you are finished with editing the file, save it as k770.js (or whatever) and create a bash file in the same directory where the k770.js resides.

Put this into the bash script:

avidemux2_gtk –nogui –run “k770.js” –quit

change k770.js to the filename of your created job script and save the bash script to for instance runme.sh.

Make the script executable:

chmod +x runme.sh

If everything went well, double clicking runme.sh should start converting all your videos in srcDir to dstDir to the format .mp4.

That’s it.

Advertisements
Categories: Handy/Mobile, Ubuntu
  1. No comments yet.
  1. June 9, 2008 at 1:06 pm

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: