Ubuntu Forums ubuntu.com - launchpad.net - ubuntu help  

Go Back   Ubuntu Forums > The Ubuntu Forum Community > Other Community Discussions > Tutorials & Tips
Register Reset Password Forum Help Forum Council Search Today's Posts Mark Forums Read

Ubuntu 9.10 is out!!!

When downloading Ubuntu 9.10 please consider using bittorrent to get your copy of Ubuntu.

The Ubuntu Developers Summit for Lucid Lynx will be held the week of 16-Nov-2009 till 20-Nov-2009 in Dallas, TX USA. Visit the the Ubuntu wiki for more information about UDS and how to participate remotely.

Tutorials & Tips
The place to find Ubuntu related Tips & Tricks.

 
Thread Tools Display Modes
Old May 7th, 2008   #1
FakeOutdoorsman
May the Ubuntu Be With You!
 
FakeOutdoorsman's Avatar
 
Join Date: Sep 2006
Location: Alaska
Beans: 1,585
HOWTO: Install and use the latest FFmpeg and x264

FFmpeg is a versatile tool to encode and convert a multitude of video and audio formats. x264 encodes high-quality video superior to other popular encoders.

Although FFmpeg and x264 are available in the Ubuntu repositories, you may need to compile from source. For example, the ffmpeg-user mailing list requires that you use the latest FFmpeg svn before asking for help. Since FFmpeg and x264 are updated frequently, you may also like to have the bleeding-edge for encoding videos. Also, FFmpeg in the Ubuntu repository may not support necessary encoders, decoders, and formats.

Note: Those who are uncomfortable compiling can still enable restricted encoders in the repository FFmpeg:
HOWTO: Easily enable MP3, MPEG4, AAC, and other restricted encoding in FFmpeg


Choose your Ubuntu
0.The instructions on the page are for Ubuntu Karmic Koala 9.10. Separate instructions are also available for older releases:
Install the Dependencies
1. Uninstall x264, libx264-dev, and ffmpeg if they are already installed. Open a terminal and run the following:
Code:
sudo apt-get remove ffmpeg x264 libx264-dev
2. Next, get all of the packages you will need to install FFmpeg and x264 (you may need to enable the universe and multiverse repositories):
Code:
sudo apt-get update
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 libtheora-dev libx11-dev libxvidcore4-dev zlib1g-dev

Install x264
3. Get the most current source files from the official x264 git repository, compile, and install. You can run "./configure --help" to see what features you can enable/disable. If you are behind a firewall or unable to use git, then daily source tarballs are also available.
Code:
cd
git clone git://git.videolan.org/x264.git
cd x264
./configure
make
sudo checkinstall --pkgname=x264 --pkgversion "1:0.svn`date +%Y%m%d`" --backup=no --default

Install FFmpeg
4. Get the most current source files from the official FFmpeg svn, compile, and install. Run "./configure --help" to see what features you can enable/disable. If you are behind a firewall or unable to use subversion, then nightly FFmpeg snapshots are also available.
Code:
cd
svn checkout svn://svn.ffmpeg.org/ffmpeg/trunk ffmpeg
cd ffmpeg
./configure --enable-gpl --enable-version3 --enable-nonfree --enable-pthreads --enable-libfaac --enable-libfaad --enable-libmp3lame --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libtheora --enable-libx264 --enable-libxvid --enable-x11grab
make
sudo checkinstall --pkgname=ffmpeg --pkgversion "4:0.5+svn`date +%Y%m%d`" --backup=no --default
That's it for installation. You can keep the ~/x264 and ~/ffmpeg directories if you later want to update the source files to a new revision. See "Updating Your Installation" below for more details.


Using FFmpeg and x264
The easiest method for high quality video encoding is by using the libx264 presets that are included with FFmpeg. I recommend reading the FFmpeg x264 encoding guide before trying these presets so you have a better idea of what to use. You can add options such as frame size (for example: -s 640x480) or tweak my examples to customize your encode. You can see a current list of other presets, such as lossless and ipod, available in the /usr/share/ffmpeg directory.

