Converting Avi files to Mp4 for Nokia 5800
Recently I acquired a Nokia 5800 XpressMusic mobile phone. Great one! It comes with a native video player capable of playing video up to a resolution of 640×480 at 30fps. The video player, in combination with its big screen and the TV-out capability, makes this phone a good device for watching a movie with an acceptable quality. It can play videos in the following formats:
- 3GPP formats (H.263)
- Flash Video
- H.264/AVC
- MPEG-4
- RealVideo 7,8,9/10
- WMV 9
But as you can see, there is no AVI playback. Right now there is no AVI player that supports Symbian s60 5th. So we’ll have to transcode our .avi files to .mp4 before playing them on the Nokia 5800. Playing with mencoder I’ve come to following commands for converting an AVI to MP4, Nokia 5800 ready. Subtitles included and 2 pass (which means high quality results):
mencoder -of lavf -lavfopts format=mp4 -oac lavc -ovc lavc -lavcopts aglobal=1:vglobal=1:acodec=libfaac:vcodec=mpeg4:abitrate=96:vbitrate=800:keyint=250:mbd=1:vqmax=10:lmax=10:vpass=1:turbo -ofps 25 -af lavcresample=44100 -vf harddup,scale=640:-3 -sub 'input.srt' 'input.avi' -o 'output.mp4' mencoder -of lavf -lavfopts format=mp4 -oac lavc -ovc lavc -lavcopts aglobal=1:vglobal=1:acodec=libfaac:vcodec=mpeg4:abitrate=96:vbitrate=800:keyint=250:mbd=1:vqmax=10:lmax=10:vpass=2 -ofps 25 -af lavcresample=44100 -vf harddup,scale=640:-3 -sub 'input.srt' 'input.avi' -o 'output.mp4'
Messy huh? What I did is a Ruby script that takes AVI files as an argument an transcode them taking care of everything. I mean:
- Automatically names the output as “file.mp4″ from “file.avi”
- Checks if there are subtitles (as an SRT file) and add them.
- Performs the 2 passes.
- Deletes the useless “divx2pass.log” once finished.
- Resize and resample video and audio to fit the phone specs.
Here is the script:
#!/usr/bin/ruby ARGV.each do |input_f| everything_ok = true log_file = "divx2pass.log" # Escapes single quotes input_f = input_f.split("'").join("\'\\'\'") # Extracts the file name without extension file_name = input_f.split(".")[0..-2].join(".") # Output file output_f = file_name + ".mp4" puts "\nConverting \'#{input_f}\' into \'#{output_f}\':" # Check if there are subtitles to add to the file subs_args = File.exists?(file_name + ".srt") ? " -sub \'#{file_name}.srt\'" : "" # Encodes with two pass [":vpass=1:turbo",":vpass=2"].each do |vpass| command = "mencoder" command << " -of lavf -lavfopts format=mp4" # Set container format to mp4 command << " -oac lavc" # Audio will be encoded with a libavcodec codec command << " -ovc lavc" # Video will be encoded with a libavcodec codec command << " -lavcopts" # Begin of options for libavcodec command << " aglobal=1:vglobal=1" # Needed for mp4 command << ":acodec=libfaac" # Encode audio as AAC command << ":vcodec=mpeg4" # Encode video as mp4 too command << ":abitrate=96" # Audio bitrate in Kbps command << ":vbitrate=800" # Video bitrate in Kbps command << ":keyint=250" # Number of frames between keyframes, a bit lower than the default to save some Megabytes command << ":mbd=1" # Macroblock decision algorithm set to minimize size of file command << ":vqmax=10:lmax=10" # Don't know whay but raises the video quality command << vpass # First pass or second pass command << " -ofps 25" # Convert the video to 25 fps to avoid incompatible fps values command << " -af lavcresample=44100" # Resample the audio command << " -vf harddup,scale=640:-3" # Scale the video to 640 px width mantaining aspect ratio command << subs_args # Add subs # command << " -endpos 10" # Encode just the first 10 seconds of video, useful for testing command << " \'#{input_f}\' -o \'#{output_f}\'" # file names puts command everything_ok = system command if !everything_ok puts "There were errors encoding #{input_f}, aborting." break end end File.delete log_file if File.exists? log_file puts "File succesfully encoded as \'#{output_f}\'" if everything_ok end
Use it like this:
avi2mp4 myfile.avi
And it will generate “myfile.mp4″. It also can receive several files as argument and will encode each of them separately. Hope you find this useful!
English
Español
Rune Larsen
14 Apr, 2009
Awsome. Been searching for this. Thanks for the good work
lukas
1 May, 2009
thanks for the nice script, was just about out to go on a trial-and-error spree when I found this :3
mohamed
13 Sep, 2009
thanks but how can i use this script in which folder in the nokia 5800 i can add it pls explain n details
s_ms
20 Sep, 2009
big thanks!
noob
24 Sep, 2009
I DIDNT UNDERSTAND WATEVER U EXPLAINED.. I DUNNOO WAT IT MEANS N WAT TO DO WITH DIS.. WHICH CONVERTER TO USE N HOW IT WORKS.. PLEASE TELL NOOBS LIKE ME THAT WAT DO WE HAVE TO DO COZ I DNT HAVE ANY KNOWLEDGE ABOUT WATEVER U WROTE ABOYE.. SCRIPTS N ALL!! PLEASE HELP.
PLEASE REPLY ME THRU EMAIL.. PLEASE PLEASE.. ALSO POST THE SOLUTION HERE!!!!!
noob
24 Sep, 2009
i m using windows xp
Ian Spillane
12 Oct, 2009
The line that reads:
command << " -vf harddup,scale=640:-3" # Scale the video to 640 px width mantaining aspect ratio
Needs to be changed to … scale=-3:360 … so as to scale the video along the shortest side (otherwise some traditional size videos will still not fit and thus work on the Nokia 5800).
Thanks for the script. It is working nicely. I call it with another script to loop through a whole directory (all those episodes that need to be converted):
for file in $(find -name "*.avi")
do
./avi2mp4 "$file"
done
Of course this could be added to your script itself.
Cecil
15 Nov, 2009
Great script.
I don’t do Ruby before. So I’ve to install Ruby for Windows.
Your script works great:)
Many thanks.
Vikas
22 Jan, 2010
Simply put, IT’S AWESOME!!
I’ve been trying to do this for my Nokia 5230 since 3 days using mencoder. `midentify`ing doesn’t show any difference between the video I generate and the one that Format Factory gemerates but mine doesn’t play on the mobile. Now, thanks to you, it works flawlessly…
Smiley000
18 Feb, 2010
Can you please help me with your script. I have never used Ruby before. I installed it, i copied you script into Ruby`s window and now?
What to do?
To save it as what type of file?
Or how to do it?
Please help me !
Susana
10 Mar, 2010
Nice post. automate-everythingc.om is killer.
Munira
6 Apr, 2010
Can some please tell me what to do in details as i didnt understand a single word of teh above details…. i need to go like step1, then step2 n so on… you cab email me on soni.691989@hotmail.com.. PLEASE HELP!!!!
atul
27 May, 2010
Can some please tell me what to do in details as i didnt understand a single word of teh above details…. i need to go like step1, then step2 n so on
even i didn’t under stand a word it will be great help if u tell it step by step. where this script is to be written.
Sergio Lima
5 Nov, 2010
Hi.
i dont know anything about programing (even at Ruby, or C, C+, etc) but I understood u used the scrip posted above to do the proper convertion…
I´m trying to find a way to watch my movies with the subtitles in 5800, but i couldn´t find it yet.
Can u provide a explanation how to use that script you wrote?
Thanks anyway
Frankie Mamaclay
15 Feb, 2011
Excellent post however , I was wondering if you could write a litte more on this subject? I’d be very thankful if you could elaborate a little bit more. Appreciate it!
Jayo
16 Mar, 2011
Thanks for the script. It got me a lot closer to a working example. I guess this would have worked for me if I had a mencoder compiled with support for libfaac, but I don’t, which is quite common. So, for those who run into that problem, here’s a one-liner version that will work on the command line in Linux, using mp3lame for the audio encoder:
inf=”/home/me/Videos/SomeMpgFile.mpg” && outf=”SomeMp4File.mp4″ && mencoder -of lavf -lavfopts format=mp4 -oac mp3lame -lameopts cbr -ovc lavc -lavcopts aglobal=1:vglobal=1:acodec=mp3lame:vcodec=mpeg4:abitrate=96:vbitrate=800:keyint=250:mbd=1:vqmax=10:lmax=10:vpass=1:turbo -ofps 25 -af lavcresample=44100 -vf harddup,scale=640:360 “$inf” -o “$outf” && mencoder -msglevel all=5 -of lavf -lavfopts format=mp4 -oac mp3lame -lameopts cbr -ovc lavc -lavcopts aglobal=1:vglobal=1:acodec=mp3lame:vcodec=mpeg4:abitrate=96:vbitrate=800:keyint=250:mbd=1:vqmax=10:lmax=10:vpass=2 -ofps 25 -af lavcresample=44100 -vf harddup,scale=640:360 “$inf” -o “$outf”
Obviously the inf and outf variables specify the input and output filenames for the video. Note that my ‘scale’ setting is for the Nokia 5230, adjust as appropriate. Also note, I removed the subtitles feature in this simple one-liner approach.
I assume this can work also on Windows, but I haven’t tested.
Zackary Mcgary
1 Jun, 2011
There’s obviously a lot to understand about this. I believe you created some excellent points in Characteristics also.
Keep working , fantastic work!