<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Automate Everything &#187; Mobile</title>
	<atom:link href="http://automate-everything.com/category/mobile/feed/" rel="self" type="application/rss+xml" />
	<link>http://automate-everything.com</link>
	<description>Ruby, Linux and other hints, tips and tricks.</description>
	<lastBuildDate>Tue, 13 Jul 2010 06:36:43 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Converting Avi files to Mp4 for Nokia 5800</title>
		<link>http://automate-everything.com/lang/en/2009/03/converting-avi-files-to-mp4-for-nokia-5800</link>
		<comments>http://automate-everything.com/lang/en/2009/03/converting-avi-files-to-mp4-for-nokia-5800#comments</comments>
		<pubDate>Sun, 29 Mar 2009 16:30:28 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Linux]]></category>
		<category><![CDATA[Mobile]]></category>
		<category><![CDATA[Ruby]]></category>

		<guid isPermaLink="false">http://blog.team-trick.net/?p=28</guid>
		<description><![CDATA[I've come to following Ruby script for converting an avi to mp4, Nokia 5800 ready. Subtitles included and 2 pass (which means high quality results).]]></description>
			<content:encoded><![CDATA[<p>Recently I acquired a <strong>Nokia 5800 XpressMusic</strong> mobile phone. Great one! It comes with a native video player capable of playing video up to a resolution of 640&#215;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:</p>
<ul>
<li>3GPP formats (H.263)</li>
<li>Flash Video</li>
<li>H.264/AVC</li>
<li>MPEG-4</li>
<li>RealVideo 7,8,9/10</li>
<li>WMV 9</li>
</ul>
<p>But as you can see, there is <strong>no AVI playback</strong>. Right now there is no AVI player that supports Symbian s60 5th. So we&#8217;ll have to transcode our .avi files to .mp4 before playing them on the Nokia 5800. Playing with mencoder I&#8217;ve come to following commands for <strong>converting an AVI to MP4</strong>, Nokia 5800 ready. <strong>Subtitles included and 2 pass</strong> (which means high quality results):</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">mencoder <span style="color: #660033;">-of</span> lavf <span style="color: #660033;">-lavfopts</span> <span style="color: #007800;">format</span>=mp4 <span style="color: #660033;">-oac</span> lavc <span style="color: #660033;">-ovc</span> lavc <span style="color: #660033;">-lavcopts</span> <span style="color: #007800;">aglobal</span>=<span style="color: #000000;">1</span>:<span style="color: #007800;">vglobal</span>=<span style="color: #000000;">1</span>:<span style="color: #007800;">acodec</span>=libfaac:<span style="color: #007800;">vcodec</span>=mpeg4:<span style="color: #007800;">abitrate</span>=<span style="color: #000000;">96</span>:<span style="color: #007800;">vbitrate</span>=<span style="color: #000000;">800</span>:<span style="color: #007800;">keyint</span>=<span style="color: #000000;">250</span>:<span style="color: #007800;">mbd</span>=<span style="color: #000000;">1</span>:<span style="color: #007800;">vqmax</span>=<span style="color: #000000;">10</span>:<span style="color: #007800;">lmax</span>=<span style="color: #000000;">10</span>:<span style="color: #007800;">vpass</span>=<span style="color: #000000;">1</span>:turbo <span style="color: #660033;">-ofps</span> <span style="color: #000000;">25</span> <span style="color: #660033;">-af</span> <span style="color: #007800;">lavcresample</span>=<span style="color: #000000;">44100</span> <span style="color: #660033;">-vf</span> harddup,<span style="color: #007800;">scale</span>=<span style="color: #000000;">640</span>:-<span style="color: #000000;">3</span> <span style="color: #660033;">-sub</span> <span style="color: #ff0000;">'input.srt'</span> <span style="color: #ff0000;">'input.avi'</span> <span style="color: #660033;">-o</span> <span style="color: #ff0000;">'output.mp4'</span>
mencoder <span style="color: #660033;">-of</span> lavf <span style="color: #660033;">-lavfopts</span> <span style="color: #007800;">format</span>=mp4 <span style="color: #660033;">-oac</span> lavc <span style="color: #660033;">-ovc</span> lavc <span style="color: #660033;">-lavcopts</span> <span style="color: #007800;">aglobal</span>=<span style="color: #000000;">1</span>:<span style="color: #007800;">vglobal</span>=<span style="color: #000000;">1</span>:<span style="color: #007800;">acodec</span>=libfaac:<span style="color: #007800;">vcodec</span>=mpeg4:<span style="color: #007800;">abitrate</span>=<span style="color: #000000;">96</span>:<span style="color: #007800;">vbitrate</span>=<span style="color: #000000;">800</span>:<span style="color: #007800;">keyint</span>=<span style="color: #000000;">250</span>:<span style="color: #007800;">mbd</span>=<span style="color: #000000;">1</span>:<span style="color: #007800;">vqmax</span>=<span style="color: #000000;">10</span>:<span style="color: #007800;">lmax</span>=<span style="color: #000000;">10</span>:<span style="color: #007800;">vpass</span>=<span style="color: #000000;">2</span> <span style="color: #660033;">-ofps</span> <span style="color: #000000;">25</span> <span style="color: #660033;">-af</span> <span style="color: #007800;">lavcresample</span>=<span style="color: #000000;">44100</span> <span style="color: #660033;">-vf</span> harddup,<span style="color: #007800;">scale</span>=<span style="color: #000000;">640</span>:-<span style="color: #000000;">3</span> <span style="color: #660033;">-sub</span> <span style="color: #ff0000;">'input.srt'</span> <span style="color: #ff0000;">'input.avi'</span> <span style="color: #660033;">-o</span> <span style="color: #ff0000;">'output.mp4'</span></pre></div></div>

<p>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: </p>
<ul>
<li>Automatically names the output as &#8220;file.mp4&#8243; from &#8220;file.avi&#8221;</li>
<li>Checks if there are subtitles (as an SRT file) and add them.</li>
<li>Performs the 2 passes.</li>
<li>Deletes the useless &#8220;divx2pass.log&#8221; once finished.</li>
<li>Resize and resample video and audio to fit the phone specs.</li>
</ul>
<p>Here is the script:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#008000; font-style:italic;">#!/usr/bin/ruby</span>
&nbsp;
ARGV.<span style="color:#9900CC;">each</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>input_f<span style="color:#006600; font-weight:bold;">|</span>
  everything_ok = <span style="color:#0000FF; font-weight:bold;">true</span>
  log_file = <span style="color:#996600;">&quot;divx2pass.log&quot;</span>
&nbsp;
  <span style="color:#008000; font-style:italic;"># Escapes single quotes</span>
  input_f = input_f.<span style="color:#CC0066; font-weight:bold;">split</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;'&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">join</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;<span style="color:#000099;">\'</span><span style="color:#000099;">\\</span>'<span style="color:#000099;">\'</span>&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
&nbsp;
  <span style="color:#008000; font-style:italic;"># Extracts the file name without extension</span>
  file_name = input_f.<span style="color:#CC0066; font-weight:bold;">split</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;.&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#91;</span>0..<span style="color:#006600; font-weight:bold;">-</span><span style="color:#006666;">2</span><span style="color:#006600; font-weight:bold;">&#93;</span>.<span style="color:#9900CC;">join</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;.&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span>  
&nbsp;
  <span style="color:#008000; font-style:italic;"># Output file</span>
  output_f = file_name <span style="color:#006600; font-weight:bold;">+</span> <span style="color:#996600;">&quot;.mp4&quot;</span>
  <span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;<span style="color:#000099;">\n</span>Converting <span style="color:#000099;">\'</span>#{input_f}<span style="color:#000099;">\'</span> into <span style="color:#000099;">\'</span>#{output_f}<span style="color:#000099;">\'</span>:&quot;</span>
&nbsp;
  <span style="color:#008000; font-style:italic;"># Check if there are subtitles to add to the file</span>
  subs_args = <span style="color:#CC00FF; font-weight:bold;">File</span>.<span style="color:#9900CC;">exists</span>?<span style="color:#006600; font-weight:bold;">&#40;</span>file_name <span style="color:#006600; font-weight:bold;">+</span> <span style="color:#996600;">&quot;.srt&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span> ? <span style="color:#996600;">&quot; -sub <span style="color:#000099;">\'</span>#{file_name}.srt<span style="color:#000099;">\'</span>&quot;</span> : <span style="color:#996600;">&quot;&quot;</span>
&nbsp;
  <span style="color:#008000; font-style:italic;"># Encodes with two pass</span>
  <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#996600;">&quot;:vpass=1:turbo&quot;</span>,<span style="color:#996600;">&quot;:vpass=2&quot;</span><span style="color:#006600; font-weight:bold;">&#93;</span>.<span style="color:#9900CC;">each</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>vpass<span style="color:#006600; font-weight:bold;">|</span>
    command = <span style="color:#996600;">&quot;mencoder&quot;</span>
    command <span style="color:#006600; font-weight:bold;">&lt;&lt;</span> <span style="color:#996600;">&quot; -of lavf -lavfopts format=mp4&quot;</span> <span style="color:#008000; font-style:italic;"># Set container format to mp4</span>
    command <span style="color:#006600; font-weight:bold;">&lt;&lt;</span> <span style="color:#996600;">&quot; -oac lavc&quot;</span> <span style="color:#008000; font-style:italic;"># Audio will be encoded with a libavcodec codec</span>
    command <span style="color:#006600; font-weight:bold;">&lt;&lt;</span> <span style="color:#996600;">&quot; -ovc lavc&quot;</span> <span style="color:#008000; font-style:italic;"># Video will be encoded with a libavcodec codec</span>
&nbsp;
    command <span style="color:#006600; font-weight:bold;">&lt;&lt;</span> <span style="color:#996600;">&quot; -lavcopts&quot;</span> <span style="color:#008000; font-style:italic;"># Begin of options for libavcodec</span>
    command <span style="color:#006600; font-weight:bold;">&lt;&lt;</span> <span style="color:#996600;">&quot; aglobal=1:vglobal=1&quot;</span> <span style="color:#008000; font-style:italic;"># Needed for mp4</span>
    command <span style="color:#006600; font-weight:bold;">&lt;&lt;</span> <span style="color:#996600;">&quot;:acodec=libfaac&quot;</span> <span style="color:#008000; font-style:italic;"># Encode audio as AAC</span>
    command <span style="color:#006600; font-weight:bold;">&lt;&lt;</span> <span style="color:#996600;">&quot;:vcodec=mpeg4&quot;</span> <span style="color:#008000; font-style:italic;"># Encode video as mp4 too</span>
    command <span style="color:#006600; font-weight:bold;">&lt;&lt;</span> <span style="color:#996600;">&quot;:abitrate=96&quot;</span> <span style="color:#008000; font-style:italic;"># Audio bitrate in Kbps</span>
    command <span style="color:#006600; font-weight:bold;">&lt;&lt;</span> <span style="color:#996600;">&quot;:vbitrate=800&quot;</span> <span style="color:#008000; font-style:italic;"># Video bitrate in Kbps</span>
    command <span style="color:#006600; font-weight:bold;">&lt;&lt;</span> <span style="color:#996600;">&quot;:keyint=250&quot;</span> <span style="color:#008000; font-style:italic;"># Number of frames between keyframes, a bit lower than the default to save some Megabytes</span>
    command <span style="color:#006600; font-weight:bold;">&lt;&lt;</span> <span style="color:#996600;">&quot;:mbd=1&quot;</span> <span style="color:#008000; font-style:italic;"># Macroblock decision algorithm set to minimize size of file</span>
    command <span style="color:#006600; font-weight:bold;">&lt;&lt;</span> <span style="color:#996600;">&quot;:vqmax=10:lmax=10&quot;</span> <span style="color:#008000; font-style:italic;"># Don't know whay but raises the video quality</span>
    command <span style="color:#006600; font-weight:bold;">&lt;&lt;</span> vpass <span style="color:#008000; font-style:italic;"># First pass or second pass</span>
&nbsp;
    command <span style="color:#006600; font-weight:bold;">&lt;&lt;</span> <span style="color:#996600;">&quot; -ofps 25&quot;</span> <span style="color:#008000; font-style:italic;"># Convert the video to 25 fps to avoid incompatible fps values</span>
    command <span style="color:#006600; font-weight:bold;">&lt;&lt;</span> <span style="color:#996600;">&quot; -af lavcresample=44100&quot;</span> <span style="color:#008000; font-style:italic;"># Resample the audio</span>
    command <span style="color:#006600; font-weight:bold;">&lt;&lt;</span> <span style="color:#996600;">&quot; -vf harddup,scale=640:-3&quot;</span> <span style="color:#008000; font-style:italic;"># Scale the video to 640 px width mantaining aspect ratio</span>
    command <span style="color:#006600; font-weight:bold;">&lt;&lt;</span> subs_args <span style="color:#008000; font-style:italic;"># Add subs</span>
&nbsp;
    <span style="color:#008000; font-style:italic;"># command &lt;&lt; &quot; -endpos 10&quot; # Encode just the first 10 seconds of video, useful for testing</span>
&nbsp;
    command <span style="color:#006600; font-weight:bold;">&lt;&lt;</span> <span style="color:#996600;">&quot; <span style="color:#000099;">\'</span>#{input_f}<span style="color:#000099;">\'</span> -o <span style="color:#000099;">\'</span>#{output_f}<span style="color:#000099;">\'</span>&quot;</span> <span style="color:#008000; font-style:italic;"># file names</span>
&nbsp;
    <span style="color:#CC0066; font-weight:bold;">puts</span> command
    everything_ok = <span style="color:#CC0066; font-weight:bold;">system</span> command
&nbsp;
    <span style="color:#9966CC; font-weight:bold;">if</span> !everything_ok
      <span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;There were errors encoding #{input_f}, aborting.&quot;</span>
      <span style="color:#9966CC; font-weight:bold;">break</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  <span style="color:#CC00FF; font-weight:bold;">File</span>.<span style="color:#9900CC;">delete</span> log_file <span style="color:#9966CC; font-weight:bold;">if</span> <span style="color:#CC00FF; font-weight:bold;">File</span>.<span style="color:#9900CC;">exists</span>? log_file
  <span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;File succesfully encoded as <span style="color:#000099;">\'</span>#{output_f}<span style="color:#000099;">\'</span>&quot;</span> <span style="color:#9966CC; font-weight:bold;">if</span> everything_ok
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>Use it like this:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">avi2mp4 myfile.avi</pre></div></div>

<p>And it will generate &#8220;myfile.mp4&#8243;. It also can receive several files as argument and will encode each of them separately. Hope you find this useful!</p>
]]></content:encoded>
			<wfw:commentRss>http://automate-everything.com/lang/en/2009/03/converting-avi-files-to-mp4-for-nokia-5800/feed</wfw:commentRss>
		<slash:comments>13</slash:comments>
		</item>
	</channel>
</rss>