One-pass CRF (Constant Rate Factor) using the hq preset. This is good for general encoding and is what I use most often. Adjust -crf to change the quality. Lower numbers mean higher quality and a larger output file size. A sane range is 18 to 28.
Code:
ffmpeg -i input.avi -acodec libfaac -ab 128k -ac 2 -vcodec libx264 -vpre hq -crf 22 -threads 0 output.mp4
Two-Pass encode using the fastfirstpass and hq presets. Two-pass encoding is used when you are targeting a specific bitrate and/or final output file size. It is often used for portable devices such as iPod because some have bitrate limits.
Code:
ffmpeg -i input.avi -pass 1 -vcodec libx264 -vpre fastfirstpass -b 512k -bt 512k -threads 0 -f rawvideo -an -y /dev/null && ffmpeg -i input.avi -pass 2 -acodec libfaac -ab 128k -ac 2 -vcodec libx264 -vpre hq -b 512k -bt 512k -threads 0 output.mp4
Two-pass iPod 640x480 using the fastfirstpass, hq, and ipod640 presets:
Code:
ffmpeg -i input.avi -pass 1 -an -vcodec libx264 -vpre fastfirstpass -vpre ipod640 -b 512k -bt 512k -s 640x480 -threads 0 -f rawvideo -y /dev/null && ffmpeg -i input.avi -pass 2 -acodec libfaac -ab 128k -ac 2 -vcodec libx264 -vpre hq -vpre ipod640 -b 512k -bt 512k -s 640x480 -threads 0 output.mp4
You may have to use qt-faststart, MP4Box, or AtomicParsley to make the video iTunes friendly. I don't use iTunes or iPod so I can't test this. More info and other usage examples: iPod Video Guide.


Updating Your Installation
Development of FFmpeg and x264 is active and an occasional update can give you new features and bug fixes. To update FFmpeg and x264 you will need to remove the packages, make distclean, update the source, recompile, and install. To update x264:
Code:
sudo apt-get remove ffmpeg x264 libx264-dev
cd ~/x264
make distclean
git pull
Now compile x264 as shown earlier in the guide starting with the x264 ./configure line. Now FFmpeg:
Code:
cd ~/ffmpeg
make distclean
svn update
Now continue with the installation starting with the FFmpeg ./configure line.


Reverting Changes Made by This Tutorial
To remove FFmpeg/x264 and any changes made from this tutorial:
Code:
sudo apt-get remove x264 ffmpeg build-essential subversion git-core checkinstall yasm texi2html libfaac-dev libfaad-dev libmp3lame-dev libsdl1.2-dev libtheora-dev libx11-dev libxvidcore4-dev zlib1g-dev
Lastly, delete the ffmpeg and x264 directories in your home folder.

Additional Resources
If You Need Help
Feel free to ask your questions here and I'll try to give you a good answer. Helpful information includes your Ubuntu version, names of any third-party repositories or PPAs you are using, FFmpeg command, and the complete FFmpeg output if applicable.

Recent Tutorial Updates
2009-11-18: Split Karmic section from other Ubuntu versions due to the addition of libopencore-amr to the guide.

Last edited by FakeOutdoorsman; 1 Day Ago at 09:57 PM..
FakeOutdoorsman is online now   Reply With Quote
Old May 12th, 2008   #2
Paerez
100% Pure Ubuntu
 
Paerez's Avatar
 
Join Date: Jan 2005
Location: Baltimore, MD, USA
Beans: 857
Hardy Heron (Ubuntu Development)
Send a message via AIM to Paerez
Re: HOWTO: Compile the latest ffmpeg and x264 from source

Great guide. Thanks a lot. One problem:

When I configure x264 I get the error:
Code:
./configure --enable-pthread --enable-mp4-output --enable-shared
No suitable assembler found.  x264 will be several times slower.
Please install 'yasm' to get MMX/SSE optimized code.
I tried nasm and yasm from the repos but they didn't work

But I figured it out!

All I had to do was go to:
http://www.tortall.net/projects/yasm/wiki/Download

Grab the 0.7.0 tarball, then ./configure, make, sudo checkinstall, then build x264, then ffmpeg. Now I can encode much faster.

A note to those who are interested:

I used the ffmpeg encoding script:
Code:
#!/bin/sh
ffmpeg -i $1 -y -an -pass 1 -vcodec libx264 -threads 4 -b 1024kbps -flags +loop -cmp +chroma -partitions +parti4x4+partp8x8+partb8x8 -me epzs -subq 1 -trellis 0 -refs 1 -bf 3 -b_strategy 1 -coder 1 -me_range 16 -g 250 -keyint_min 25 -sc_threshold 40 -i_qfactor 0.71 -rc_eq 'blurCplx^(1-qComp)' -qcomp 0.6 -qmin 10 -qmax 51 -qdiff 4 $2

ffmpeg -i $1 -y -acodec libfaac -ab 128k -pass 2 -vcodec libx264 -threads 4 -b 1024kbps -flags +loop -cmp +chroma -partitions +parti4x4+partp8x8+partb8x8 -flags2 +mixed_refs -me umh -subq 5 -trellis 1 -refs 5 -bf 3 -b_strategy 1 -coder 1 -me_range 16 -g 250 -keyint_min 25 -sc_threshold 40 -i_qfactor 0.71 -rc_eq 'blurCplx^(1-qComp)' -qcomp 0.6 -qmin 10 -qmax 51 -qdiff 4 $2
Which can be run:
Code:
./scriptname myfile.avi myfile.mp4
And the result plays in Flash 9 using their h264 playback stuff. Pretty neat and high quality too.
__________________
Knowledge is half the battle.
The other half is violence!
Paerez is offline   Reply With Quote
Old May 12th, 2008   #3
FakeOutdoorsman
May the Ubuntu Be With You!
 
FakeOutdoorsman's Avatar
 
Join Date: Sep 2006
Location: Alaska
Beans: 1,585
Re: HOWTO: Compile the latest ffmpeg and x264 from source

Quote:
Originally Posted by Paerez View Post
Great guide. Thanks a lot. One problem:

When I configure x264 I get the error:
Code:
./configure --enable-pthread --enable-mp4-output --enable-shared
No suitable assembler found.  x264 will be several times slower.
Please install 'yasm' to get MMX/SSE optimized code.
I tried nasm and yasm from the repos but they didn't work

...
Thanks for pointing that out. I'll update it with a section on yasm compilation. Was the error the same if you tried using just nasm?

As of March 14, '08 (I think), x264 requires a newer yasm >= 0.6.0 than what is in the Ubuntu universe repository (yasm 0.5.0). x264 should fallback to nasm if yasm isn't detected and worked for me just fine despite the following error:
Code:
./configure: 330: yasm: not found
If I omitted nasm, I got the same error as you. I'm not sure why nasm worked for me and not for you.

Before I wrote this tutorial I tested the speed of x264 either using a compiled yasm 0.7.0, nasm from the repository, or no assembler. The speed differences were very small between compiled yasm and nasm from the repo:
x264/mencoder/ffmpeg on a dual quad core.

Edit: Tutorial updated with yasm compilation.

Last edited by FakeOutdoorsman; May 12th, 2008 at 07:11 PM..
FakeOutdoorsman is online now   Reply With Quote
Old May 14th, 2008   #4
Paerez
100% Pure Ubuntu
 
Paerez's Avatar
 
Join Date: Jan 2005
Location: Baltimore, MD, USA
Beans: 857
Hardy Heron (Ubuntu Development)
Send a message via AIM to Paerez
Re: HOWTO: Compile the latest ffmpeg and x264 from source

I tried using both nasm and yasm from the repos, and x264 would compile but it would say it was building without support for yasm and nasm.

Then, when I used it to convert a file it would say that it was using no cpu extensions, as opposed to saying "MMX SSE ...". It would also take significantly longer (like 2-5 times as long).

But just installing the yasm from source fixed it, and it wasn't very difficult to do.
__________________
Knowledge is half the battle.
The other half is violence!
Paerez is offline   Reply With Quote
Old June 24th, 2008   #5
ractalfece
First Cup of Ubuntu
 
Join Date: Apr 2006
Beans: 1
Re: HOWTO: Compile the latest ffmpeg and x264 from source

Thanks a million. Great in depth job on this one.
ractalfece is offline   Reply With Quote
Old June 26th, 2008   #6
d_mcqueen
First Cup of Ubuntu
 
Join Date: Jan 2008
Beans: 1
Re: HOWTO: Compile the latest ffmpeg and x264 from source

