In PHP projects, the FFmpeg extension enables handling audio and video files, allowing you to perform tasks like encoding, decoding, editing, and converting. This article outlines the steps to install the PHP FFmpeg extension and provides related configuration and code examples.
First, ensure that PHP and FFmpeg are installed on your server. If FFmpeg is not installed, follow the steps below to install it:
Start by updating the system package list:
sudo
apt update
Then, install FFmpeg:
sudo
apt install ffmpeg
Next, download the PHP FFmpeg extension source code and unzip it:
wget https://github.com/PHP-FFMpeg/PHP-FFMpeg/archive/master.zip
unzip master.zip
cd
PHP-FFMpeg-master
Install the necessary dependencies:
sudo
apt install php-dev
sudo
apt install pkg-config
sudo
apt install yasm
Next, compile and install the PHP FFmpeg extension with the following commands:
phpize
./configure
make
sudo make install
Add the FFmpeg extension to your PHP configuration file (php.ini):
sudo
echo
"extension=ffmpeg.so"
>>
/etc/php/7.x/cli/php.ini
sudo
echo
"extension=ffmpeg.so"
>>
/etc/php/7.x/apache2/php.ini
Replace `7.x` with your PHP version number.
To apply the configuration, restart Apache or PHP-FPM services:
sudo
service apache2 restart
sudo
service php7.x-fpm restart
Once the installation is complete, you can write a simple PHP script to test the FFmpeg extension's functionality. Here's a basic example:
<?php
use
FFMpegFFMpeg;
use
FFMpegFFProbe;
require
'vendor/autoload.php'
;
$ffmpeg
= FFMpeg::create();
$video
=
$ffmpeg
->open(
'path/to/video.mp4'
);
$video
->filters()
->resize(
new
FFMpegCoordinateDimension(320, 240))
//Additional processing
->synchronize();
$video
->save(
new
FFMpegFormatVideoX264(),
'path/to/output.mp4'
);
?>
This guide has covered the steps to install the PHP FFmpeg extension on your server. By installing this extension, you can handle audio-video files in your PHP projects, offering greater functionality and flexibility to your applications.