<?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>Diego's Rants &#187; Video Encoding</title>
	<atom:link href="http://blog.massanti.com/category/video-encoding/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.massanti.com</link>
	<description>Confessions from a Geek</description>
	<lastBuildDate>Wed, 25 Aug 2010 09:39:58 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>I have bought an EasyCAP</title>
		<link>http://blog.massanti.com/2008/10/21/i-have-bought-an-easycap/</link>
		<comments>http://blog.massanti.com/2008/10/21/i-have-bought-an-easycap/#comments</comments>
		<pubDate>Tue, 21 Oct 2008 21:41:01 +0000</pubDate>
		<dc:creator>Diego Massanti</dc:creator>
				<category><![CDATA[Computers & Tech]]></category>
		<category><![CDATA[Video Encoding]]></category>
		<category><![CDATA[easycap]]></category>
		<category><![CDATA[video]]></category>

		<guid isPermaLink="false">http://blog.massanti.com/?p=867</guid>
		<description><![CDATA[So i have a lot of family videos on several analog media (VHS-C, Super VHS, Hi-8 and 8mm) that i want to finally convert to something not so vulnerable to aging, DVD being an option but Blu-Ray saying &#8220;me! me!&#8221; on my head. Even the fact that I have several workstations with professional level video [...]]]></description>
			<content:encoded><![CDATA[<p><a rel="lightbox" href="http://blog.massanti.com/wp-content/uploads/2008/10/easycap003-1.jpg"><img class="size-medium wp-image-868 alignright" title="EasyCap" src="http://blog.massanti.com/wp-content/uploads/2008/10/easycap003-1-300x240.jpg" alt="" width="300" height="240" /></a>So i have a lot of family videos on several analog media (VHS-C, Super VHS, Hi-8 and 8mm) that i want to finally convert to something not so vulnerable to aging, DVD being an option but Blu-Ray saying &#8220;me! me!&#8221; on my head.</p>
<p>Even the fact that I have several workstations with professional level video capture devices, those machines are usually working doing renders or any other thing and is not that I can waste days capturing all that media there, so <a href="http://cgi.ebay.com/ws/eBayISAPI.dll?ViewItem&amp;ssPageName=STRK:MEWNX:IT&amp;item=270286913609">i decided to buy one of those EasyCap devices</a> on eBay and give it a try.</p>
<p>The specifications are pretty outstanding for what it is, and specially, for the $15 price&#8230; AND it is compatible with Mac OS (by using the non-free <a href="http://www.echofx.com/videoglide.html">VideoGlide</a> app) which makes it even better.</p>
<p>Just some highlights:</p>
<ul>
<li>Complies with Universal Serial Bus Specification Rev 2.0</li>
<li>Supports NTSC, PAL (B), Video format</li>
<li>Video input: RCA composite or S-Video</li>
<li>Audio input: Stereo audio (RCA)</li>
<li>Dimension(Main Body): 8.8cm x 2.8cm x 1.8cm typical credit card size is 8.5cmx5.4cm)</li>
<li>Powers by USB </li>
<li>Supports high quality video resolution for </li>
<li>NTSC: 720 x 480 @30fps</li>
<li>PAL: 720 x 576 @25fps</li>
</ul>
<p><strong>Im still waiting</strong> for it to arrive, and i will post several video examples and personal impressions as soon as i get it.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.massanti.com/2008/10/21/i-have-bought-an-easycap/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>iPhone and iPod Touch compatible video encoding, server side.</title>
		<link>http://blog.massanti.com/2008/10/12/iphone-and-ipod-touch-compatible-server-side-encoding/</link>
		<comments>http://blog.massanti.com/2008/10/12/iphone-and-ipod-touch-compatible-server-side-encoding/#comments</comments>
		<pubDate>Sun, 12 Oct 2008 06:50:51 +0000</pubDate>
		<dc:creator>Diego Massanti</dc:creator>
				<category><![CDATA[Video Encoding]]></category>
		<category><![CDATA[AppleTV]]></category>
		<category><![CDATA[encoding]]></category>
		<category><![CDATA[iPhone]]></category>
		<category><![CDATA[iPod]]></category>
		<category><![CDATA[mkmp4]]></category>
		<category><![CDATA[video]]></category>

		<guid isPermaLink="false">http://blog.massanti.com/?p=852</guid>
		<description><![CDATA[Some days ago, I published a tutorial with some bash scripts to automate the whole process of encoding MP4 files with H264 video and HE-AAC audio which are playable by flash players and the like. Since I&#8217;m working on a really big project which involves a &#8220;youtube like&#8221; behavior, i though it would be cool [...]]]></description>
			<content:encoded><![CDATA[<p><a rel="lightbox" href="http://blog.massanti.com/wp-content/uploads/2008/10/iphone_massanti.png"><img class="size-medium wp-image-861 alignright" title="iPhone screenshot" src="http://blog.massanti.com/wp-content/uploads/2008/10/iphone_massanti-161x300.png" alt="" width="97" height="180" /></a>Some days ago, I <a href="http://blog.massanti.com/2008/09/26/mkmp4-automated-h264-aacplus-encoder-script-mac-linux/">published a tutorial with some bash scripts to automate the whole process of encoding MP4 files with H264 video and HE-AAC audio</a> which are playable by flash players and the like.</p>
<p>Since I&#8217;m working on a really big project which involves a &#8220;youtube like&#8221; behavior, i though it would be cool to also create iPhone / iPod compatible streams (I&#8217;m amazed at the fact that 3% of the people coming to this site is using iPod&#8217;s or iPhone&#8217;s)&#8230; so, I did some modifications to the original script for it to generate the compatible streams.</p>
<p>Now, how am i dealing with this&#8230; well&#8230; let me explain you:</p>
<p>The <a href="http://www.apple.com/iphone/specs.html">iPhone specification</a> states:</p>
<blockquote><p>Video formats supported: H.264 video, up to 1.5 Mbps, 640 by 480 pixels, 30 frames per second, Low-Complexity version of the H.264 Baseline Profile with AAC-LC audio up to 160 Kbps, 48kHz, stereo audio in .m4v, .mp4, and .mov file formats; H.264 video, up to 2.5 Mbps, 640 by 480 pixels, 30 frames per second, Baseline Profile up to Level 3.0 with AAC-LC audio up to 160 Kbps, 48kHz, stereo audio in .m4v, .mp4, and .mov file formats; MPEG-4 video, up to 2.5 Mbps, 640 by 480 pixels, 30 frames per second, Simple Profile with AAC-LC audio up to 160 Kbps, 48kHz, stereo audio in .m4v, .mp4, and .mov file formats</p></blockquote>
<p>This means that the high profile streams that I am generating with the MKMP4 script won&#8217;t play, simple as that. So i have 2 possible approachs:</p>
<ol>
<li>I could just use a single low profile stream for the iPhone and iPod, and since the stream is H264 and low complexity AAC audio, it will just play fine on the flash players too.</li>
<li>I could have 2 different files, one for the flash players (making use of b-frames, entropy coding, and a high profile stream, which substantially improves the final video quality).</li>
</ol>
<p>I decided to use the option 2, mostly because i want to give desktop users as much quality as possible, and i want to use the HE-AAC encoder to save some extra bandwidth that i can instead use for the video stream, so I am using 2 different files, one for desktop users, and a different one for iPhone or iPod users.</p>
<p><span id="more-852"></span>To check if the user is visiting the site with a desktop browser or a Mobile Safari version is really simple: Let&#8217;s consider a PHP based scenario.</p>
<p>The first thing that i had to do is to create a simple function to check the browser&#8217;s user-agent string.</p>
<pre class="brush: php;">function isIphone() {
$agent = $_SERVER['HTTP_USER_AGENT'];
if (stristr($agent, 'iPhone') == TRUE || stristr($agent, 'iPod') == TRUE) {
echo '&lt;!--iPhone or iPod Touch detected--&gt;;';
return TRUE;
} else {
echo '&lt;!--iPhone or iPod Touch not detected--&gt;';
return FALSE;
}
}</pre>
<p>This function is self explanatory for anybody with basic PHP skills, basically it checks the user-agent string and returns TRUE if it contains the words &#8220;iPhone&#8221; or &#8220;iPod&#8221;.</p>
<p>So, then in your template you could make something like this:</p>
<pre class="brush: php;">if (!isIphone()) {
// Code to display the standard high profile stream goes here.
} else {
// Code to display the iPhone compatible stream goes here.
}</pre>
<p>Pretty simple.</p>
<h2>The updated MKMP4 script.</h2>
<p>This is an updated version of my MKMP4 encoding script, basically, it is almost the same thing than <a href="http://blog.massanti.com/2008/09/26/mkmp4-automated-h264-aacplus-encoder-script-mac-linux/">the original one published here</a>, with the addition of an extra -i switch that you can use if you want to create an iPhone compatible stream.</p>
<blockquote class="warning">
<h3>Important: FAAC encoder is a requeriment for this!</h3>
<p>This version of the MKMP4 script uses the FAAC encoder that you can download <a title="FAAC download page" href="http://www.audiocoding.com/downloads.html">by going here</a>. Be sure that you have correctly compiled and installed it BEFORE trying to use this script on your desktop or server.</p></blockquote>
<pre class="brush: bash;">#! /bin/bash

# This bash script has been created by Diego Massanti
# www.massanti.com
#
# set -x # uncomment this for debugging
shopt -s nullglob # so the glob expands to nothing when there are no .mov files.

# User settings:

fileext=&quot;.mov&quot; # Set this variable to the video file extension that you want to search for when encoding whole directories.
onlythumbs=FALSE # IF you set this to TRUE, the script will only generate thumbnails.
# End user settings
# You should not change anything below this line unless you know what you are doing.

usage()
{
cat &lt;&lt; EOF
usage: $0 -f  [-w ] -b  [-q][-i][-k][-t][-o ]

* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* h264 video with he-aac audio encoding script by Diego Massanti. *
*                 October 2008,  Made in Argentina.               *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *

OPTIONS:
 -h    Show this message
 -f    Path to the file to encode &lt; REQUIRED
 -w    Resize video to fit inside this width while keeping the aspect ratio &lt; OPTIONAL
 -b    Desired video bitrate &lt; REQUIRED
 -o    Output directory for compressed media &lt; OPTIONAL (will use same as source if ommited)
 -q     Better quality encoding using 2 passes (slower) &lt; OPTIONAL
 -k    Do not delete (keep) temporary files &lt; OPTIONAL
EOF
}
audiobitrate=48000
platform=&quot;&quot;
uname=`uname`
if [ $uname == &quot;Darwin&quot; ]; then
 ## 99% of chances that this is a Mac
 platform=&quot;Mac&quot;
else
 platform=&quot;Linux&quot;
fi

width=&quot;&quot;
bitrate=
deltemp=TRUE
quality=FALSE
iphone=FALSE
poster=FALSE
filename=&quot;&quot;
rsize=&quot;&quot;
outdir=`pwd`
outFileName=&quot;&quot;
itemVideoBitrate=&quot;&quot;

while getopts &quot;:f:w:o:b:qktvi&quot; OPTION; do
 case $OPTION in
 w ) width=$OPTARG;;
 f ) filename=$OPTARG;;
 b ) bitrate=$OPTARG;;
 k ) deltemp=FALSE;;
 q ) quality=TRUE;;
 t ) poster=TRUE;;
 i ) iphone=TRUE;;
 o ) outdir=`cd &quot;$OPTARG&quot;; pwd`;;
 h ) usage;;
 \? ) usage
 exit 1;;
 * ) echo $usage
 exit 1;;
 esac
