AnalogClock






Tuesday, June 17, 2008

ffmpeg with h.264 3gp/amr xvid lame/mp3 a52/ac3 dts aac

I was going through the source for mythweb to find a good spot to insert code to setup and return VLC VOD links instead of simple http streams.  I followed the code for flash video .flv on the fly conversion and streaming with http range based seeking.  I was surpised to find a very simple and elegant ffmpeg based  CGI script doing all the work.  In /var/www/mythweb/modules/stream/handler.pl I found the following:

elsif ($ENV{'REQUEST_URI'} =~ /\.flv$/i) {
# Print the movie
$ffmpeg_pid = open(DATA,
"$ffmpeg -y -i ".shell_escape($filename)
.' -s '.shell_escape("${width}x$height")
.' -r 24 -f flv -ac 2 -ar 11025
.' -ab '.shell_escape("${abitrate}k")
.' -b '.shell_escape("${vbitrate}k")
.' /dev/stdout 2>/dev/null |'
);
unless ($ffmpeg_pid) {
print header(),
"Can't do ffmpeg: $!";
exit;
}
print header(-type => 'video/x-flv');
my $buffer;
while (read DATA, $buffer, 262144) {
print $buffer;
}
close DATA;
exit;
}

Pretty cool. I'm going to use this as a template for generating VLC VOD media on the fly using netcat. More to follow.

Anyway what happened is that I realized the flash video was working great but there was no audio. Playing with ffmpeg on the command line using the parameters above clued me in. ffmpeg didn't know how to decode AC3 and it also didn't know how to encode mp3.

I was surpised to find that the Medibuntu ffmpeg was missing some codecs that I know ffmpeg supports. I looked around the web for a way to compile ffmpeg with the missing codecs.

I found a bunch of apt-get source ffmpeg style how to's and figured out the DEBIAN_BUILD_OPTIONS="risky" part.

Had to install fakeroot to make the source by hand.

Another site had the details for getting AMR/3GP/3GPP/3GP2 AKA AMR narrow band and wide band. Found the how to for ffmpeg for Debian at this page. And the HOWTO with AMR at this page

I performed the following:

sudo apt-get build-dep ffmpeg
sudo aptitude install fakeroot liblame-dev liba52-0.7.4 liba52-dev libdts-dev libfaac0 libfaac0-dev libfaad0 libfaad-dev libxvidcore4 libxvidcore4-dev libx264-57 libx264-dev
apt-get source ffmpeg
wget http://www.3gpp.org/ftp/Specs/archive/26_series/26.104/26104-510.zip
wget http://www.3gpp.org/ftp/Specs/archive/26_series/26.204/26204-510.zip
unzip -q 26104-510.zip
unzip -q 26204-510.zip
cd ffmpeg-0.cvs20070307
mkdir libavcodec/amr_float
mkdir libavcodec/amrwb_float
cd libavcodec/amr_float
unzip -q ../../../26104-510_ANSI_C_source_code.zip
cd ../../libavcodev/amrwb_float
unzip -q ../../../26204-510_ANSI-C_source_code.zip
cd ../..

Ended up changing the following section in ffmpeg-0.cvs20070307/debian/rules in the source directory from this:

confflags += --enable-gpl --enable-pp --enable-swscaler --enable-pthreads
confflags += --enable-libvorbis --enable-libtheora --enable-libogg --enable-libgsm

to this:

confflags += --enable-gpl --enable-pp --enable-swscaler --enable-pthreads
confflags += --enable-libvorbis --enable-libtheora --enable-libogg --enable-libg
confflags += --enable-x264 --enable-liba52 --enable-libdts --enable-amr_nb
confflags += --enable-amr_wb

And finally I performed the following in the extracted source directory:

DEBIAN_BUILD_OPTIONS="risky" fakeroot debian/rules binary
cd ..
sudo dpkg -i *.deb

Blogged with the Flock Browser

No comments: