  
<div id="snippet_136" class="snippet">
  <h2><a href="/snippets/136-How-to-extract-video-metadata-with-mplayer-and-Ruby" title="How to extract video metadata with mplayer and Ruby - Ruby - mplayer, ruby, metadata, flv, movie, video">How to extract video metadata with mplayer and Ruby</a></h2>
  <div class="details">
    <a style="background-color: #FFFF94;" href="http://snippets.aktagon.com/languages/124-Ruby">
      Ruby</a> posted over 2 years ago by christian
          
  </div>

  <div class="body">
    <p>You can easily extract video metadata into a hash with mplayer and Ruby:
<pre class="active4d"><span class="line-numbers">   1 </span> <span class="Keyword">class</span> <span class="TypeName">Metadata</span>
<span class="line-numbers">   2 </span>   
<span class="line-numbers">   3 </span>   <span class="Keyword">def</span> <span class="FunctionName">self.get</span>(<span class="FunctionArgument">file</span>)
<span class="line-numbers">   4 </span>     command  <span class="Operator">=</span> <span class="String"><span class="String">&quot;</span>mplayer -vo null -ao null -frames 0 -identify <span class="String"><span class="String">#{</span>file<span class="String">}</span></span><span class="String">&quot;</span></span>
<span class="line-numbers">   5 </span>     metadata <span class="Operator">=</span> <span class="BuiltInConstant">nil</span>
<span class="line-numbers">   6 </span>     
<span class="line-numbers">   7 </span>     <span class="LibraryClassType">Open3</span>.<span class="FunctionName">popen3</span>(command) <span class="Keyword">do </span>|<span class="Variable">input</span>, <span class="Variable">output</span>, <span class="Variable">error</span>|
<span class="line-numbers">   8 </span>       metadata <span class="Operator">=</span> output.<span class="FunctionName">readlines</span>.<span class="FunctionName">grep</span>(<span class="String"><span class="String">/</span></span><span class="String">ID_.*</span><span class="String"><span class="String">/</span></span>).<span class="FunctionName">collect</span> {|<span class="Variable">name</span>| name[<span class="Number">3</span>..name.<span class="FunctionName">length</span>].<span class="FunctionName">split</span>(<span class="String"><span class="String">'</span>=<span class="String">'</span></span>)}
<span class="line-numbers">   9 </span>       metadata <span class="Operator">=</span> metadata.<span class="FunctionName">inject</span>({}) {|<span class="Variable">hash</span>, (<span class="Variable">key</span>, <span class="Variable">value</span>)| hash[key.<span class="FunctionName">to_sym</span>] <span class="Operator">=</span> value.<span class="FunctionName">chomp</span>; hash}
<span class="line-numbers">  10 </span>     <span class="Keyword">end</span>
<span class="line-numbers">  11 </span>     
<span class="line-numbers">  12 </span>     <span class="Keyword">return</span> metadata
<span class="line-numbers">  13 </span>   <span class="Keyword">end</span>
<span class="line-numbers">  14 </span>   
<span class="line-numbers">  15 </span> <span class="Keyword">end</span>
</pre></p>


	<p>Then in an RSpec test do:
<pre class="active4d"><span class="line-numbers">   1 </span> it <span class="String"><span class="String">&quot;</span>should support 3gp<span class="String">&quot;</span></span> <span class="Keyword">do</span>
<span class="line-numbers">   2 </span>     metadata <span class="Operator">=</span> <span class="LibraryClassType">Metadata</span>.<span class="FunctionName">get</span>(<span class="String"><span class="String">&quot;</span>videos/sony_ericsson_k850i.3gp<span class="String">&quot;</span></span>)
<span class="line-numbers">   3 </span>     
<span class="line-numbers">   4 </span>     metadata[<span class="UserDefinedConstant"><span class="UserDefinedConstant">:</span>DEMUXER</span>].<span class="FunctionName">should</span> <span class="Operator">==</span> <span class="String"><span class="String">'</span>mov<span class="String">'</span></span>
<span class="line-numbers">   5 </span>     metadata[<span class="UserDefinedConstant"><span class="UserDefinedConstant">:</span>VIDEO_FORMAT</span>].<span class="FunctionName">should</span> <span class="Operator">==</span> <span class="String"><span class="String">'</span>s263<span class="String">'</span></span>
<span class="line-numbers">   6 </span>     metadata[<span class="UserDefinedConstant"><span class="UserDefinedConstant">:</span>VIDEO_WIDTH</span>].<span class="FunctionName">should</span> <span class="Operator">==</span> <span class="String"><span class="String">'</span>176<span class="String">'</span></span>
<span class="line-numbers">   7 </span>     metadata[<span class="UserDefinedConstant"><span class="UserDefinedConstant">:</span>VIDEO_HEIGHT</span>].<span class="FunctionName">should</span> <span class="Operator">==</span> <span class="String"><span class="String">'</span>144<span class="String">'</span></span>
<span class="line-numbers">   8 </span>   <span class="Keyword">end</span>
</pre></p>


	<p>Note to self: metadata.demuxer would perhaps be a slightly better syntax.</p>
  </div>

  <div style="font-size: 0.8em;margin:0.5em;">
    
      Tagged <a href="/tags/346-mplayer">mplayer</a>, <a href="/tags/8-ruby">ruby</a>, <a href="/tags/27-metadata">metadata</a>, <a href="/tags/21-flv">flv</a>, <a href="/tags/22-movie">movie</a>, <a href="/tags/347-video">video</a>
    
    
  </div>