done

if [ &quot;$1&quot; == &quot;&quot; ]; then
 usage
 exit 0
fi

if [ &quot;$filename&quot; == &quot;&quot; ]; then
 echo
 echo you MUST supply a file to encode!, use the -f parameter. i.e: -f mymovie.avi
 echo
 usage
 exit 1
fi
if [ &quot;$bitrate&quot; == &quot;&quot; ]; then
 echo
 echo you MUST specify a target bitrate!, use the -b parameter. i.e: -b 512
 echo
 usage
 exit 1
fi

if [ &quot;$width&quot; != &quot;&quot; ]; then
 rsize=&quot;-vf scale=$width:-3&quot;
 rsizemsg=&quot;fit into $width pixels wide&quot;
else
 rsize=&quot;&quot;
 rsizemsg=&quot;Movie is not being resized.&quot;
fi

# Check if output directory exists
if [ -d &quot;$outdir&quot; ]; then
 echo &quot;Target directory set to: $outdir&quot;

else
 echo &quot;Error: '$outdir' does not exist!!&quot;
 echo &quot;Can not continue, please make sure that the specified output directory exists. Exiting...&quot;
 exit 1
fi

# Start the magic...

showMessage() {
 echo &quot;MKMP4 : &quot;$1&quot;&quot;
}

initialize() {
 if [ -d &quot;$filename&quot; ]; then
 $filename = `cd $filename; pwd`
 showMessage &quot;Output directory set to: $outdir.&quot;
 showMessage &quot;Triggering the encoder with all media files in directory: $filename.&quot;
 for file in &quot;$filename&quot;*$fileext; do
 if [ -f &quot;$file&quot; ]; then
 showMessage &quot;Will encode file: $file&quot;
 startEncoders &quot;$file&quot;

 fi
 done
 elif [ -f &quot;$filename&quot; ]; then
 showMessage &quot;Triggering the encoder with source media: $filename.&quot;
 startEncoders &quot;$filename&quot;

 else
 # input is no file and no directory ? WTF ?
 echo &quot;Something really weird has happened here: $filename is not valid, exiting...&quot;
 exit 1
 fi
}

startEncoders() {
 if [ $onlythumbs == &quot;FALSE&quot; ]; then
 firstPass &quot;$1&quot;
 if [ &quot;$quality&quot; == &quot;TRUE&quot; ]; then
 secondPass &quot;$1&quot;
 fi
 extractAudio &quot;$1&quot;
 encodeAAC &quot;$1&quot;
 mux &quot;$1&quot;
 fi
 if [ $poster == &quot;TRUE&quot; ]; then
 createThumbnail &quot;$1&quot;
 fi
 if [ $deltemp == &quot;TRUE&quot; ]; then
 cleanTemporaryFiles &quot;$1&quot;
 fi
}
firstPass() {
 local iFile=&quot;${1##*/}&quot;    # remove directory and keep only file name.
 # First, lets get some basical information about this file, as FPS.
 MOVIE_FPS=`midentify &quot;$1&quot; | grep FPS | cut -d = -f 2`
 MOVIE_LENGTH = `midentify &quot;$1&quot; | grep &quot;ID_LENGTH&quot; | cut -d = -f 2 | cut -d . -f 1`
 let &quot;THUMB_SECONDS = $MOVIE_LENGTH / 2&quot;
 # Lets print some info to stdout.
 showMessage &quot;Encoding: $1&quot;
 showMessage &quot;Resizing to: $rsizemsg.&quot;
 showMessage &quot;Total Bitrate: $bitrate kbps.&quot;
 let &quot;caudiobitrate = $audiobitrate / 1000&quot;
 let &quot;itemVideoBitrate = $bitrate - $caudiobitrate&quot;
 showMessage &quot;Video Bitrate: $itemVideoBitrate kbps.&quot;
 showMessage &quot;Audio Bitrate: $caudiobitrate kbps.&quot;
 showMessage &quot;Platform: $platform.&quot;
 # Lets start the encoding for the first pass.
 showMessage &quot;Starting video encoding pass 1...&quot;
 if [ $iphone == &quot;TRUE&quot; ]; then
 mencoder &quot;$1&quot; -o &quot;$outdir/${iFile%%.*}_temp.264&quot; -demuxer lavf -passlogfile &quot;$outdir/${iFile%%.*}&quot;_temp.log $rsize -ssf lgb=0.2 -ovc x264 -x264encopts bitrate=$itemVideoBitrate:frameref=1:nocabac:bframes=0:b_adapt:b_pyramid:weight_b:partitions=all:me=umh:subq=6:trellis=2:brdo:threads=auto:pass=1:analyse=all -of rawvideo -nosound
 else
 mencoder &quot;$1&quot; -o &quot;$outdir/${iFile%%.*}_temp.264&quot; -demuxer lavf -passlogfile &quot;$outdir/${iFile%%.*}&quot;_temp.log $rsize -ssf lgb=0.2 -ovc x264 -x264encopts bitrate=$itemVideoBitrate:frameref=8:bframes=3:b_adapt:b_pyramid:weight_b:partitions=all:8x8dct:me=umh:subq=6:trellis=2:brdo:threads=auto:pass=1:analyse=all -of rawvideo -nosound
 fi
}

secondPass() {
 local iFile=&quot;${1##*/}&quot;    # remove directory and keep only file name.
 showMessage &quot;Starting video encoding pass 2...&quot;
 if [ $iphone == &quot;TRUE&quot; ]; then
 mencoder &quot;$1&quot; -o &quot;$outdir/${iFile%%.*}_temp.264&quot; -demuxer lavf -passlogfile &quot;$outdir/${iFile%%.*}&quot;_temp.log $rsize -ssf lgb=0.2 -ovc x264 -x264encopts bitrate=$itemVideoBitrate:frameref=1:nocabac:bframes=0:b_adapt:b_pyramid:weight_b:partitions=all:me=umh:subq=6:trellis=2:brdo:threads=auto:pass=2:analyse=all -of rawvideo -nosound
 else
 mencoder &quot;$1&quot; -o &quot;$outdir/${iFile%%.*}_temp.264&quot; -demuxer lavf -passlogfile &quot;$outdir/${iFile%%.*}&quot;_temp.log $rsize -ssf lgb=0.2 -ovc x264 -x264encopts bitrate=$itemVideoBitrate:frameref=8:bframes=3:b_adapt:b_pyramid:weight_b:partitions=all:8x8dct:me=umh:subq=6:trellis=2:brdo:threads=auto:pass=2:analyse=all -of rawvideo -nosound
 fi
}

extractAudio() {
 local iFile=&quot;${1##*/}&quot;    # remove directory and keep only file name.
 showMessage &quot;Extracting Audio...&quot;
 if [ $iphone == &quot;TRUE&quot; ]; then
 mplayer &quot;$1&quot; -af resample=48000:0:2,channels=2,volnorm=1:0.25,format=s16le -ao pcm:file=&quot;$outdir/${iFile%.*}_temp.wav&quot; -vc dummy -vo null
 else
 mplayer &quot;$1&quot; -af resample=48000:0:2,channels=2,volnorm=1:0.25,format=s16le -ao pcm:file=&quot;$outdir/${iFile%.*}_temp.wav&quot; -vc dummy -vo null
 fi

}

encodeAAC() {
 local iFile=&quot;${1##*/}&quot;    # remove directory and keep only file name.
 if [ $iphone == &quot;TRUE&quot; ]; then
 faac &quot;$outdir/${iFile%.*}_temp.wav&quot; -q 100 -o &quot;$outdir/${iFile%.*}_temp.mp4&quot;
 else
 if [ &quot;$platform&quot; == &quot;Mac&quot; ]; then
 enhAacPlusEnc &quot;$outdir/${iFile%.*}_temp.wav&quot; &quot;$outdir/${iFile%.*}_temp.aac&quot; $audiobitrate s
 else
 neroAacEnc -br 48000 -he -if $outdir/${iFile%.*}_temp.wav -of $outdir/${iFile%.*}_temp.mp4
 fi
 fi
}

mux() {
 local iFile=&quot;${1##*/}&quot;    # remove directory and keep only file name.
 MP4Box -add &quot;$outdir/${iFile%.*}_temp.264#video:fps=$MOVIE_FPS&quot; &quot;$outdir/${iFile%.*}.m4v&quot;
 if [ $iphone == &quot;TRUE&quot; ]; then
 MP4Box -add &quot;$outdir/${iFile%.*}_temp.mp4#audio&quot; &quot;$outdir/${iFile%.*}.m4v&quot;
 else
 if [ &quot;$platform&quot; == &quot;Mac&quot; ]; then
 MP4Box -add &quot;$outdir/${iFile%.*}_temp.aac&quot; -sbr &quot;$outdir/${iFile%.*}.m4v&quot;
 else
 MP4Box -add &quot;$outdir/${iFile%.*}_temp.mp4#audio&quot; &quot;$outdir/${iFile%.*}.m4v&quot;
 fi
 fi

 name=${iFile%.*}
 album=&quot;The Mac Video Archive&quot;
 author=&quot;Apple Computer // massanti.com&quot;
 comment=&quot;Professionally encoded by Diego Massanti&quot;
 created=&quot;2008&quot;
 MP4Box -inter 500 -itags album=&quot;$album&quot;:artist=&quot;$author&quot;:comment=&quot;$comment&quot;:created=&quot;$created&quot;:name=&quot;$name&quot; -lang English &quot;$outdir/${iFile%.*}&quot;.m4v
}

createThumbnail() {
 local iFile=&quot;${1##*/}&quot;    # remove directory and keep only file name.
 mplayer &quot;$1&quot; -ss 10 -nosound $rsize -sws 7 -vo jpeg:outdir=$outdir -frames 1
 mv &quot;$outdir/00000001.jpg&quot; &quot;$outdir/${iFile%.*}.jpg&quot;
 mplayer &quot;$1&quot; -ss 10 -nosound -vf scale=150:-3 -sws 7 -vo jpeg:outdir=$outdir -frames 1
 mv &quot;$outdir/00000001.jpg&quot; &quot;$outdir/${iFile%.*}_small.jpg&quot;
}

cleanTemporaryFiles() {
 local iFile=&quot;${1##*/}&quot;    # remove directory and keep only file name.
 showMessage &quot;Removing temporary files...&quot;
 rm &quot;$outdir/${iFile%.*}&quot;_temp*
}

initialize</pre>
<p>The following example command line will encode the video stream in my_video.avi as a low profile H264 without b-frames, will encode the audio track as a Low Complexity AAC stream and will mux everything inside a M4V file which will play on the iPhone, iPod Touch, Apple TV and Quicktime / iTunes.</p>
<pre>./mkmp4 -f my_video.avi -b 512 -w 640 -q -t -i</pre>
<p>For a detailed explanation of all the supported command line switches, you can visit <a href="http://blog.massanti.com/2008/09/26/mkmp4-automated-h264-aacplus-encoder-script-mac-linux/">the original MKMP4 post</a>.</p>
<p>Some final considerations:</p>
<ul>
<li>Due to the fact that I am forced to use a low complexity AAC encoder for the iPhone streams, the audio bandwidth is hardcoded at around 100kbps inside the script, that would give you an audio quality similar to the one you get with the HE-AAC encoder at 48kbps, feel free to experiment with different values for this.</li>
<li>Using a low profile H264 stream gives me iPhone compatibility, but at the expense of losing some video quality, clearly, a 512kbps stream looks much better when we use b-frames and entropy coding so unless you really care about iPhone and iPod users like I do, you should use high profile streams.</li>
<li>If you are visiting this page with an iPhone or iPod and want to see how all this works, feel free to visit <a href="http://blog.massanti.com/apple-video-archive/ipod-itunes/">this section of the Apple Video Archive here</a> to see the magic happening: If you visit the page with a desktop browser, it will simply show the standard flash player, <strong>BUT</strong> when it detects you are on a Mobile Safari platform, it will automatically show you the iPhone compatible stream (<a rel="lightbox" href="http://blog.massanti.com/wp-content/uploads/2008/10/iphone_massanti.png">click here to see how this all looks</a>) and properly embed the quicktime player on the page.</li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://blog.massanti.com/2008/10/12/iphone-and-ipod-touch-compatible-server-side-encoding/feed/</wfw:commentRss>
		<slash:comments>22</slash:comments>
		</item>
		<item>
		<title>MKMP4: An automated H264 / HE-AAC (aacPlus) encoder script for Mac and Linux / Unix</title>
		<link>http://blog.massanti.com/2008/09/26/mkmp4-automated-h264-aacplus-encoder-script-mac-linux/</link>
		<comments>http://blog.massanti.com/2008/09/26/mkmp4-automated-h264-aacplus-encoder-script-mac-linux/#comments</comments>
		<pubDate>Fri, 26 Sep 2008 23:43:30 +0000</pubDate>
		<dc:creator>Diego Massanti</dc:creator>
				<category><![CDATA[Computers & Tech]]></category>
		<category><![CDATA[Video Encoding]]></category>
		<category><![CDATA[aacPlus]]></category>
		<category><![CDATA[encoding]]></category>
		<category><![CDATA[h264]]></category>
		<category><![CDATA[script]]></category>
		<category><![CDATA[video]]></category>

		<guid isPermaLink="false">http://blog.massanti.com/?p=562</guid>
		<description><![CDATA[I have been nothing but amazed about the feedback that i got on my prior tutorials on how to automate the whole chain of free and open source tools to create amazing quality video at really low bitrates (great for flash streaming, etcetera). I usually write such tools and scripts for myself or for clients, [...]]]></description>
			<content:encoded><![CDATA[<p>I have been nothing but amazed about the feedback that i got on my prior tutorials on how to automate the whole chain of free and open source tools to create amazing quality video at really low bitrates (great for flash streaming, etcetera).</p>
<p>I usually write such tools and scripts for myself or for clients, but due to the fact that it seems a lot of people is interested in the process, I&#8217;m releasing here the new version of my encoding tools for the Mac and any Linux / Unix variant.</p>
<h3>Needed tools and applications</h3>
<p>For all this to work, you will need the following things</p>
<ul>
<li>The latest GIT version of the one and only <a href="http://www.videolan.org/developers/x264.html">x264 encoder</a>.</li>
<li>The latest SVN version of <a href="http://www.mplayerhq.hu/design7/dload.html">mplayer</a></li>
<li>The latest version of the <a href="http://gpac.sourceforge.net/home_download.php">GPAC utilities</a></li>
<li>My build of the <a href="http://blog.massanti.com/wp-content/uploads/2008/09/aacplusencoder.zip">3GPP HE-AAC encoder</a> if you are on a Mac, or the <a href="http://www.nero.com/enu/nero-aac-codec.html">Nero Digital Audio Encoder</a> if you are on unix / linux.</li>
</ul>
<blockquote class="mac">
<h3>Extra pre-requisites for Mac OS X</h3>
<p>Mac OS X users need to install the <a title="Mac Developer's Center" href="http://developer.apple.com/mac/">Apple Developer Tools (aka XCode)</a> in order to compile software, also, for building the x264 encoder, you will need to get and install <a href="http://www.tortall.net/projects/yasm/wiki/Download">YASM</a>.</p></blockquote>
<p>Now, you have to build and install all the tools, AND they have to be on any directory in your executable path. M<strong>y scripts assume that all the tools are in your executable path</strong>.<br />
<span id="more-562"></span></p>
<h2>Building x264</h2>
<p>Building x264 is usually a matter of just typing:</p>
<pre>./configure</pre>
<p>in the source directory, and then the usual:</p>
<pre>make</pre>
<p>and</p>
<pre>make install</pre>
<p>If you happen to have any problem with the building process, usually a simple google search with the error string is all you need.</p>
<h2>Building mplayer</h2>
<p>The process of building mplayer is certainly similar, BUT if you plan to use the video thumbnail and poster frame generation on my scripts, you will have to get and compile the jpeg libraries (if you don&#8217;t have them already) BEFORE building mplayer, this should be also a really easy thing to do: go get the libjpeg sources from <a href="http://www.ijg.org/">HERE</a>, and then configure, build and install.</p>
<p>Now, you have to download the latest SVN version of mplayer, assuming that you have the subversion client enabled, you can get the whole source tree by typing:</p>
<pre>svn checkout svn://svn.mplayerhq.hu/mplayer/trunk mplayer</pre>
<p>Now, you need to enable some things at configure time for mplayer and mencoder to work nice with the scripts, so our configure line will look something like this</p>
<pre>./configure --disable-gui --enable-png --enable-jpeg</pre>
<p>Note: there should be no reason at all for the &#8211;enable-jpeg flag, but for some reason that I ignore, mplayer wont detect my libjpeg install if i don&#8217;t force it like that, also, the &#8211;disable-gui option is to save some compile time, since we don&#8217;t plan to use any of the GUI features on mplayer.</p>
<p>Now, its time to build, so type:</p>
<pre>make</pre>
<p>and then, if you get no errors, you are safe to type:</p>
<pre>make install</pre>
<p>At this point you should have a working install, but since we want to be 100% sure that you REALLY have a working install, in any terminal type this:</p>
<pre>mencoder -ovc help</pre>
<p>and you should get something like this (please notice the x264 part, that is fundamental for everything to work)</p>
<pre>Available codecs:

   copy     - frame copy, without re-encoding. Doesn't work with filters.
   frameno  - special audio-only file for 3-pass encoding, see DOCS.
   raw      - uncompressed video. Use fourcc option to set format explicitly.
   lavc     - libavcodec codecs - best quality!
   vfw      - VfW DLLs, read DOCS/HTML/en/encoding-guide.html.
   qtvideo  - QuickTime DLLs, currently only SVQ1/3 are supported.
   x264     - H.264 encoding</pre>
<p>now let&#8217;s verify the mplayer part, again, in any terminal type:</p>
<pre class="pre_terminal">mplayer -vo help</pre>
<p>and you should get something like this (here, notice the PNG and JPEG bits, this is fundamental if you plan to use the thumbnail and poster generation features).</p>
<pre>Available video output drivers:
	macosx	Mac OSX Core Video
	quartz	Mac OSX (Quartz)
	xv	X11/Xv
	x11	X11 ( XImage/Shm )
	xover	General X11 driver for overlay capable video output drivers
	gl	X11 (OpenGL)
	gl2	X11 (OpenGL) - multiple textures version
	null	Null video output
	mpegpes	MPEG-PES file
	yuv4mpeg	yuv4mpeg output for mjpegtools
	png	PNG file
	jpeg	JPEG file
	tga	Targa output
	pnm	PPM/PGM/PGMYUV file
	md5sum	md5sum of each frame</pre>
<blockquote class="warning">
<h3>Very Important!</h3>
<p>After you compile and install mplayer, <strong>you need to copy</strong><strong> the midentify.sh script into any directory inside your executable path</strong>, for example <em>/usr/local/bin</em>. <strong>This is mandatory</strong>, because the <em>MKMP4</em> script will <strong>NOT</strong> be able to correctly identify the source video files without this tool. The <em><strong>midentify</strong></em> script (called <em>midentify.sh</em>) is located inside the <strong>TOOLS</strong> directory in the mplayer source tree.</p></blockquote>
<p>At this point, almost everything is ready, now it is time for the next step.</p>
<h2>Building and configuring the GPAC utilities</h2>
<p>If you are on Linux / Unix, building the GPAC utilities is a really simple process.</p>
<p>Get the latest stable release from <a href="http://gpac.sourceforge.net/home_download.php">here</a>, and then do the usual ./configure, make and make install.</p>
<blockquote class="mac"><p>For Mac OS users there is an extra step that you have to do if you want to be able to build and install the GPAC utilities without headaches, and it involves the editing of a source file in the package.</p>
<p>So, after you have downloaded the sources, open the file <strong>src/utils/os_net.c</strong> , line <strong>78</strong> reads:</p>
<pre>#ifndef __DARWIN__</pre>
<p>right <strong>AFTER</strong> it, add the following:</p>
<pre>typedef unsigned long u_long;</pre>
<p>save the file, and you can now configure, build and install the GPAC utilities (MP4Box) on Mac OS without any problem.</p></blockquote>
<h2>Installing the HE-AAC (aacPlus) audio encoder.</h2>
<p>This is a pretty simple step, if you are on a Mac, <a title="Download the AacPlus Encoder for Mac OS" href="http://blog.massanti.com/wp-content/uploads/2008/09/aacplusencoder.zip">get this encoder that i have built myself from the 3GPP sources</a> and copy it to <strong>/usr/local/bin</strong>, and if you are on linux, get the <a href="http://www.nero.com/enu/nero-aac-codec.html">Nero Digital Audio Encoder</a> from <a href="http://www.nero.com/enu/down-ndaudio.html">HERE</a> and again, copy it to <strong>/usr/local/bin</strong>. You can actually move the encoders to any directory that IS inside your executable path, /usr/local/bin is a good choice though <img src='http://blog.massanti.com/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> .</p>
<h2>Putting it all togheter.</h2>
<p>At this point, you have pretty much everything you would need to encode amazing quality H264 video and aacPlus audio, the only one remaining thing is some tool to put all this togheter in an *easy to use* way, so i have created the following bash script which pretty much simplifies the whole process.</p>
<pre class="brush: bash;">#! /bin/bash

# This bash script has been created by Diego Massanti
# www.massanti.com
#
# set -x # uncomment this for debugging
shopt -s nullglob # so the glob expands to nothing when there are no .mov files.

# User settings:

fileext=&quot;.mov&quot; # Set this variable to the video file extension that you want to search for when encoding whole directories.

# End user settings
# You should not change anything below this line unless you know what you are doing.

usage()
{
cat &lt;&lt; EOF
usage: $0 -f  [-w ] -b  [-q][-k][-t][-o ]

* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* h264 video with he-aac audio encoding script by Diego Massanti. *
*                September 2008, Made in Argentina.               *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *

OPTIONS:
   	-h	Show this message
-f	Path to the file to encode &lt; REQUIRED
   	-w	Resize video to fit inside this width while keeping the aspect ratio &lt; OPTIONAL
   	-b	Desired video bitrate &lt; REQUIRED
-o	Output directory for compressed media &lt; OPTIONAL (will use same as source if ommited)
   	-q 	Better quality encoding using 2 passes (slower) &lt; OPTIONAL
-k	Do not delete (keep) temporary files &lt; OPTIONAL
EOF
}
audiobitrate=48000
platform=&quot;&quot;
uname=`uname`
if [ $uname == &quot;Darwin&quot; ]; then
## 99% of chances that this is a Mac
platform=&quot;Mac&quot;
else
platform=&quot;Linux&quot;
fi

width=&quot;&quot;
bitrate=
deltemp=TRUE
quality=FALSE
poster=FALSE
filename=&quot;&quot;
rsize=&quot;&quot;
outdir=`pwd`
outFileName=&quot;&quot;
itemVideoBitrate=&quot;&quot;

while getopts &quot;:f:w:o:b:qktv&quot; OPTION; do
case $OPTION in
w ) width=$OPTARG;;
f ) filename=$OPTARG;;
b ) bitrate=$OPTARG;;
k ) deltemp=FALSE;;
q ) quality=TRUE;;
t ) poster=TRUE;;
o ) outdir=`cd &quot;$OPTARG&quot;; pwd`;;
h ) usage;;
\? ) usage
exit 1;;
* ) echo $usage
exit 1;;
esac
done

