skip to navigation
May 28th, 2010 09:00 AM

Transcoding Video for Yahoo! Connected TV Devices Using ffmpeg

This guide will address the problem of transcoding video for multiple television platforms and provide detailed steps on how to install ffmpeg and the x264 library. This guide provides only one method for encoding video, many other methods exist. The success of the method provided here may depend on your own encoding environment and video workflow. Based on Yahoo! QA testing, videos encoded to these specifications work on all 2010 LG, Samsung, and Vizio Connected TV devices.


If you wish to find out more information about transcoding videos for the Sony platform, please email tvwidgets@yahoo-inc.com.

Large video files (> ~50MB) encoded using the steps outlined in this guide do not play on 2009 Samsung Connected TV devices. Yahoo! Connected TV is in discussions with Samsung to come up with a solution.

Compiling and Installing ffmpeg on Ubuntu

The following steps walk you through installing ffmpeg and x264 from the latest versions:

  1. Clean up systems.
  2. Install prerequisites.
  3. Checkout newest versions of ffmpeg and x264.
  4. Compile ffmpeg without x264 support and install.
  5. Compile x264 and install.
  6. Recompile ffmpeg with x264 support and install.

Some might say, “But wait, ffmpeg and x264 are already in the Ubuntu repository!” Yes, they are, but they are not up-to-date versions. Also, the ffmpeg version included in the repository was not built with x264 support.

Clean Up Systems

First, let’s clean up your system a bit. Over time, updated versions of packages may become available, for example security updates. To upgrade your system, first update your package index and then remove any existing installations of ffmpeg, as outlined below:

#update the package index
sudo apt-get update
#remove existing installs of ffmpeg
sudo apt-get remove ffmpeg x264 libx264-dev

Install Prerequisites

Next, you install supporting packages, including build utilities, audio codecs, and version control drivers:

#get supporting packages
#For Ubuntu 9.10 (Karmic Koala) use:
sudo apt-get install build-essential subversion git-core checkinstall yasm texi2html libfaac-dev libfaad-dev libmp3lame-dev libopencore-amrnb-dev libopencore-amrwb-dev libsdl1.2-dev libx11-dev libxfixes-dev libxvidcore4-dev zlib1g-dev
#For Ubuntu 10.04 (Lucid Lynx) use:
sudo apt-get install build-essential subversion git-core checkinstall yasm texi2html libfaac-dev libfaad-dev libmp3lame-dev libopencore-amrnb-dev libopencore-amrwb-dev libsdl1.2-dev libx11-dev libxfixes-dev libxvidcore-dev zlib1g-dev

To make future upgrades easier, create a directory for builds:

mkdir ~/ffmpegBuilds
cd ~/ffmpegBuilds

Checkout the Newest Versions of ffmpeg and x264

Now grab the latest code for both ffmpeg and x264:

#get ffmpeg source
git clone git://git.ffmpeg.org/ffmpeg.git
#get x264
git clone git://git.videolan.org/x264.git

Compile ffmpeg without x264 Support and Install

First, compile and install ffmpeg without x264. You need ffmpeg installed to build x264.

#compile ffmpeg without x264
cd ffmpeg

#For Ubuntu 9.10 (Karmic Koala) use:
./configure --enable-gpl --enable-version3 --enable-nonfree --enable-postproc --enable-pthreads --enable-libfaac --enable-libfaad --enable-libmp3lame --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libxvid --enable-x11grab

#For Ubuntu 10.04 (Lucid Lynx) use:
./configure --enable-gpl --enable-version3 --enable-nonfree --enable-postproc --enable-pthreads --enable-libfaac --enable-libmp3lame --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libxvid --enable-x11grab

make
#install ffmpeg
sudo checkinstall --pkgname=ffmpeg --pkgversion "4:0.5+svn`date +%Y%m%d`" --backup=no --default
cd ../

Compile x264 and Install

Next, compile and install x264:

#compile x264
cd x264
./configure
make
#install x264
sudo checkinstall --pkgname=x264 --pkgversion "1:0.svn`date +%Y%m%d`+`git rev-list HEAD -n 1 | head -c 7`" --backup=no --default
cd ../

Recompile ffmpeg with x264 Support and Install

Now, recompile ffmpeg with x264 support and install it:

#compile ffmpeg with x264
cd ffmpeg
./configure --enable-gpl --enable-version3 --enable-nonfree --enable-postproc --enable-pthreads --enable-libfaac --enable-libfaad --enable-libmp3lame --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libx264 --enable-libxvid --enable-x11grab
make
#install ffmpeg
sudo checkinstall --pkgname=ffmpeg --pkgversion "4:0.5+svn`date +%Y%m%d`" --backup=no --default
cd ../

Now you have ffmpeg installed! Make sure to check if everything installed correctly by typing the following command:

ffmpeg

The output should look something like this:

FFmpeg version SVN-r22470, Copyright (c) 2000-2010 the FFmpeg developers
built on Mar 11 2010 12:16:34 with gcc 4.4.1
configuration: --enable-gpl --enable-version3 --enable-nonfree --enable-postproc --enable-pthreads --enable-libfaac --enable-libfaad --enable-libmp3lame --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libx264 --enable-libxvid --enable-x11grab
libavutil     50.11. 0 / 50.11. 0
libavcodec    52.58. 0 / 52.58. 0
libavformat   52.55. 0 / 52.55. 0
libavdevice   52. 2. 0 / 52. 2. 0
libswscale     0.10. 0 /  0.10. 0
libpostproc   51. 2. 0 / 51. 2. 0
Hyper fast Audio and Video encoder
usage: ffmpeg [options] [[infile options] -i infile]... {[outfile options] outfile}...
Use -h to get full help or, even better, run 'man ffmpeg'