</div>



  
<div id="snippet_132" class="snippet">
  <h2><a href="/snippets/132-How-to-install-ffmpeg-mencoder-and-flvtool2-on-Mac-OSX-Leopard-convert-an-AVI-to-FLV-and-view-the-FLV-video-with-FlowPlayer" title="How to install ffmpeg, mencoder and flvtool2 on Mac OSX Leopard, convert an AVI to FLV, and view the FLV video with FlowPlayer - Shell Script (Bash) - ffmpeg, osx, leopard, flvtool2, flv, mencoder, h.264">How to install ffmpeg, mencoder and flvtool2 on Mac OSX Leopard, convert an AVI to FLV, and view the FLV video with FlowPlayer</a></h2>
  <div class="details">
    <a style="background-color: #FFFF94;" href="http://snippets.aktagon.com/languages/131-Shell-Script-Bash-">
      Shell Script (Bash)</a> posted over 2 years ago by christian
          
  </div>

  <div class="body">
    <p>This is a short tutorial on how to quickly get up to speed with  FLV  movie creation (transcoding) and viewing (Flash player).</p>


	<p>I tried to install ffmpeg using ports and fink, but had problems. The instructions I found <a href="http://www.macosxhints.com/article.php?story=20061220082125312">here</a> work though.</p>


	<p>The following is my adaptation of the instruction, where I show you how to install both ffmpeg and flvtool2.</p>


	<p>First, download ffmpegx, which contains ffmpeg from <a href="http://ffmpegx.com/download.html">http://ffmpegx.com/download.html</a></p>


	<p>Then, mount the .dmg file, navigate to the mounted disk, and control-click on the ffmpegX application (ffmpegX.app), choose Show Package Contents from the pop-up menu, and copy the ffmpeg binary to, for example, /tmp.</p>


	<p>Now execute the following commands in a terminal shell:
<pre class="active4d"><span class="line-numbers">   1 </span> <span class="LineComment"><span class="LineComment">#</span> Move ffmpeg to /usr/local/bin:</span>
<span class="line-numbers">   2 </span> sudo mv /tmp/ffmpeg /usr/local/bin/
<span class="line-numbers">   3 </span> 
<span class="line-numbers">   4 </span> <span class="LineComment"><span class="LineComment">#</span> Change owner and make executable</span>
<span class="line-numbers">   5 </span> sudo chown root:wheel /usr/local/bin/ffmpeg
<span class="line-numbers">   6 </span> sudo chmod 755 /usr/local/bin/ffmpeg
</pre></p>


	<p>You&#8217;re now ready to use ffmpeg; the following command lists all the supported input and output formats:</p>


	<p><pre class="active4d"><span class="line-numbers">   1 </span> ffmpeg -formats
</pre></p>


	<p>Use the following command to transcode an  AVI  to  FLV  (Flash video) format:
<pre class="active4d"><span class="line-numbers">   1 </span> fmpeg -i mymovie.avi -s 320x240 -ar 44100 -r 12 mymovie.flv
<span class="line-numbers">   2 </span> cat mymovie.flv <span class="Operator">|</span> flvtool2 -U stdin mymovie.flv
</pre></p>


	<p>To add metadata to the  FLV  file&mdash;such as video length, which is required for the Flash player progress bar&mdash;you need to install <a href="http://rubyforge.org/projects/flvtool2/">flvtool2</a>.</p>


	<p>This is done with the following command (note this requires Ruby and RubyGems, which are not part of all  OSX  Leopard installations):
<pre class="active4d"><span class="line-numbers">   1 </span> sudo gem install flvtool2
</pre></p>


	<p>Now add the metadata to the  FLV  file:
<pre class="active4d"><span class="line-numbers">   1 </span> flvtool2 -UP mymovie.flv
</pre></p>


	<p>To test the  FLV  movie you just created, download and extract <a href="http://flowplayer.org">FlowPlayer</a>  to your folder of choice. Now create a page that loads your  FLV  movie; note that you need to update the paths:
<pre class="active4d"><span class="line-numbers">   1 </span> <span class="Operator">&lt;</span>html<span class="Operator">&gt;</span>
<span class="line-numbers">   2 </span> 	<span class="Operator">&lt;</span>head<span class="Operator">&gt;</span>
<span class="line-numbers">   3 </span> 		<span class="Operator">&lt;</span>script type=<span class="String"><span class="String">&quot;</span>text/javascript<span class="String">&quot;</span></span> src=<span class="String"><span class="String">&quot;</span>../flowplayer/html/swfobject.js<span class="String">&quot;</span></span><span class="Operator">&gt;</span><span class="Operator">&lt;</span>/script<span class="Operator">&gt;</span>
<span class="line-numbers">   4 </span> 	<span class="Operator">&lt;</span>/head<span class="Operator">&gt;</span>
<span class="line-numbers">   5 </span> <span class="Operator">&lt;</span>body<span class="Operator">&gt;</span>
<span class="line-numbers">   6 </span> 	<span class="Operator">&lt;</span>div id=<span class="String"><span class="String">&quot;</span>flowplayerholder<span class="String">&quot;</span></span><span class="Operator">&gt;</span>
<span class="line-numbers">   7 </span> 		This will be replaced by the player. 
<span class="line-numbers">   8 </span> 	<span class="Operator">&lt;</span>/div<span class="Operator">&gt;</span>
<span class="line-numbers">   9 </span> 	
<span class="line-numbers">  10 </span> <span class="Operator">&lt;</span>script type=<span class="String"><span class="String">&quot;</span>text/javascript<span class="String">&quot;</span></span><span class="Operator">&gt;</span>
<span class="line-numbers">  11 </span> // <span class="Operator">&lt;</span><span class="Operator">!</span>[CDATA[
<span class="line-numbers">  12 </span> var fo = new SWFObject(<span class="String"><span class="String">&quot;</span>../flowplayer/FlowPlayerDark.swf<span class="String">&quot;</span></span>, <span class="String"><span class="String">&quot;</span>FlowPlayer<span class="String">&quot;</span></span>, <span class="String"><span class="String">&quot;</span>468<span class="String">&quot;</span></span>, <span class="String"><span class="String">&quot;</span>350<span class="String">&quot;</span></span>, <span class="String"><span class="String">&quot;</span>7<span class="String">&quot;</span></span>, <span class="String"><span class="String">&quot;</span>#000000<span class="String">&quot;</span></span>, true)<span class="Operator">;</span>
<span class="line-numbers">  13 </span> 	// need this next line <span class="Keyword">for</span> local testing, it<span class="String"><span class="String">'</span>s optional if your swf is on the same domain as your html page</span>
<span class="line-numbers">  14 </span> <span class="String">	fo.addParam(&quot;allowScriptAccess&quot;, &quot;always&quot;);</span>
<span class="line-numbers">  15 </span> <span class="String">	fo.addVariable(&quot;config&quot;, &quot;{ countryCode: <span class="String">'</span></span><span class="Keyword">fi</span><span class="String"><span class="String">'</span>, playList: [ {overlayId: <span class="String">'</span></span>play<span class="String"><span class="String">'</span> }, { url: <span class="String">'</span></span>/mockup/mymovie.flv<span class="String"><span class="String">'</span> } ], initialScale: <span class="String">'</span></span>scale<span class="String"><span class="String">'</span>,  fullScreenScriptURL: <span class="String">'</span></span>fullscreen.js<span class="String"><span class="String">'</span> }&quot;);</span>
<span class="line-numbers">  16 </span> <span class="String">	fo.write(&quot;flowplayerholder&quot;);</span>
<span class="line-numbers">  17 </span> <span class="String">// ]]&gt;</span>
<span class="line-numbers">  18 </span> <span class="String">&lt;/script&gt;</span>
<span class="line-numbers">  19 </span> <span class="String">&lt;/body&gt;</span>
<span class="line-numbers">  20 </span> <span class="String">&lt;/html&gt;</span>
</pre></p>


	<p>I&#8217;ve <a href="http://www.macosxhints.com/article.php?story=20060923203722112&#38;query=dec">been told</a> that <a href="http://www2.mplayerhq.hu/MPlayer/releases/">mencoder</a> produces higher quality  FLV  videos, so lets compare the mencoder output to ffmpeg by first downloading and extracting mencoder to /tmp.</p>


	<p>Now move mencoder to /usr/local/bin:</p>


	<p><pre class="active4d"><span class="line-numbers">   1 </span> sudo mv /tmp/mencoder /usr/local/bin/
<span class="line-numbers">   2 </span> 
<span class="line-numbers">   3 </span> sudo chown root:wheel /usr/local/bin/mencoder
<span class="line-numbers">   4 </span> sudo chmod 755 /usr/local/bin/mencoder
</pre></p>


	<p>Next transcode the video to  FLV  format using mencoder:
<pre class="active4d"><span class="line-numbers">   1 </span> mencoder mymovie.avi -ofps 12 -o mymovie.flv -of lavf -lavfopts \
<span class="line-numbers">   2 </span> i_certify_that_my_video_stream_does_not_use_b_frames -oac lavc -lavcopts \
<span class="line-numbers">   3 </span> acodec=mp3:abitrate=32 -srate 22050  -ovc lavc -lavcopts vcodec=flv:\
<span class="line-numbers">   4 </span> vbitrate=100:mbd=2:mv0:trell:v4mv:cbp:last_pred=3:predia=2:dia=2:\
<span class="line-numbers">   5 </span> vmax_b_frames=0:vb_strategy=1:precmp=2:cmp=2:subcmp=2:preme=2:qns=2 \
<span class="line-numbers">   6 </span> -vop scale=360:240
</pre></p>


	<p>View the video in the Flash player; you should notice the difference in quality&#8230;</p>


	<p>For even higher quality encode the videos using the H.264 format, which FlowPlayer also supports; instructions can be found <a href="http://www.brad-x.com/2007/05/19/yet-another-linuxnix-video-h264-howto/">here</a>.</p>
  </div>

  <div style="font-size: 0.8em;margin:0.5em;">
    
      Tagged <a href="/tags/24-ffmpeg">ffmpeg</a>, <a href="/tags/198-osx">osx</a>, <a href="/tags/337-leopard">leopard</a>, <a href="/tags/28-flvtool2">flvtool2</a>, <a href="/tags/21-flv">flv</a>, <a href="/tags/338-mencoder">mencoder</a>, <a href="/tags/339-h-264">h.264</a>
    
    
  </div>
</div>



  
<div id="snippet_6" class="snippet">
  <h2><a href="/snippets/6-Add-duration-metadata-into-flv-movie" title="Add duration metadata into flv movie - Shell Script (Bash) - flv, movie, flash, duration, metadata, flvtool2">Add duration metadata into flv movie</a></h2>
  <div class="details">
    <a style="background-color: #FFFF94;" href="http://snippets.aktagon.com/languages/131-Shell-Script-Bash-">
      Shell Script (Bash)</a> posted over 2 years ago by marko
          
  </div>

  <div class="body">
    <p>By default an flv movie doesn&#8217;t contain the duration metadata. Using the flvtool2 program it is injected like this into the movie file.
<pre class="active4d"><span class="line-numbers">   1 </span> cat mymovie.flv <span class="Operator">|</span> flvtool2 -U stdin mymovie.flv
</pre></p>
  </div>

  <div style="font-size: 0.8em;margin:0.5em;">
    
      Tagged <a href="/tags/21-flv">flv</a>, <a href="/tags/22-movie">movie</a>, <a href="/tags/25-flash">flash</a>, <a href="/tags/26-duration">duration</a>, <a href="/tags/27-metadata">metadata</a>, <a href="/tags/28-flvtool2">flvtool2</a>
    
    
  </div>
</div>



  
<div id="snippet_5" class="snippet">
  <h2><a href="/snippets/5-Create-an-flv-movie" title="Create an flv movie - Shell Script (Bash) - flv, movie, audio, ffmpeg, flash">Create an flv movie</a></h2>
  <div class="details">
    <a style="background-color: #FFFF94;" href="http://snippets.aktagon.com/languages/131-Shell-Script-Bash-">
      Shell Script (Bash)</a> posted over 2 years ago by marko
          
  </div>

  <div class="body">
    <p>Create an flv movie that is 320 wide by 240 high. For audio functionality ffmpeg needs to be compiled with the following parameters:</p>


	<p><pre class="active4d"><span class="line-numbers">   1 </span> --enable-amr_nb 
<span class="line-numbers">   2 </span> --enable-amr_nb-fixed 
<span class="line-numbers">   3 </span> --enable-amr_wb 
<span class="line-numbers">   4 </span> --enable-mp3lame 
</pre></p>


	<p>The snippet itself:
<pre class="active4d"><span class="line-numbers">   1 </span> ffmpeg -i mymovie.avi -s 320x240 -ar 44100 -r 12 mymovie.flv
</pre></p>
  </div>

  <div style="font-size: 0.8em;margin:0.5em;">
    
      Tagged <a href="/tags/21-flv">flv</a>, <a href="/tags/22-movie">movie</a>, <a href="/tags/23-audio">audio</a>, <a href="/tags/24-ffmpeg">ffmpeg</a>, <a href="/tags/25-flash">flash</a>
    
    
  </div>
</div>