if [ &quot;$1&quot; == &quot;&quot; ]; then
usage
exit 0
fi

if [ &quot;$filename&quot; == &quot;&quot; ]; then
echo
echo you MUST supply a file to encode!, use the -f parameter. i.e: -f mymovie.avi
echo
usage
exit 1
fi
if [ &quot;$bitrate&quot; == &quot;&quot; ]; then
echo
echo you MUST specify a target bitrate!, use the -b parameter. i.e: -b 512
echo
usage
exit 1
fi

if [ &quot;$width&quot; != &quot;&quot; ]; then
rsize=&quot;-vf scale=$width:-3&quot;
rsizemsg=&quot;fit into $width pixels wide&quot;
else
rsize=&quot;&quot;
rsizemsg=&quot;Movie is not being resized.&quot;
fi

# Check if output directory exists
if [ -d &quot;$outdir&quot; ]; then
echo &quot;Target directory set to: $outdir&quot;

else
echo &quot;Error: '$outdir' does not exist!!&quot;
echo &quot;Can not continue, please make sure that the specified output directory exists. Exiting...&quot;
exit 1
fi

# Start the magic...

showMessage() {
echo &quot;MKMP4 : &quot;$1&quot;&quot;
}

initialize() {
if [ -d &quot;$filename&quot; ]; then
$filename = `cd $filename; pwd`
showMessage &quot;Output directory set to: $outdir.&quot;
showMessage &quot;Triggering the encoder with all media files in directory: $filename.&quot;
for file in &quot;$filename&quot;*$fileext; do
if [ -f &quot;$file&quot; ]; then
showMessage &quot;Will encode file: $file&quot;
startEncoders &quot;$file&quot;

fi
done
elif [ -f &quot;$filename&quot; ]; then
showMessage &quot;Triggering the encoder with source media: $filename.&quot;
startEncoders &quot;$filename&quot;

else
# input is no file and no directory ? WTF ?
echo &quot;Something really weird has happened here: $filename is not valid, exiting...&quot;
exit 1
fi
}

