Monday, May 25, 2009

Installing FFmpeg and FFmpeg-php

System Configuration/Prerequisites

The server that I had to install on was running:

  • Centos 5.x
  • cPanel 11 (but of course, you don’t need cPanel to install FFmpeg and ffmpeg-php)
  • Yum (or anything of the likes would do)
  • And I presume you have root access

Getting the required Files

Firstly, we would have to download all the required files to a folder. For me, I picked /usr/local/src, but it could be just any other folder you’d like.

Move into the directory that you’d like to download the source files to and run the following commands:

wget http://www3.mplayerhq.hu/MPlayer/releases/codecs/essential-20061022.tar.bz2
wget http://www4.mplayerhq.hu/MPlayer/releases/MPlayer-1.0rc2.tar.bz2
wget http://rubyforge.org/frs/download.php/17497/flvtool2-1.0.6.tgz
wget http://downloads.xiph.org/releases/ogg/libogg-1.1.3.tar.gz
wget http://downloads.xiph.org/releases/vorbis/libvorbis-1.1.2.tar.gz
wget http://easynews.dl.sourceforge.net/sourceforge/lame/lame-3.97.tar.gz
wget http://easynews.dl.sourceforge.net/sourceforge/ffmpeg-php/ffmpeg-php-0.5.3.1.tbz2

This is what I used in my setup. Additional libraries should be downloaded if you need them. Also, I wouldn’t say that these are the latest binaries, but they work for me for now - you should check for updates if you wish. You may also want to change the location of the SourceForge downloads.

Now extract all the files you’ve downloaded:

bunzip2 essential-20061022.tar.bz2; tar xvf essential-20061022.tar
bunzip2 MPlayer-1.0rc2.tar.bz2 ; tar -xvf MPlayer-1.0rc2.tar
tar zxvf flvtool2-1.0.6.tgz
tar zxvf libogg-1.1.3.tar.gz
tar zxvf libvorbis-1.1.2.tar.gz
tar zxvf lame-3.97.tar.gz
bunzip2 ffmpeg-php-0.5.3.1.tbz2; tar xvf ffmpeg-php-0.5.3.1.tar

Along with the additional libraries you could have possibly downloaded.

Installation

Now that we’re done downloading the files we need, it’s time to start the installation.

Codecs

Create a folder to store the Codecs that ffmpeg will need:

mkdir /usr/local/lib/codecs/
mv essential-20061022/* /usr/local/lib/codecs/
chmod -Rf 755 /usr/local/lib/codecs/

Subversion/Ruby/

yum install subversion
yum install ruby
yum install ncurses-devel

LAME

cd lame-3.97
export LD_LIBRARY_PATH=/usr/local/lib
./configure
make
make install

libogg

cd libogg-1.1.3
./configure
make
make install

libvorbis

cd libvorbis-1.1.2
./configure
make
make install

flvtool2

cd flvtool2-1.0.6
ruby setup.rb config
ruby setup.rb setup
ruby setup.rb install

MPlayer

cd MPlayer-1.0rc2
./configure
make
make install

Now for the big one, ffmpeg.

Installing FFmpeg

Generally, we would install the latest version of ffmpeg. But as of the date of this post, the latest version of ffmpeg does not wok with ffmpeg-php. We will need to checkout a previous version from the SVN.

Inside your src folder, run the following to checkout this version of ffmpeg which works.

svn checkout svn://svn.mplayerhq.hu/ffmpeg/trunk/ ffmpeg -r15261

Once it is done downloading, go into the ffmpeg folder

Note: If a version of ffmpeg was previously installed, run the following command in the ffmpeg folder first:

make uninstall
make clean

Start the installation. You may need to add/enable additional libraries into ./configure.

cd ffmpeg
./configure --enable-libmp3lame --enable-libvorbis --disable-mmx --enable-shared
make
make install

During installation, you may get an error like:

Unable to create and execute files in /tmp. Set the TMPDIR environment
variable to another directory and make sure that /tmp is not mounted
noexec.
Sanity test failed.

To fix this, run the following commands to create a temporary tmp directory:

mkdir tmp
chmod 777 tmp
export TMPDIR=./tmp

After installation, remember to change the tmp directory back to your server’s tmp disk by doing the following:

export TMPDIR=/tmp

Check if ffmpeg is working by running the following:

ffmpeg -version

It should return something similar to:

FFmpeg version SVN-r15261, Copyright (c) 2000-2008 Fabrice Bellard, et al.
configuration: --enable-libmp3lame --enable-libvorbis --disable-mmx --enable-shared
libavutil 49.10. 0 / 49.10. 0
libavcodec 51.71. 0 / 51.71. 0
libavformat 52.22. 1 / 52.22. 1
libavdevice 52. 1. 0 / 52. 1. 0

If the version isn’t “SVN-r15261″, then there is something wrong and you may need to reinstall ffmpeg.

If something like the following is returned:

ffmpeg: error while loading shared libraries: libavdevice.so.52: cannot open shared object file: No such file or directory

Search for the missing library (in this case libaddevice.so.52) with the following:

find / -name 'libavdevice.so.*'

The returned results may look something like this. The one that we want is the one in the lib folder and not the src folder.

/usr/local/src/ffmpeg/libavdevice/libavdevice.so.52
/usr/local/lib/libavdevice.so.52
/usr/local/lib/libavdevice.so.52.1.0

With that, since the libraries are in the /usr/local/lib/ folder, we run the following:

export LD_LIBRARY_PATH=/usr/local/lib/

Otherwise, simply change the stuff after “=” to the path where the library is.

Sweet! Now that we’ve got FFmpeg working successfully, time to install ffmpeg-php.

Installing ffmpeg-php

Start the installation by running the following (again, run make clean as necessary):

cd ffmpeg-php-0.5.3.1
phpize
./configure
make
make install

You may meet the following errors during installation:

checking for ffmpeg headers… configure: error: ffmpeg headers not found. Make sure you’ve built ffmpeg as shared libs using the –enable-shared option

Solution: Simply create a ffmpeg folder in /usr/local/include/ and run the following to copy all the header files:

cp /usr/local/include/libavcodec/* /usr/local/include/ffmpeg
cp /usr/local/include/libavdevice/* /usr/local/include/ffmpeg
cp /usr/local/include/libavformat/* /usr/local/include/ffmpeg
cp /usr/local/include/libavutil/* /usr/local/include/ffmpeg
cp /usr/local/include/libswscale/* /usr/local/include/ffmpeg

make: *** [ffmpeg_frame.lo] Error 1

Solution: Execute the following in the ffmpeg-php folder. I’ve no idea why the files are named wrongly too.

cp ffmpeg_frame.loT ffmpeg_frame.lo

Upon successful installation, the installer will give you a very long sting to tell you where the extension was installed to. This is a unique string. The following is what I got:

Installing shared extensions: /usr/local/lib/php/extensions/no-debug-non-zts-20060613/

This has to be added to into php.ini.
In cPanel Servers, the php.ini file is located in /usr/local/lib. Otherwise, it should be in etc/php.ini.

Add the following line into php.ini (at the bottom or wherever you want):

extension=ffmpeg.so

Finalizing Installation

Restart Apache on the server.

service httpd restart

Check that ffmpeg is running on the server:

php -i | grep ffmpeg

The following should be returned:

ffmpeg
ffmpeg support (ffmpeg-php) => enabled
ffmpeg-php version => 0.5.3.1
ffmpeg-php gd support => enabled
ffmpeg.allow_persistent => 0 => 0

If an error like the following appears:

php: symbol lookup error: /usr/local/lib/php/extensions/no-debug-non-zts-20060613/ffmpeg.so: undefined symbol: av_free_static

Simply rebuild ffmpeg-php and it should work again.

And you’re done!

You have just successfully installed FFmpeg and ffmpeg-php on your server!

Tuesday, May 12, 2009

SSH key- passwordless login

On the source server do the following

ssh-keygen -t dsa -f filename

It will prompt for a passphrace, it is desirable to leave it empty.
Two files will be created

filename
filename.pub

copy the filename.pub file to the destination server to the location /root/.ssh add the public key entry into authorized_keys as follows

cat filename.pub >> authorized_keys

/etc/init.d/sshd restart