How to Run FFmpeg Video Conversion from PHP

To run an FFmpeg video conversion from PHP, use the shell_exec() PHP utility. Pass the FFmpeg command inside "" (double quotes) and any PHP variables needed inside '' (single quotes).

 

Basic FFmpeg Video Conversion Example

Here is a basic example for converting a video into an MP4 inside a PHP program.

 

$input_path = '/var/www/example.com/public/example.wmv';
$output_path = '/var/www/example.com/public/example.mp4';

shell_exec("ffmpeg -i '$input_path' '$output_path' ");

 

A More Comprehensive Example

Here is a more comprehensive example demonstrating scaling to 720p, specifying audio and video codecs and target bitrates:

 

$input_path = '/var/www/example.com/public/example.wmv';
$output_path = '/var/www/example.com/public/example.mp4';
$scale = 720;
$target_bitrate_video = 1200;
$target_bitrate_audio = 130;

shell_exec("ffmpeg -i '$input_path' -vf scale=-2:'$scale' -c:v libx264 -preset veryfast -c:a aac -b:v '$target_bitrate_video'k -b:a '$target_bitrate_audio'k '$output' ");
video conversion