Current Location: Home> Latest Articles> How to Install PHP FFmpeg Extension on Your Server and Handle Audio-Video Files

How to Install PHP FFmpeg Extension on Your Server and Handle Audio-Video Files

M66 2025-07-27

How to Install PHP FFmpeg Extension on Your Server and Handle Audio-Video Files

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.

Prerequisites

First, ensure that PHP and FFmpeg are installed on your server. If FFmpeg is not installed, follow the steps below to install it:

Install FFmpeg

Start by updating the system package list:

sudo apt update

Then, install FFmpeg:

sudo apt install ffmpeg

Install PHP FFmpeg Extension

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

Compile and Install the PHP FFmpeg Extension

Next, compile and install the PHP FFmpeg extension with the following commands:

phpize

./configure

make

sudo make install

Configure PHP to Enable FFmpeg Extension

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.

Restart Services to Apply the Configuration

To apply the configuration, restart Apache or PHP-FPM services:

sudo service apache2 restart

sudo service php7.x-fpm restart

Test the FFmpeg Extension

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');

?>

Conclusion

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.