startEncoders() {
firstPass &quot;$1&quot;
if [ &quot;$quality&quot; == &quot;TRUE&quot; ]; then
secondPass &quot;$1&quot;
fi
extractAudio &quot;$1&quot;
encodeAAC &quot;$1&quot;
mux &quot;$1&quot;
if [ $poster == &quot;TRUE&quot; ]; then
createThumbnail &quot;$1&quot;
fi
if [ $deltemp == &quot;TRUE&quot; ]; then
cleanTemporaryFiles &quot;$1&quot;
fi
}
firstPass() {
local iFile=&quot;${1##*/}&quot;	# remove directory and keep only file name.
# First, lets get some basical information about this file, as FPS.
MOVIE_FPS=`midentify &quot;$1&quot; | grep FPS | cut -d = -f 2`
# Lets print some info to stdout.
showMessage &quot;Encoding: $1&quot;
showMessage &quot;Resizing to: $rsizemsg.&quot;
showMessage &quot;Total Bitrate: $bitrate kbps.&quot;
let &quot;caudiobitrate = $audiobitrate / 1000&quot;
let &quot;itemVideoBitrate = $bitrate - $caudiobitrate&quot;
showMessage &quot;Video Bitrate: $itemVideoBitrate kbps.&quot;
showMessage &quot;Audio Bitrate: $caudiobitrate kbps.&quot;
showMessage &quot;Platform: $platform.&quot;
# Lets start the encoding for the first pass.
showMessage &quot;Starting video encoding pass 1...&quot;
mencoder &quot;$1&quot; -o &quot;$outdir/${iFile%%.*}_temp.264&quot; -passlogfile &quot;$outdir/${iFile%%.*}&quot;_temp.log $rsize -ovc x264 -x264encopts bitrate=$itemVideoBitrate:frameref=8:bframes=3:b_adapt:b_pyramid:weight_b:partitions=all:8x8dct:me=umh:subq=6:trellis=2:brdo:threads=auto:pass=1:analyse=all -of rawvideo -nosound

}

secondPass() {
local iFile=&quot;${1##*/}&quot;	# remove directory and keep only file name.
showMessage &quot;Starting video encoding pass 2...&quot;
mencoder &quot;$1&quot; -o &quot;$outdir/${iFile%%.*}_temp.264&quot; -passlogfile &quot;$outdir/${iFile%%.*}&quot;_temp.log $rsize -ovc x264 -x264encopts bitrate=$itemVideoBitrate:frameref=8:bframes=3:b_adapt:b_pyramid:weight_b:partitions=all:8x8dct:me=umh:subq=6:trellis=2:brdo:threads=auto:pass=2:analyse=all -of rawvideo -nosound
}

extractAudio() {
local iFile=&quot;${1##*/}&quot;	# remove directory and keep only file name.
showMessage &quot;Extracting Audio...&quot;
mplayer &quot;$1&quot; -af resample=48000:0:2,channels=2,volnorm=1:0.25,format=s16le -ao pcm:file=&quot;$outdir/${iFile%.*}_temp.wav&quot; -vc dummy -vo null
}

encodeAAC() {
local iFile=&quot;${1##*/}&quot;	# remove directory and keep only file name.
if [ &quot;$platform&quot; == &quot;Mac&quot; ]; then
enhAacPlusEnc &quot;$outdir/${iFile%.*}_temp.wav&quot; &quot;$outdir/${iFile%.*}_temp.aac&quot; $audiobitrate s
else
neroAacEnc -br 48000 -he -if $outdir/${iFile%.*}_temp.wav -of $outdir/${iFile%.*}_temp.mp4
fi
}

mux() {
local iFile=&quot;${1##*/}&quot;	# remove directory and keep only file name.
MP4Box -add &quot;$outdir/${iFile%.*}_temp.264#video:fps=$MOVIE_FPS&quot; &quot;$outdir/${iFile%.*}.m4v&quot;
if [ &quot;$platform&quot; == &quot;Mac&quot; ]; then
MP4Box -add &quot;$outdir/${iFile%.*}_temp.aac&quot; -sbr &quot;$outdir/${iFile%.*}.m4v&quot;
else
MP4Box -add &quot;$outdir/${iFile%.*}_temp.mp4#audio&quot; &quot;$outdir/${iFile%.*}.m4v&quot;
fi
name=$outdir/${iFile%.*}
album=&quot;The Mac Video Archive&quot;
author=&quot;Apple Computer // massanti.com&quot;
comment=&quot;Professionally encoded by Diego Massanti&quot;
created=&quot;2007&quot;
MP4Box -inter 500 -itags album=&quot;$album&quot;:artist=&quot;$author&quot;:comment=&quot;$comment&quot;:created=&quot;$created&quot;:name=&quot;$name&quot; -lang English &quot;$outdir/${iFile%.*}&quot;.m4v
}

createThumbnail() {
local iFile=&quot;${1##*/}&quot;	# remove directory and keep only file name.
mplayer &quot;$1&quot; -ss 10 -nosound $rsize -ssf lgb=1.0 -sws 7 -vo jpeg:outdir=$outdir -frames 1
mv &quot;$outdir/00000001.jpg&quot; &quot;$outdir/${iFile%.*}.jpg&quot;
mplayer &quot;$1&quot; -ss 10 -nosound -vf scale=150:-3 -ssf lgb=1.0 -sws 7 -vo jpeg:outdir=$outdir -frames 1
mv &quot;$outdir/00000001.jpg&quot; &quot;$outdir/${iFile%.*}_small.jpg&quot;
}

cleanTemporaryFiles() {
local iFile=&quot;${1##*/}&quot;	# remove directory and keep only file name.
showMessage &quot;Removing temporary files...&quot;
rm &quot;$outdir/${iFile%.*}&quot;_temp*
}

initialize</pre>
<p>You can also download the script by clicking <a title="Download the MKMP4 encoding script" href="http://blog.massanti.com/wp-content/uploads/2008/09/mkmp4.zip">here</a> in order to keep proper code formatting. Installation is really simple, just move it to <strong>/usr/local/bin</strong>, give it execution rights (<strong>chmod +x /usr/local/bin/mkmp4</strong>) and done.</p>
<p>This script is really easy to use and implement, basically the script expects the following <strong>MANDATORY</strong> parameters in the command line:</p>
<ul>
<li>-f to specify the source media <strong>(can be a single file or a whole directory with media files)</strong></li>
<li>-b to specify the target bitrate for the final h264 encoded movie</li>
<li>-w to specify the target width for the final h264 encoded movie</li>
</ul>
<p>So for example, typing:</p>
<pre>mkmp4 -f /path/to/some/video.avi -b 512 -w 640</pre>
<p>will create a MP4 video file with a bitrate of 512kbps and a frame width of 640 pixels (please note that mencoder will correctly take care of the aspect ratio for the final movie and the resize will just look OK.)</p>
<p>Optional parameters for the MKMP4 script:</p>
<ul>
<li>-q turns on 2 pass encoding: This will increase the final quality (and the encoding time), but it worths the extra wait if you care about quality.</li>
<li>-k to keep temporary files: The script will create several temporary files during the encoding process, if for whatever reason you want to keep that files, you should use this option.</li>
<li>-o to specify a target directory: By default, the mkmp4 script will place the output video in the working directory (the directory from where you called the script), if you want to change that behavior, just use this option.</li>
<li>-t to turn ON thumbnail and poster frame generation: If you use this flag, mkmp4 will create a thumbnail and a poster frame that you can then use if you plan to publish your videos on the web.</li>
</ul>
<p>The following command line will create a 512kbps video, will set the target width to 640 pixels AND will use 2 pass encoding, will <strong>not</strong> delete temporary files, will create thumbnails and poster frames, and will place the files on the <em><strong>/target/directory/</strong></em> directory.</p>
<pre>mkmp4 -f /path/to/some/video.avi -b 512 -w 640 -k -q -t -o /target/directory/</pre>
<h3>Some things you need to know and FAQ&#8217;s about this script:</h3>
<ul>
<li>By default, when the -f parameter is a directory, the script will search for any .mov file inside it, encode it, and place the final mp4 file into the directory set in the -o parameter (or the current directory if you have ommited that parameter), if you want to change that behavior, just change the value of the <em><strong>fileext</strong></em> variable inside the script to whatever file extension you want to process.</li>
<li>Why are you reducing the audio level by 5db ? This is because with some old audio formats, for some reason that I really ignore, mplayer will distort the audio output. Now, if you take a closer look, you will see that right after the volume filter, im applying a normalization filter to get the audio back to 0db, so the output audio level will always be correct no matter what happens.</li>
<li>Why are you forcing the audio output to be always stereo ? what if i want to encode mono material ?: Just modify the audio encoder trigger.</li>
<li>The output media produced by this script is automatically interleaved and is fully compatible (and has been tested) with <a href="http://www.adobe.com/products/flashmediaserver/">Flash Media Server 3</a> and <a href="http://www.wowzamedia.com/">Wowza Media Server Pro</a>.</li>
</ul>
<p>There are a lot of things that could be improved around here, and so far i have not tested this in any platform other than Mac OS Leopard, but so far, this should work in any Linux / Unix variant too without too much work.</p>
<p>If you are interested to get an idea of how the video results will look, you can take a look a <a href="http://blog.massanti.com/apple-video-archive/iphone/">The Apple Video Archive</a> here on this site.</p>
<p>As usual, comments / feedback and suggestions are welcome.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.massanti.com/2008/09/26/mkmp4-automated-h264-aacplus-encoder-script-mac-linux/feed/</wfw:commentRss>
		<slash:comments>61</slash:comments>
		</item>
		<item>
		<title>An automated and rock-solid encoding engine solution for your youtube-like site.</title>
		<link>http://blog.massanti.com/2008/08/05/automated-video-encoding-server-engine/</link>
		<comments>http://blog.massanti.com/2008/08/05/automated-video-encoding-server-engine/#comments</comments>
		<pubDate>Tue, 05 Aug 2008 11:59:29 +0000</pubDate>
		<dc:creator>Diego Massanti</dc:creator>
				<category><![CDATA[Software]]></category>
		<category><![CDATA[Video Encoding]]></category>
		<category><![CDATA[Web & Design]]></category>
		<category><![CDATA[audio]]></category>
		<category><![CDATA[encoding]]></category>
		<category><![CDATA[engine]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[video]]></category>

		<guid isPermaLink="false">http://massanti.com/?p=147</guid>
		<description><![CDATA[It was some days ago when i was contacted by one of the visitors of this blog in order to create a &#8220;black box&#8221; solution to solve the video encoding part of a video sharing website similar to youtube.com. As you maybe know, i have posted some months ago a really basic script to automate [...]]]></description>
			<content:encoded><![CDATA[<p>It was some days ago when i was contacted by one of the visitors of this blog in order to create a &#8220;black box&#8221; solution to solve the video encoding part of a video sharing website similar to youtube.com.</p>
<p>As you maybe know, i have posted some months ago a <strong>really basic script</strong> to automate some of the tasks, but, what about integration with your actual website framework ? what about queue management, what about multi-server encoding environment ?</p>
<p>Those are all the usual questions when a potential customer calls, so i spent some days developing a complete black-box solution for this issue, using all open-source software, with the following features.</p>
<ul>
<li>Fully automatic video encoding queue management.</li>
<li>Support for <strong>1 to n</strong> encoding nodes with fully automatic transaction control (still in beta-testing).</li>
<li>Support for the following video formats: Windows Media, Quicktime, DV, Avid DV, DivX, xVid and almost any imaginable format (any format supported by mplayer is actually 100% supported)</li>
<li>Support for HE-AAC audio (Stereo near-CD quality sound at 48kbps).</li>
<li>Fully automatic thumbnail generation in 3 different positions of the video stream, supported output formats are JPG, PNG and GIF.</li>
<li>Fully automatic metadata injection, including all the popular &#8220;iTunes like&#8221; tags.</li>
<li>Fully automatic iPod Touch / iPhone compatible stream generation.</li>
<li>Support for more than 1 output format (managed from a presets file).</li>
<li>Fully automatic Pixel and Display aspect ratio correction for proper video resizing and scaling.</li>
<li>Possibility to select how many CPU cores to use for the encoding process (only on multicore cpu&#8217;s)</li>
</ul>
<p>There are a several more features that I&#8217;m still developing. If you are interested and you want more details, you can contact me at: diego (at sign here) massanti.com. I don&#8217;t bite <img src='http://blog.massanti.com/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://blog.massanti.com/2008/08/05/automated-video-encoding-server-engine/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>MediaInfo for Mac: Know everything about your media files.</title>
		<link>http://blog.massanti.com/2008/01/30/mediainfo-for-mac/</link>
		<comments>http://blog.massanti.com/2008/01/30/mediainfo-for-mac/#comments</comments>
		<pubDate>Wed, 30 Jan 2008 23:18:20 +0000</pubDate>
		<dc:creator>Diego Massanti</dc:creator>
				<category><![CDATA[Apple / Macintosh]]></category>
		<category><![CDATA[Software]]></category>
		<category><![CDATA[Video Encoding]]></category>
		<category><![CDATA[MediaInfo Mac]]></category>

		<guid isPermaLink="false">http://massanti.com/2008/01/30/mediainfo-for-mac/</guid>
		<description><![CDATA[You should consider this piece of software my &#8220;hello world&#8221; project in the Mac development arena, because, even the fact that i have pretty good skills when it comes to OO development, I&#8217;m totally new when it comes to the Mac, and I&#8217;m using this project mainly as a learning experience. Attention: download links were [...]]]></description>
			<content:encoded><![CDATA[<blockquote><p>You should consider this piece of software my &#8220;hello world&#8221; project in the Mac development arena, because, even the fact that i have pretty good skills when it comes to OO development, I&#8217;m totally new when it comes to the Mac, and I&#8217;m using this project mainly as a learning experience.</p></blockquote>
<blockquote class="warning">
<h2>Attention: download links were moved</h2>
<p>In order to keep things organized, the download links and general information for <strong>MediaInfo Mac</strong> were moved <a href="http://mediainfo.massanti.com/">into this new website</a>.</p></blockquote>
<p><a title="MediaInfo Mac Screenshot" href="http://blog.massanti.com/wp-content/uploads/2008/01/mediainfo_mac_screenshot.png" rel="shadowbox[post-114];player=img;"><img class="right alignright" src="http://blog.massanti.com/wp-content/uploads/2008/01/mediainfo_mac_screenshot.thumbnail.png" alt="MediaInfo Mac Screenshot" width="79" height="128" /></a>In the past few days i have been working in a full Aqua version of a linux tool called &#8220;Mediainfo&#8221;, so this is the result.<br />
Let me introduce you to &#8220;MediaInfo Mac&#8221;.</p>
<p>MediaInfo Mac does just one thing: it gives you a lot of information about multimedia files, even files that QuickTime can&#8217;t read, and it does that by using a custom Universal Binary build of the <a href="http://mediainfo.sourceforge.net">linux library &#8220;libmediainfo&#8221; by Jerome Martinez</a> as its core.<br />
This application is obviously OpenSource and i will make all the sources available as soon as i finish cleaning the code and configuring an SVN repository on this server.<br />
MediaInfo Mac requires any PowerPC or Intel Macintosh computer running MacOS X 10.4 &#8220;Tiger&#8221; or newer.<br />
If you have any question or suggestion, feel free to leave your comment below <img src='http://blog.massanti.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> .</p>
<p>A BIG thank you goes to <a href="http://www.brockerhoff.net">Rainer Brockerhoff</a>, (who is a true monster in the Macintosh development area), for having the patience to answer a lot of my questions, and also, to the guys at<a href="irc://irc.freenode.org/macdev"> #macdev on freenode</a> for helping me to understand a lot of things in the &#8220;Cocoa Arena&#8221;. <strong>Thank you, i really appreciate your time.</strong></p>
<p>In order to keep things in organized, anything in relation with MediaInfo Mac is going to be <a href="http://mediainfo.massanti.com/">on this new website</a>.</p>
<p>IF you are any kind of software tracker, there is a <a href="http://mediainfo.massanti.com/u.php">feed with MediaInfo updates</a>, and it is compatible with the <a href="http://metaquark.de/blog/archive/About-Appcasting/">AppCast</a> definition.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.massanti.com/2008/01/30/mediainfo-for-mac/feed/</wfw:commentRss>
		<slash:comments>36</slash:comments>
		</item>
		<item>
		<title>Encoding high quality h.264 / aacPlus v2 media for web streaming with Flash 9, version 2.</title>
		<link>http://blog.massanti.com/2008/01/01/encoding-h264-aacplus-with-mencoder-for-flash/</link>
		<comments>http://blog.massanti.com/2008/01/01/encoding-h264-aacplus-with-mencoder-for-flash/#comments</comments>
		<pubDate>Tue, 01 Jan 2008 12:11:43 +0000</pubDate>
		<dc:creator>Diego Massanti</dc:creator>
				<category><![CDATA[Computers & Tech]]></category>
		<category><![CDATA[Video Encoding]]></category>
		<category><![CDATA[Web & Design]]></category>
		<category><![CDATA[aacPlus]]></category>
		<category><![CDATA[audio]]></category>
		<category><![CDATA[encoding]]></category>
		<category><![CDATA[flash]]></category>
		<category><![CDATA[h264]]></category>
		<category><![CDATA[linux]]></category>
		<category><![CDATA[mac]]></category>
		<category><![CDATA[media]]></category>
		<category><![CDATA[quality]]></category>
		<category><![CDATA[streaming]]></category>
		<category><![CDATA[tutorial]]></category>
		<category><![CDATA[unix]]></category>
		<category><![CDATA[video]]></category>

		<guid isPermaLink="false">http://massanti.com/2008/01/01/encoding-h264-aacplus-with-mencoder-for-flash/</guid>
		<description><![CDATA[Update Notice This post has been updated with a newer and improved script and instructions, please visit the new one HERE. It was some months ago when I wrote a tutorial about how to encode good quality h.264 video with aacPlus audio in order to use the great capabilities of the new flash player version [...]]]></description>
			<content:encoded><![CDATA[<blockquote class="warning"><h3>Update Notice</h3>
<p>This post has been updated with a newer and improved script and instructions, please visit the new one <a href="http://blog.massanti.com/2008/09/26/mkmp4-automated-h264-aacplus-encoder-script-mac-linux/#more-562">HERE</a>.</p></blockquote>
<p>It was some months ago when I wrote a <a href="http://massanti.com/2007/09/28/encoding-h264-video-aac-plus-audio-for-flash/">tutorial about how to encode good quality h.264 video with aacPlus audio</a> in order to use the great capabilities of the new flash player version 9.0.115 (that in case you missed it, now <a href="http://www.adobe.com/aboutadobe/pressroom/pressreleases/200708/082107FlashPlayer.html">can play h264 video and aacPlus v2 audio</a>).<br />
Due to some personal convictions for my prior tutorial, i decided to use <a href="http://ffmpeg.mplayerhq.hu/">FFMPEG</a>, and while it does the job pretty well, it has some limitations (specially in the video scaling / resizing area). So in my particular case, I had to use <a href="http://massanti.com/2007/10/01/serverside-video-resizing-script/">several php / bash scripts</a> in order to feed the encoders with the proper resizing data, etcetera.<br />
<strong>Well, let me introduce you to the 2nd version of my h.264 / aacPlus v2 tutorial.</strong></p>
<p>This time I used Mencoder, an amazing tool that comes with the <a href="http://www.mplayerhq.hu/">mPlayer</a> package, and i also created what i think is a really nice bash script to make the process almost 100% smooth and simple.</p>
<h3>These are the things you will need:</h3>
<ul>
<li>the <a href="http://www.mplayerhq.hu/design7/dload.html">mPlayer package</a> for whatever platform you&#8217;re running</li>
<li>the <a href="http://www.nero.com/enu/nero-aac-codec.html">Nero enhanced AAC audio encoder</a> if you are on linux / unix or <a href="http://massanti.com/2007/09/30/he-aac-plus-encoder-mac-universal-binary/">my build of the 3GPP HE-AAC encoder</a> if you are on a Mac.</li>
<li>the <a href="http://gpac.sourceforge.net/home_download.php">GPAC utilities</a></li>
<li>the <a href="http://www.videolan.org/developers/x264.html">x264 video encoder</a></li>
</ul>
<p><span id="more-96"></span><br />
So let&#8217;s start. As usual, I wont dig a lot in the details about how to install or build software on linux / unix / mac, because you can find several <a href="http://www.tuxfiles.org/linuxhelp/softinstall.html">tutorials about those matters</a> by using google.</p>
<h3>Building and installing the x264 encoder</h3>
<p>In order to compile and install the x264 encoder, i recommend you to get <strong>the latest SVN</strong> version and build it yourself.<br />
There are not any specific parameters at all for x264 other than the install path because the bash script that i will show you later <strong>assumes that you have all the tools in a directory that is inside your executable path</strong>.<br />
So&#8230; get the latest sources for x264:<br />
<code>svn co svn://svn.videolan.org/x264/trunk x264</code><br />
and then just run:<br />
<code>./configure --prefix=/usr<br />
make<br />
make install<br />
</code><br />
After you have the x264 encoder installed, its time to get and build mPlayer.</p>
<h3>Installing mPlayer and Mencoder</h3>
<p>Again, get the latest sources for mPlayer:<br />
<code>svn checkout svn://svn.mplayerhq.hu/mplayer/trunk mplayer</code><br />
and this is the configuration that you **should** use:<br />
<code>./configure --prefix=/usr</code></p>
<blockquote class="mac"><p><strong>If you are on MacOS and you get errors when building, do this:</strong><br />
<code>./configure --prefix=/usr --disable-encoder=flac --disable-encoder=cavs --disable-decoder=cavs</code><br />
and then the usual&#8230;<br />
<code>make<br />
make install</code></p></blockquote>
<blockquote class="warning">
<h2>Very Important:</h2>
<p>After you compile and install all the tools, you will find a directory called &#8220;TOOLS&#8221; inside the mplayer source tree, go inside it, and look for an small script called &#8220;midentify&#8221;, you MUST COPY this script to your <code>/usr/bin</code> directory or any other dir inside your executable path for the bash script to work. This is mandatory.</p></blockquote>
<h2> The bash magic&#8230;</h2>
<p>Ok, so after you have all the tools built, **AND** in your path, let me show you this simple script that i have written in order to make things really simple.</p>
<pre><code class="bash">#! /bin/bash   

# This bash script has been created by Diego Massanti
# You are free and welcome to do whatever you want with it.
# www.massanti.com

usage()
{
cat &lt; &lt; EOF
usage: $0 -f  [-w ] -b  [-q][-k]

* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* h264 video with he-aac audio encoding script by Diego Massanti. *
*                January 2008, Made in Argentina.                 *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *

OPTIONS:
   	-h	Show this message
 -f	Path to the file to encode &lt; REQUIRED
   	-w	Resize video to fit inside this width while keeping the aspect ratio &lt; OPTIONAL
   	-b	Desired video bitrate &lt; REQUIRED
   	-q 	Better quality encoding using 2 passes (slower) &lt; OPTIONAL
 -k	Do not delete (keep) temporary files &lt; OPTIONAL
EOF
}
audiobitrate=48000
platform=""
uname=`uname`
if [ $uname == "Darwin" ]; then
 ## 99% of chances that this is a Mac
 platform="Mac"
else
 platform="Linux"
fi

width=""
bitrate=
deltemp=TRUE
quality=FALSE
filename=
rsize=""

while getopts ":f:w:b:qkv" OPTION; do
  case $OPTION in
    w ) width=$OPTARG;;
 f ) filename=$OPTARG;;
    b ) bitrate=$OPTARG;;
    k ) deltemp=FALSE;;
 q )	quality=TRUE;;
    h ) usage;;
    \? ) usage
         exit 1;;
    * ) echo $usage
          exit 1;;
  esac
done

if [ "$1" == "" ]; then
 usage
 exit 0
fi

if [ "$filename" == "" ]; then
 echo
 echo you MUST supply a file to encode!, use the -f parameter. i.e: -f mymovie.avi
 echo
 usage
 exit 1
fi
if [ "$bitrate" == "" ]; then
 echo
 echo you MUST specify a target bitrate!, use the -b parameter. i.e: -b 512
 echo
 usage
 exit 1
fi

if [ "$width" != "" ]; then
 rsize="-vf scale=$width:-3"
 rsizemsg="fit into $width pixels wide"
else
 rsize=""
 rsizemsg="Movie is not being resized."
fi

MOVIE_FPS=`midentify "$filename" | grep FPS | cut -d = -f 2`
#clear
echo "*    Encoding: $filename"
echo "*    Resizing to: $rsizemsg."
echo "*    Total Bitrate: $bitrate kbps."
let "caudiobitrate = $audiobitrate / 1000"
let "bitrate = $bitrate - $caudiobitrate"
echo "*    Video Bitrate: $bitrate kbps."
echo "*    Audio Bitrate: $caudiobitrate kbps."
echo "*    Platform: $platform."

# Encoding phase starts here...
# Encoding Video...
echo "* * * Starting video encoding pass 1... * * *"
mencoder "$filename" -o "${filename%.*}_temp.264" -passlogfile "${filename%.*}"_temp.log $rsize -ovc x264 -x264encopts bitrate=$bitrate:frameref=8:bframes=3:b_adapt:b_pyramid:weight_b:partitions=all:8x8dct:me=umh:subq=6:trellis=2:brdo:threads=auto:pass=1:analyse=all -of rawvideo -nosound

if [ "$quality" == "TRUE" ]; then
echo "* * * Starting video encoding pass 2... * * *"
mencoder "$filename" -o "${filename%.*}_temp.264" -passlogfile "${filename%.*}"_temp.log $rsize -ovc x264 -x264encopts bitrate=$bitrate:frameref=8:bframes=3:b_adapt:b_pyramid:weight_b:partitions=all:8x8dct:me=umh:subq=6:trellis=2:brdo:threads=auto:pass=2:analyse=all -of rawvideo -nosound
fi

# Extracting audio to a 48khz WAV file.
echo "* * * Extracting Audio... * * *"
mplayer "$filename" -af resample=48000:0:2,volnorm=2:0.25 -ao pcm:file="${filename%.*}_temp.wav" -vc dummy -vo null

# Encoding Audio to HE-AAC plus file
echo "* * * Encoding Audio... * * *"

if [ "$platform" == "Mac" ]; then
enhAacPlusEnc "${filename%.*}_temp.wav" "${filename%.*}_temp.aac" $audiobitrate s
else
neroAacEnc -br 48000 -he -if ${filename%.*}_temp.wav -of ${filename%.*}_temp.mp4
fi

# Muxing
echo "* * * Generating final MP4 container... * * *"

MP4Box -add "${filename%.*}_temp.264#video:fps=$MOVIE_FPS" "${filename%.*}.m4v"
if [ "$platform" == "Mac" ]; then
MP4Box -add "${filename%.*}_temp.aac" "${filename%.*}.m4v"
else
MP4Box -add "${filename%.*}_temp.mp4#audio" "${filename%.*}.m4v"
fi

# MetaData and interleaving
name=${filename%.*}
album="Some Album"
author="Some Author"
comment="Some Comment"
created="2007"
MP4Box -inter 500 -itags album="$album":artist="$author":comment="$comment":created="$created":name="$name" -lang English "${filename%.*}".m4v

# Clean up temporary files...
if [ $deltemp == "TRUE" ]; then
 echo "* * * Removing temporary files... * * *"
 rm "${filename%.*}"_temp*
fi</code></pre>
<p>You can also <a title="download the mkmp4 shell script" href="http://massanti.com/wp-content/uploads/2008/01/mkmp4.sh">download the mkmp4 shell script</a> in order to keep proper tabulations and code formatting.</p>
<p>You should save the code here into a simple text file, give it any name you want (i call mine &#8220;mkmp4&#8243;) and dont forget to <em>chmod +x</em> it.<br />
Usage is really simple:<br />
The scripts expects 2 main parameters that are mandatory:</p>
<ul>
<li>-f</li>
<li>-b</li>
</ul>
<p>So for example, typing <code>./mkmp4 -f myvideo.avi -b 512</code> will create a H.264 video file, with aacPlus v2 audio inside an MP4 container with a total <strong>bitrate of 512kbps</strong> and using <em>myvideo.avi</em> as source.<br />
The resulting file is ready for <strong>HTTP Streaming</strong> or <strong>FlashMedia Streaming Server 3</strong>, and of course, it will also play in any video player out there capable of H.264 and aacPlus v2 decoding.<br />
The script also accepts some other optional parameters.</p>
<ul>
<li>-q to use <strong>2 pass encoding</strong>, slower, but better quality <strong>specially</strong> at low bitrates</li>
<li>-w to resize the output video, for example &#8220;-w 480&#8243; will resize the video to 480 pixels wide <strong>while keeping the correct aspect ratio</strong>.</li>
<li>-k to keep temporary files</li>
<li>-h to show inline help</li>
</ul>
<p><strong>The script will detect if you are running it on a Mac or on any kind of linux / unix variant, and use the proper audio encoder for each platform.</strong><br />
Also, in lines 133 to 136 in the script code, you will find some meta information variables that you can customize. That information is used by players like QuickTime, iTunes and many others in order to correctly organize the file inside a user&#8217;s media library.</p>
<h3>Final considerations</h3>
<p>This script can use as source, any video format supported by mPlayer, wich in other words means that almost any kind of video file can be transcoded by using this method.<br />
If you are wondering what kind of quality / bitrate relation you will get by using this method, you are free to <a href="http://massanti.com/macvideo/ipod-itunes/ipod-touch/">watch this 512kbps video stream</a> (feel free to press the fullscreen button on the player <img src='http://blog.massanti.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />  )<br />
When i released the first version of this tutorial 3 months ago, somebody asked how to get better quality for screencasts, and i guess the answer to that question would be something like: Framerate is not important at all, anything over 10FPS is useless for a screencast, by lowering the video framerate you get more quality at the same bandwidth, also, if you are doing a screencast, most probably you want your viewers to be able to &#8220;read&#8221; at texts, etc, so, a resolution of at least 640&#215;480 is a must, and for bitrate, i think 512kbps with a two pass encoding process will be more than enough.</p>
<p>If you have any comment or suggestion you can leave a coment below. Thanks for reading !</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.massanti.com/2008/01/01/encoding-h264-aacplus-with-mencoder-for-flash/feed/</wfw:commentRss>
		<slash:comments>78</slash:comments>
		</item>
		<item>
		<title>Creating good quality h264 video for the upcoming Flash Player and Flash Media Server</title>
		<link>http://blog.massanti.com/2007/09/28/encoding-h264-video-aac-plus-audio-for-flash/</link>
		<comments>http://blog.massanti.com/2007/09/28/encoding-h264-video-aac-plus-audio-for-flash/#comments</comments>
		<pubDate>Fri, 28 Sep 2007 11:04:42 +0000</pubDate>
		<dc:creator>Diego Massanti</dc:creator>
				<category><![CDATA[Apple / Macintosh]]></category>
		<category><![CDATA[Video Encoding]]></category>
		<category><![CDATA[aac]]></category>
		<category><![CDATA[audio]]></category>
		<category><![CDATA[encoding]]></category>
		<category><![CDATA[flash]]></category>
		<category><![CDATA[h264]]></category>
		<category><![CDATA[high]]></category>
		<category><![CDATA[linux]]></category>
		<category><![CDATA[mac]]></category>
		<category><![CDATA[OS]]></category>
		<category><![CDATA[quality]]></category>
		<category><![CDATA[stream]]></category>
		<category><![CDATA[streamming]]></category>
		<category><![CDATA[tutorial]]></category>
		<category><![CDATA[video]]></category>

		<guid isPermaLink="false">http://massanti.com/2007/09/28/encoding-h264-video-aac-plus-audio-for-flash/</guid>
		<description><![CDATA[Important This tutorial has been updated (and improved) a lot, for the new version please follow this link. UPDATE: I realized that when i read a tutorial about anything, in most cases, i want to first see what result im going to get so then i can decide if the tutorial is worth reading (or [...]]]></description>
			<content:encoded><![CDATA[<blockquote class="warning">
<h3>Important</h3>
<p>This tutorial has been updated (and improved) a lot, for the new version please follow <a href="http://massanti.com/2008/01/01/encoding-h264-aacplus-with-mencoder-for-flash/">this link</a>.
</p></blockquote>
<blockquote><p>UPDATE: I realized that when i read a tutorial about anything, in most cases, i want to first see what result im going to get so then i can decide if the tutorial is worth reading (or not), so here is a head by head comparison: </p>
<p>This is an h264 file encoded by the guys at apple.com, it&#8217;s the TV Commercial for the brand new iMac: <a href="http://movies.apple.com/movies/us/apple/mac/imac/2007/apple_solar_20070820_848x480.mov" rel="shadowbox[post-15];width=640;height=385;">ORIGINAL VIDEO</a></p>
<p>This is the same file re-encoded using the steps on this tutorial: <a href="http://plusmediaserver.com/media/new_imac_2007.mp4">RE-ENCODED VIDEO</a></p>
<p>Obviouisly, there is an small quality degradation, but keep in mind that apple&#8217;s original movie is 14MB in size, while mine is <strong>1.5MB</strong> <img src='http://blog.massanti.com/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> .<br />
You can play my MP4 video with almost any video player out there, but remember that <strong>not all video players out there support HE-AAC audio (or aacPlus) and b-frames</strong>, so if you play the file with a player like QuickTime, <strong>it is NOT going to work</strong>.<br />
I recommend <a href="http://www.mplayerhq.hu/">mPlayer</a> or <a href="http://www.videolan.org/vlc/">VLC media player</a> to play this kind of video, and obviously, Flash Player will play the file perfectly too.
</p></blockquote>
<blockquote class="mac"><h3>Update for Macintosh users:</h3>
<p>I updated the bash script on this post so it can be used also by Macintosh users, so instead of using the NERO Digital AAC Encoder, Macintosh users can take advantage of the native 3GPP AAC Plus V2 encoder that i just compiled. Visit <a href="http://massanti.com/2007/09/30/he-aac-plus-encoder-mac-universal-binary/">this post</a> for more information about it.</p></blockquote>
<p>Like a lot of people out there, i was really happy when i found the <a href="http://www.adobe.com/aboutadobe/pressroom/pressreleases/200708/082107FlashPlayer.html">press release from Adobe</a>, stating the addition of h.264 video support and HE-AAC Plus audio to their <a href="http://labs.adobe.com/downloads/flashplayer9.html">upcoming Flash Player</a> versions, and also, to the upcoming Flash Media Server.<br />
After following several &#8220;Tutorials&#8221; all over the web, i found that there was not any single one that gave me the results that i wanted; that is, good video quality, with the less possible file size, so after a lot of trial and error and after a lot of help and optimizations by the guys at the ffmpeg and x264 IRC channels, i decided to create this tutorial that will most probably help you a lot to encode good quality h264 video, with HE-AAC Plus Audio, and all this inside an MP4 container with tags and all ready to deliver to your visitors.</p>
<p>There are several things that you will need in order to do this:</p>
<ul>
<li>The latest <a href="http://wiki.videolan.org/Subversion">SVN version of the x264 video codec</a>.</li>
<li>The latest <a href="http://ffmpeg.mplayerhq.hu/download.html">SVN version of the excellent ffmpeg video encoding utility</a>.</li>
<li>The <a href="http://gpac.sourceforge.net/home_download.php">latest version of the GPAC utilies</a> (it contains several tools to mux and work with MP4 files).</li>
<li>The <a href="http://www.nero.com/nerodigital/eng/down-ndaudio.php">linux version of the Nero Digital AAC Audio Encoder</a></li>
<li>GCC 4.1 or later installed, this is a requeriment for x264 to encode faster. It WILL work if you compile with older versions of GCC, but you will have performance issues resulting in more time needed to compress the video streams.</li>
</ul>
<p><span class="info"><strong>Some friendly bookmarks:</strong><br />
SVN checkout for x264:<br />
<code>svn checkout svn://svn.videolan.org/vlc/trunk vlc-trunk</code><br />
SVN checkout for ffmpeg:<br />
<code>svn checkout svn://svn.mplayerhq.hu/ffmpeg/trunk ffmpeg</code></span></p>
<p>First of all, I&#8217;m assuming that you are running Linux, Mac-OS X or any other UNIX variant. I never tried this on windows because I am pretty sure that there are lot of really nice programs that you could buy to do the job.<br />
<span id="more-15"></span><br />
<strong>Step 1: Configuring, compiling and installing all the stuff.</strong></p>
<p>I am assuming that you have some basic skills with Linux / Unix / Mac OS, so i won&#8217;t really digg into the details of how to compile from sources all the tools. In most cases just taking a look at the readme file for each piece of software will tell you all you need to know, so i will just explain here some basical tips that you need to know.<br />
After configuring, building and installing the x264 codec, this is the configure line that i used for ffmpeg in my paticular case:</p>
<p><code>./configure --enable-libx264 --enable-libfaac --enable-gpl --prefix=/usr --enable-libfaad --enable-swscaler --enable-pp</code></p>
<p>All the settings there are self-explanatory, the only thing you could need to change is the prefix to something like <strong>&#8211;prefix=/home/yourusername/</strong> if you prefer to install the software just for your own user account.<br />
After you have ffmpeg installed and running, we need to install two more things, the GPAC utilities and the Nero Digital AAC Audio Encoder.<br />
Go <a href="http://gpac.sourceforge.net/home_download.php">grab the latest sources for GPAC</a>, do the usual:</p>
<p><code>./configure &amp;&amp; make &amp;&amp; make install</code></p>
<p>After that you are ready to go, then go and <a href="http://www.nero.com/nerodigital/eng/down-ndaudio.php">download the latest version of the Nero encoder</a>, and you will notice a directory called &#8220;linux&#8221; inside it, you should copy all the files there to any location in your path, usually /usr/bin (if you are installing everything as root) or /home/yourusername/bin (if you are installing just for your user account).</p>
<p><strong>Step 2: Checking that everything is working as supposed.</strong></p>
<p>To check that everything is ok, you should type:</p>
<p><code>ffmpeg -formats</code></p>
<p>and you will get a LONG list of supported encoders and decoders, if you have the following line, you are ready to go:</p>
<p><code> EV    libx264</code></p>
<p><strong>Step 3: Doing the video encoding (or transcoding).</strong></p>
<p>So everthing looks great so far, except for one little detail, there is actually no support for any HE-AAC encoder directly from inside ffmpeg, so we will do the encode in 2 phrases, first the video, and then the audio.</p>
<p>From now on, i will assume that we have a video called &#8220;movie.avi&#8221; that we want to convert to a Flash Ready h264 file, so lets start encoding the video.<br />
I am a person who likes quality, so for this example I am going to use a 2 pass encoding process.</p>
<p>This is the command line to perform the first pass of the video encoding:</p>
<p><code>ffmpeg -y -i movie.avi  -an <strong>-pass 1</strong> -vcodec libx264 -b 384k -flags +loop -cmp +chroma -partitions +parti4x4+partp8x8+partb8x8 -flags2 +mixed_refs -me umh -subq 5 -trellis 1 -refs 3 -bf 3 -b_strategy 1 -coder 1 -me_range 16 -g 250 -keyint_min 25 -sc_threshold 40 -i_qfactor 0.71 -bt 384k -rc_eq 'blurCplx^(1-qComp)' -qcomp 0.8 -qmin 10 -qmax 51 -qdiff 4 movie_temp.mp4</code></p>
<p>Depending on how long is your video source and how powerful is your computer, after some minutes it should finish, so it is time to make the second pass of the video encoding:</p>
<p><code>ffmpeg -y -i movie.avi  -an <strong>-pass 2</strong> -vcodec libx264 -b 384k -flags +loop -cmp +chroma -partitions +parti4x4+partp8x8+partb8x8 -flags2 +mixed_refs -me umh -subq 5 -trellis 1 -refs 3 -bf 3 -b_strategy 1 -coder 1 -me_range 16 -g 250 -keyint_min 25 -sc_threshold 40 -i_qfactor 0.71 -bt 384k -rc_eq 'blurCplx^(1-qComp)' -qcomp 0.8 -qmin 10 -qmax 51 -qdiff 4 movie_temp.mp4</code></p>
<p>This pass is gonna take more time than the first one, because the first one just analyzes the video while the 2nd pass does the actual encoding.</p>
<p>IF everything went as supposed, after some time you should have a new file called &#8220;movie_temp.mp4&#8243; and you will notice that it is just your movie, but without sound&#8230;. dont alarm! we are going to make the sound part right now.</p>
<p><strong>Step 4: Doing the audio encoding.</strong></p>
<p>This part is pretty simple, we will use ffmpeg to create a simple wav file because the nero encoder only accepts a wav file as input, so we will do:</p>
<p><code>ffmpeg -i movie.avi -ar 48000 -ac 2 movie_temp_audio.wav</code></p>
<p>After this finishes, you will have a wav file called &#8220;movie_temp_audio.wav&#8221; that should be playable in any audio player of your choice, it basically contains the audio of your movie, so let&#8217;s transform this to a shiny HE-AAC Plus audio file.</p>
<p>To convert the wav audio file into an HE-AAC Plus file, we will use the Nero Encoder (neroAacEnc) and we will tell it to use our recently created wav file as source and output it to a new audio only mp4 file called &#8220;movie_temp_audio.mp4&#8243; like this:</p>
<p><code>neroAacEnc -br 48000 -he -if movie_temp_audio.wav -of movie_temp_audio.mp4</code></p>
<p>So ok, we have at this point one MP4 file with just video, and another MP4 file with just audio, lets mux all this into our final flash ready MP4.</p>
<p><strong>Step 5: Muxing everything into our final MP4 file ready for Flash broadcast.</strong></p>
<p>At this point we are going to use a program called &#8220;MP4Box&#8221; that is part of the GPAC utilities that you have already installed if you followed my instructions.</p>
<p>First, we will command MP4Box to add <strong>ONLY</strong> the video track of our first <em>video only</em> MP4 into our final MP4:</p>
<p><code>MP4Box -add movie_temp.mp4#video final_movie.mp4</code></p>
<p>This will create a new MP4 file called &#8220;final_movie.mp4&#8243;, and it will add our video track inside it. Now we need to add the audio track into our final movie file, to do that we do as follows:</p>
<p><code>MP4Box -add movie_temp_audio.mp4#audio final_movie.mp4</code></p>
<p>After this step, if you open your &#8220;final_movie.mp4&#8243; file with some video player like QuickTime, mPlayer or VLC, you should have a brand new h264 file with both video and audio, and with perfect sync between the two.<br />
We need now to do some extra steps to optimize the file for progressive HTTP streaming so the flash player doesn&#8217;t needs to download the whole file before starting to play it, so lets interleave our file:</p>
<p><code>MP4Box -inter 500 final_movie.mp4</code></p>
<p>This line will interleave meta information to the file every 500 msec, that way, the flash player will be able to play the file as soon as it gets the first 500msec of video data.</p>
<p>After you are satisfied with your result, you can simply type:</p>
<p><code>rm movie_temp* -f</code></p>
<p>to get rid of all the temporal files that you created.</p>
<p>Obviously, this involves a LOT of steps in order to create the final file, and it is good to make them one by one so you get an idea of what are you doing&#8230;. after all, the idea is to learn&#8230;. isn&#8217;t it ?<br />
But as you may suppose, the logical step after doing the &#8220;manual&#8221; way, is to create a bash script that is gonna do everything for us, so i give you below, an example of how this script could look:</p>
<pre><code class="bash">#! /bin/bash
# ${foo%.*} to remove the extension
# FIRST PASS
echo "Starting Pass 1 ! ! !"

ffmpeg -y -i $1 -an -pass 1 -vcodec libx264 -b 384k -flags +loop -cmp +chroma -partitions +parti4x4+partp8x8+partb8x8 -flags2 +mixed_refs -me umh -subq 5 -trellis 1 -refs 3 -bf 3 -b_strategy 1 -coder 1 -me_range 16 -g 250 -keyint_min 25 -sc_threshold 40 -i_qfactor 0.71 -bt 384k -rc_eq 'blurCplx^(1-qComp)' -qcomp 0.8 -qmin 10 -qmax 51 -qdiff 4 ${1%.*}_temp.mp4

# SECOND PASS
echo "Starting pass 2 ! ! !"

ffmpeg -y -i $1 -an -pass 2 -vcodec libx264 -b 384k -flags +loop -cmp +chroma -partitions +parti4x4+partp8x8+partb8x8 -flags2 +mixed_refs -me umh -subq 5 -trellis 1 -refs 3 -bf 3 -b_strategy 1 -coder 1 -me_range 16 -g 250 -keyint_min 25 -sc_threshold 40 -i_qfactor 0.71 -bt 384k -rc_eq 'blurCplx^(1-qComp)' -qcomp 0.8 -qmin 10 -qmax 51 -qdiff 4 ${1%.*}_temp.mp4

# Exporting audio to a temporal 48k WAVE file
ffmpeg -y -i $1 -ar 48000 -ac 2 ${1%.*}_temp_audio.wav

# Encoding audio to HE-AAC V2 (aacPlus SBR + PS) -- UNCOMMENT THE FOLLOWING LINE on LINUX / UNIX with the NERO encoder
neroAacEnc -br 48000 -he -if ${1%.*}_temp_audio.wav -of ${1%.*}_temp_audio.mp4

# Encoding audio to HE-AAC V2 (aacPlus SBR + PS) -- UNCOMMENT THE FOLLOWING LINE on Mac OS X and remember to comment the LINUX one:
# enhAacPlusEnc ${1%.*}_temp_audio.wav ${1%.*}_temp_audio.aac 48000 s

#Creates final MP4 container and adds video track...
MP4Box -add "${1%.*}_temp.mp4#video" ${1%.*}_flash_ready.mp4

#Adds audio track...
# IMPORTANT: If running this script on LINUX, UNCOMMENT THIS line:
MP4Box -add "${1%.*}_temp_audio.mp4#audio" ${1%.*}_flash_ready.mp4

# IMPORTANT:if running this script on a MAC, UNCOMMENT THIS line and remember to comment the LINUX one:
#MP4Box -add "${1%.*}_temp_audio.aac" ${1%.*}_flash_ready.mp4 

#Sets MetaData...
MP4Box -lang English ${1%.*}_flash_ready.mp4

#Interleaving (For HTTP Streming... this should be removed if using FMS / RTMP )
MP4Box -inter 500 ${1%.*}_flash_ready.mp4

echo "Deleting Temporary Files..."
rm ${1%.*}_temp* *.log -f

echo "DONE!"
echo "Your file has been encoded as ${1%.*}_flash_ready.mp4"</code></pre>
<p>You should make a file called, for example, mkmp4.sh and paste the script inside it, then chmod +x the file and move it to /usr/bin , after that, you should be able to run for example:</p>
<p><code>mkmp4 mymovie.avi</code></p>
<p>and the bash script will create a new file called mymovie_flash_ready.mp4 in the same directory, that assuming that you have all the needed tools in the path.</p>
<p>So happy encoding! if you have any questions or any problem, please post a comment here and i will try to help you as soon as i can. Please avoid to post questions about how to build / configure / compile software because i don&#8217;t really have the time to help you with that and there are a lot of tutorials for that on the web, and like i said, this tutorial assumes that you have the basic linux skills already inside your little brain.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.massanti.com/2007/09/28/encoding-h264-video-aac-plus-audio-for-flash/feed/</wfw:commentRss>
		<slash:comments>28</slash:comments>
<enclosure url="http://movies.apple.com/movies/us/apple/mac/imac/2007/apple_solar_20070820_848x480.mov" length="13824259" type="video/quicktime" />
<enclosure url="http://plusmediaserver.com/media/new_imac_2007.mp4" length="1597936" type="video/mp4v-es" />
		</item>
	</channel>
</rss>