Updating ffmpeg or x264

If ffmpeg or x264 ever need to be updated, you need to update the code from the repository:

cd ~/ffmpegBuilds
#get recent ffmpeg changes
cd ffmpeg
git pull
cd ..
#get recent x264 changes
cd x264
git pull
cd ..

Then follow directions  from section “Compile ffmpeg without x264 Support and Install”.

Using ffmpeg to Encode Video for Connected TV Devices

This section describes how to encode a video using ffmpeg using the following steps:

  1. Verify your source format is supported.
  2. Create encoding configuration files.
  3. Run the transcoding commands.
  4. Change the position of the video metadata.

Verify your Source Format is Supported

Ok, let’s get started. First, determine if ffmpeg can read your video sources. To see if the video is supported, try playing a video with ffmpeg’s video player, ffplay using the following command:

ffplay <path_to_video>

Create Encoding Configuration Files

To avoid retyping the encoding options each time, use ffmpeg configuration files to make things a bit easier. For maximum compatibility and quality use 2-pass encoding.

Copy and paste the following text into a new file at “~/YahooConnectedTVPass1.ffconfig“:

#general
threads=0

#video
vcodec=libx264
flags=+loop
cmp=+chroma
partitions=+parti8x8+parti4x4+partp8x8+partp4x4+partb8x8
flags2=+dct8x8+wpred+bpyramid+mixed_refs
subq=5
trellis=1
refs=5
bf=16
b_strategy=1
bidir_refine=1
coder=1
g=250
me_range=16
me_method=hex
directpred=3
keyint_min=24
sc_threshold=40
i_qfactor=0.71
rc_eq='blurCplx^(1-qComp)'
qcomp=0.6
qmin=10
qmax=51
qdiff=4

Copy and paste the following text into a new file at “~/YahooConnectedTVPass2.ffconfig“:

#general
threads=0

#video
vcodec=libx264
flags=+loop
cmp=+chroma
partitions=+parti8x8+parti4x4+partp8x8+partp4x4+partb8x8
flags2=+dct8x8+wpred+bpyramid+mixed_refs
subq=5
trellis=1
refs=5
bf=16
b_strategy=1
bidir_refine=1
coder=1
g=250
me_range=16
me_method=hex
directpred=3
keyint_min=24
sc_threshold=40
i_qfactor=0.71
rc_eq='blurCplx^(1-qComp)'
qcomp=0.6
qmin=10
qmax=51
qdiff=4
deblockalpha=0
deblockbeta=0

#audio
acodec=libfaac
ac=2
ab=96000
ar=48000

Run the Transcoding Commands

First start the first encoding pass:

ffmpeg -i <Source_File> -fpre ~/YahooConnectedTVPass1.ffconfig -b 1000k -r 29.97 -s 640:480 -aspect 4:3 -an -async 500 -y -f mp4 -pass 1 /dev/null

Once the step above is done, run the second encoding pass:

ffmpeg -i <Source_File> -fpre ~/YahooConnectedTVPass2.ffconfig -r 29.97 -s 640x480 -aspect 4:3 -b 1000k -async 500 -y -f mp4 -pass 2 <Output_File>

The second encoding pass will take a long time, depending on your video source size. Once that is done, congratulations, you have encoded your first video! Your mp4/h.264 video will be the file you specified as <Output_File>.

Change the Position of the Video Metadata

The steps outlined above work on most 2010 Connected TV device models and web servers. To support video on older 2009 models and web servers that do not support file offsets, you need to move the video’s metadata to the front of the video file.

The problem is the metadata for the video, also known as the “moov atom,” is placed at the end of the file http://lists.mplayerhq.hu/pipermail/ffmpeg-user/2009-October/022482.html. This is only a problem when the file is larger than the Connected TV device can buffer at one time, so small files (~50MB) will not be a problem.

To fix this issue, use a tool called qt-faststart. To compile and install this tool do the following:

#compile qt-faststart
cd ~/ffmpegBuilds/ffmpeg
make tools/qt-faststart
#now install to file system
sudo cp ~/ffmpegBuilds/ffmpeg/tools/qt-faststart /usr/local/bin/

Now run your video through the tool:

qt-faststart <source_file> <output_file>

Your video is now compatible for progressive download on all 2010 LG, Samsung, and Vizio Connected TV device models and 2009 LG and Vizio Connected TV device models.

Changing Output Size and Bitrates

In the encoding commands above, there are a few flags to pay attention to. The two obvious ones source, and output are self-explanatory. The flags, “-b”, “-s”, and “-aspect” stand for Bitrate, Resolution, and Aspect Ratio, respectively. You will want to modify these as you see fit. Yahoo! Connected TV recommends choosing one resolution based on your video source size. Then, encode your video at all the suggested bitrates. The KONtx.mediaplayer interface will choose the best bitrate based upon the consumer’s connection speed.

Here are some more recommended values:

  • Bitrate (use all):
    • 300kbps, 700kbps, 1000kbps, 2000kbps
  • Resolution in pixels (choose one):
    • 1280×720 (for HD content only)
    • 720×480 (for high quality)
    • 640×480 (medium quality 4:3)
    • 640×360 (medium quality 16:9)
  • Aspect Ratio (choose one):
    • 4:3 (Standard)
    • 16:9 (Widescreen)

Special Thanks

— Jim Cortez and the Yahoo! Connected TV Team

One Response to “Transcoding Video for Yahoo! Connected TV Devices Using ffmpeg”

  1. michael m says:

    Replace “git clone git://git.ffmpeg.org/ffmpeg.git” with

    git clone git://git.videolan.org/ffmpeg.git

Leave a Reply