ffmpeg installation on Mac OSX
ffmpeg installation on Mac OSX
This article explains how to install ffmpeg and ffmpeg-php on Mac OS X. ffmpeg is a command line tool used to convert video and audio formats. It allows you to convert audio and video between any of its supported formats.
ffmpeg-php is an extension for php that allows you to access information about the movies/videos from within php. The actual commands to convert the video must still be executed with functions such as exec() or system(), but by having ffmpeg-php you can test the video(say to verify an uploaded file is actually a video) and gain access to information that you would not have otherwise.
My primary interest in ffmpeg is for converting videos of various formats into a flash video format for display on websites. This setup works to allow me to develop scripts on my locally on my mac that I later use on my linux server.
My setup for this install is as follows:
Mac OS X 10.4.11
Marc Linyanages' (entropy) PHP version 5.2.4
I have not tested this on either different versions of php or Mac OS X. But this is a successful install with the latest libraries for this setup.
This installation process will be conducted from a terminal shell. If you are completely new to this I suggest that you read up on linux shell commands first as the entire process will take place in Terminal.
Prerequisites
Apple Developer Tools
To have access to the command line tools necessary to install ffmpeg and its required tools you need to have Apple's Developer Tools installed on your Mac. This will give you the C development tools you need to run commands such as 'configure", "make", and "make install".
The Apple Developer Tools package is located on the install disk for your Mac in a folder named Xcode Tools. Within the Xcode folder you will find a file named "XcodeTools.mpkg". Double click this package to begin the install and proceed as normal. Note: the Xcode Tools package is rather large, and will take up around 2 gigs of disk space. It’s a necessary price to pay if you want to develop with these tools.
Ensuring PHP Continuity
We need to make sure that your terminal and your server are using the same php binary. If you are using the php setup that came with your Mac this should not be a problem. If however you are using a version that you installed, either from entropy or MAMP for example, then your command line will be pointing to a different php than you are using with your server.
You can find this out by running a phpinfo() script and comparing the values against values returned by running "php -i" from your terminal. If they are different versions you will need to modify the '/usr/bin/php-config' file to contain the appropriate values and move/remove /usr/bin/php and replace it with a symlinc to your active php binary, such as:
$ mv /usr/bin/php /usr/bin/php_bak
$ ln -s /path/to/your/phps/bin/php /usr/bin/php
While your at it you will need to do the same with phpize:
$ mv /usr/bin/phpize /usr/local/phpize_bak
$ ln -s /path/to/your/phps/bin/phpize /usr/bin/phpize
Doing this allows the terminal to point to the correct php when called for by the ffmpeg-php configure script.
make – version 3.81 or later
ffmpeg requires you to make and install it via the shell. Due to a broken backward compatibility issue you will need to upgrade your version of "make" to version 3.81 or newer if you want to install the latest version of ffmpeg. We will include this step in the tutorial.
Setting Up our Directories and Downloading Packages
We will be installing these packages in ‘/usr/local/’. First we will create a directory for our source files. The dollar sign($) begining a line in this tutorial represents the command line prompt.
$ cd /usr/local
$ sudo mkdir ./src
$ cd ./src
Install wget
I preffer working with wget over curl, it seems to work better for me in getting these packages. First we have to download and install it though, which we will use curl to do.
$ curl -O http://ftp.gnu.org/pub/gnu/wget/wget-1.9.1.tar.gz
$ tar -xzvf wget-1.9.1.tar.gz
$ cd wget-1.9.1
$ ./configure
$ make
$ sudo make install
$ ln -s /usr/local/bin/wget /usr/bin/wget
Download the necessary packages.
We will do this from the command line using wget, but you can download it however you want and then transfer the files to the src folder we created earlier.
$ wget http://promotionalpromos.com/mirrors/gnu/gnu/make/make-3.81.tar.gz
$ wget http://sourceforge.net/projects/lame/files/lame/3.97/lame-3.97.tar.gz/download
$ wget http://downloads.sourceforge.net/faac/faad2-2.7.tar.gz
$ wget http://ffmpeg.org/releases/ffmpeg-0.5.tar.bz2
$ wget http://rubyforge.org/frs/download.php/17497/flvtool2-1.0.6.tgz
$ wget http://sourceforge.net/projects/ffmpeg-php/files/ffmpeg-php/0.6.0/ffmpeg-php-0.6.0.tbz2/download
Now that we have all our necessary stuff we will extract the packages that we downloaded so we can start installing them.
$ tar -xzvf make-3.81.tar.gz
$ tar -xzvf lame-3.97.tar.gz
$ tar -xzvf faad2-2.7.tar.gz
$ bunzip2 ffmpeg-0.5.tar.bz2
$ tar -xf ffmpeg-0.5.tar
$ bunzip2 ffmpeg-php-0.6.0.tbz2
$ tar -xf ffmpeg-php-0.6.0.tar
Installing make
$ cd /usr/local/src/make-3.81
$ ./configure
$ make
$ sudo make install
Installing lame
lame is an mp3 encoder. Since Flash video usually uses mp3 as its audio format and ffmpeg doesn't include and mp3 encoder you will need to install lame and tell ffmpeg to use it.
$ cd /usr/local/src/lame-3.97
$ ./configure
$ make
$ sudo make install
Installing faad
Quicktime videos use AAC as its audio codec, so to encode and decode in this format you will need to install faad and tell ffmpeg to use it.
$ cd /usr/local/src/faad2-2.7
$ ./configure
$ make
$ sudo make install
Installing ffmpeg
When configuring ffmpeg we need to tell it to use it as a shared library and to enable lame and faad (faad requires the --enable-gpl as well). This configuration worked well for me.
$ cd /usr/local/src/ffmpeg-0.5
$ ./configure --enable-shared --enable-libmp3lame --enable-gpl --enable-libfaad
$ make
$ sudo make install
$ sudo ln -s /usr/local/bin/ffmpeg /usr/bin/ffmpeg
Now from your terminal enter:
$ ffmpeg -version
You should get something similar to this:
FFmpeg version 0.5, Copyright (c) 2000-2009 Fabrice Bellard, et al.
configuration: --enable-shared --enable-libmp3lame --enable-libfaad --enable-gpl
libavutil 49.15. 0 / 49.15. 0
libavcodec 52.20. 0 / 52.20. 0
libavformat 52.31. 0 / 52.31. 0
libavdevice 52. 1. 0 / 52. 1. 0
built on Jul 24 2009 21:22:05, gcc: 4.0.1 (Apple Computer, Inc. build 5250)
FFmpeg 0.5
libavutil 49.15. 0 / 49.15. 0
libavcodec 52.20. 0 / 52.20. 0
libavformat 52.31. 0 / 52.31. 0
libavdevice 52. 1. 0 / 52. 1. 0
Congratulations! you have successfully installed ffmpeg! Check below for an example of how to execute commands to convert video from the terminal. You will be able to execute these same commands from within your php scripts with the exec() function.
Installing ffmpeg-php
If you want to have access to the functionality provided by ffmpeg-php you can go ahead and install it now. Heres how:
$ cd /usr/local/src/ffmpeg-php-0.6.0
$ phpize
$ ./configure --with-ffmpeg
$ make
$ sudo make install
Restart your server
$ sudo apachectl restart
Installing flvtool2
flvtool2 adds necessary meta data to your Flash videos to make them a more proper file and provide to flash player with information about the video.
$ cd /usr/local/src/flvtool2_1.0.5_rc6
$ ruby setup.rb config
$ ruby setup.rb setup
$ sudo ruby setup.rb install
Possible Problems
ffmpeg headers not found.
If while configuring ffmpeg-php you encounter this error, which should hopefully not happen when working with these versions, the problem lies with ffmpeg-php looking in '/usr/local/include/ffmpeg/' instead of '/usr/local/include/'. To fix this try this:
$ 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
unkown token near ('ffmpeg,' )
If you get this error it lies with a compatibility issue with the ffmpeg-php compilers between new and old versions of php. The problem is with the configure script trying to load an extension which is no longer used in php. to fix it:
$ cd /usr/local/src/ffmpeg-0.6.0
$ vim config.m4 +/PHP_ADD_EXTENSION_DEP
add 'dnl' without the quotes (stands for 'do not load') before and save and try again. this should fix it.
Note: These problems only existed when my systems recognition of the php and phpize binaries and php-config weren't set correctly. When I removed all components to try the install again from scratch everything worked fine with no errors during configure, make or make install of any step of this entire procedure.
Also make sure that you get a copy of lame 3.97. When I tried it with 3.98 I would get an audio conversion error when converting my videos to flash. The only problem I saw with the files that where output was that they would not tell the browser to total length of the file and therefor the video progress bar wasn't functional.
ffmpeg conversion example
I will give you an example of a basic conversion command. Put a video on your desktop. We are going to tell ffmpeg to convert this video into a flash video format and then pipe it through flvtool2 to add the meta data. For an explanation of these commands run 'man ffmpeg' from terminal.
$ ffmpeg -i /Users/your~username/Desktop/moviename.ext -s 320x240 -sameq -ab 32000 -ar 22050 -f flv /Users/your~username/Desktop/moviename.flv | flvtool2 -U /Users/your~username/Desktop/moviename.flv
Have fun!