<?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; unix</title>
	<atom:link href="http://blog.massanti.com/tag/unix/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.massanti.com</link>
	<description>Confessions from a Geek</description>
	<lastBuildDate>Wed, 27 Oct 2010 11:22:32 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.4</generator>
		<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>
	</channel>
</rss>

