Automate Everything: Ruby, Linux and other hints, tips and tricks.

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!

  • Awsome. Been searching for this. Thanks for the good work

  • thanks for the nice script, was just about out to go on a trial-and-error spree when I found this :3

  • thanks but how can i use this script in which folder in the nokia 5800 i can add it pls explain n details

  • big thanks!

  • 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!!!!!

  • i m using windows xp

  • 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.

  • Great script.
    I don’t do Ruby before. So I’ve to install Ruby for Windows.

    Your script works great:)

    Many thanks.

  • 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…

  • 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 !

  • Nice post. automate-everythingc.om is killer.

  • 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!!!!

  • 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.

You can follow any responses to this entry through the RSS 2.0 feed.