This is a great howto, since in my opinion ffmpeg is broken due to these issues.

Therefore, what are the additional steps to fully replace ffmpeg, x64 and the libx264 packages such that it satisfies dependencies for other applications that use ffmpeg and my update manager does not keep asking me to upgrade from the checkinstall packages?
d_mcqueen is offline   Reply With Quote
Old June 26th, 2008   #7
Nais
First Cup of Ubuntu
 
Join Date: Jul 2005
Beans: 8
Re: HOWTO: Compile the latest ffmpeg and x264 from source

When I try to "make" x264, I get this error.
Code:
/usr/bin/ld: common/mc.o: relocation R_X86_64_32S against `a local symbol' can not be used 
when making a shared object; recompile with -fPIC
common/mc.o: could not read symbols: Bad value
collect2: ld returned 1 exit status
make: *** [libx264.so.60] Error 1
I'm on 64-bit Ubuntu, is there something else I should be doing? Dunno what "-fPIC" means.

UPDATE: Never mind. Cleaned up my folder of a previous failed build attempt and it seems to be okay now. Thanks for the guide!

Last edited by Nais; June 26th, 2008 at 01:08 PM..
Nais is offline   Reply With Quote
Old June 26th, 2008   #8
FakeOutdoorsman
May the Ubuntu Be With You!
 
FakeOutdoorsman's Avatar
 
Join Date: Sep 2006
Location: Alaska
Beans: 1,585
Re: HOWTO: Compile the latest ffmpeg and x264 from source

Quote:
Originally Posted by d_mcqueen View Post
This is a great howto, since in my opinion ffmpeg is broken due to these issues.

Therefore, what are the additional steps to fully replace ffmpeg, x64 and the libx264 packages such that it satisfies dependencies for other applications that use ffmpeg and my update manager does not keep asking me to upgrade from the checkinstall packages?
I believe your compiled versions should satisify as dependencies for other packages as long as the packages are named correctly and compiling options that are required by the parent package are enabled. I don't use update-manager so I can't give instruction on that, but you can try:
Code:
sudo aptitude hold ffmpeg
If you prefer Synaptic, then select the package and then choose "Package -> Lock Version" from the menu. I haven't tested either of these commands, so I'm unsure how effective they are.
FakeOutdoorsman is online now   Reply With Quote
Old June 26th, 2008   #9
cor2y
Dipped in Ubuntu
 
cor2y's Avatar
 
Join Date: Dec 2005
Beans: 644
Ubuntu 9.10 Karmic Koala
Re: HOWTO: Compile the latest ffmpeg and x264 from source

Yes locking versions work but how about trying to get synaptic to see that you installed libx264 via the source code with checkinstall so far according to synaptic i only have x264 the binary installed and not libx264 and libx264-dev so now other multimedia apps will not install without using the repos version of libx264 if i try to install via synaptic
cor2y is offline   Reply With Quote
Old June 28th, 2008   #10
FakeOutdoorsman
May the Ubuntu Be With You!
 
FakeOutdoorsman's Avatar
 
Join Date: Sep 2006
Location: Alaska
Beans: 1,585
Re: HOWTO: Compile the latest ffmpeg and x264 from source

Quote:
Originally Posted by cor2y View Post
Yes locking versions work but how about trying to get synaptic to see that you installed libx264 via the source code with checkinstall so far according to synaptic i only have x264 the binary installed and not libx264 and libx264-dev so now other multimedia apps will not install without using the repos version of libx264 if i try to install via synaptic
When you compile x264 it should create the same files that libx264 and libx264-dev would create. If you add "--prefix=/usr" to the x264 configure line then these files will also install in the same location as the repo versions instead of the default /usr/local/lib. I'm unsure how to get Synaptic or apt to recognize this. I'm out of town until July 4 and can't test anything until then.
FakeOutdoorsman is online now   Reply With Quote

Bookmarks

Tags
encode, ffmpeg, h264, libx264, x264

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 06:12 PM.


vBulletin ©2000 - 2009, Jelsoft Enterprises Ltd. Ubuntu Logo, Ubuntu and Canonical © Canonical Ltd. Tango Icons © Tango Desktop Project. bilberry