VEXXHOST Logo
Purple pattern background

How to convert/encode files to FLV using FFMPEG & PHP

Mohammed NaserMohammed Naser

So, as I've written in an earlier article on how to install FFMPEG on your server, while there are those who probably use a “YouTube Clone” script, there might be those who want to create their own using FFMPEG & PHP. FLV is the most widely used type of codec that runs on most Flash players. So, let's get started, there are actually a few steps into converting a file to FLV which are shown below1. Send the script to FFMPEG-PHP and get it's info
So, before doing any of this, you should make sure that your file has been uploaded to somewhere and you have the full path to it. (You can't use what you have in "memory", so you'll have to look on how to upload a file, once you got that and have the path of the file, we'll start our script to invoke FFMPEG-PHP and get the file's resolution. What we mainly need is the width, height & FPS (frame per second) so that we can tell FFMPEG about. I'll be using the clock.avi located in every windows system.

We'll start out our code with getting our variables:

<?php
// Set our source file  
$srcFile = "/path/to/clock.avi";  
$destFile = "/path/to/clock.flv";  
$ffmpegPath = "/path/to/ffmpeg";  
$flvtool2Path = "/path/to/flvtool2";

// Create our FFMPEG-PHP class  
$ffmpegObj = new ffmpeg_movie($srcFile);

// Save our needed variables  
$srcWidth = makeMultipleTwo($ffmpegObj->getFrameWidth());  
$srcHeight = makeMultipleTwo($ffmpegObj->getFrameHeight());  
$srcFPS = $ffmpegObj->getFrameRate();
?>

Also, the width/height has to be multiples of two so I have created a function that makes it a multiple of two:

<?php
function makeMultipleTwo ($value)  
{  
  $sType = gettype($value/2);

  if($sType == "integer") { 
    return $value;  
  } else {  
    return ($value-1);  
  }
}
?>

2. Send the script to FFMPEG for encoding
Here is where the fun starts, executing it and telling FFMPEG where to place it later. Let's see on how our command will consist and what it will be made of. We'll see what quality settings we will have to set.

ffmpeg -i video.avi -ar 22050 -ab 32 -f flv -s 320x240 video.flv

That's generally how to convert a video.avi to video.flv with the audio sampling at 22050 & audio bit rate at 32, with the size 320×240. While I suggest the values above for audio as they are the most compressed, but we'll use the old audio settings for better quality.

<?php
$srcAB = intval($ffmpegObj->getAudioBitRate()/1000);  
$srcAR = $ffmpegObj->getAudioSampleRate();
?>

Now we have pretty much most of the values ready for our compression, however, we need to call flvtool2 to get our Meta information. Steps 4 and 5 in the diagram work simultaneously with this one.

What we do is make flvtool2 run at the same time as FFMPEG so we'll pipe it into the command which means our general command is

ffmpeg -i video.avi -ar 22050 -ab 32 -f flv -s 320x240 video.flv | flvtool2 -U stdin video.flv

Now, we have a kind of complete command, let's make our final code!

<?php  
// Set our source file  
$srcFile = "/path/to/clock.avi";  
$destFile = "/path/to/clock.flv";  
$ffmpegPath = "/path/to/ffmpeg";  
$flvtool2Path = "/path/to/flvtool2";  

// Save our needed variables  
$ffmpegObj = new ffmpeg_movie($srcFile);  
$srcWidth = makeMultipleTwo($ffmpegObj->getFrameWidth());  
$srcHeight = makeMultipleTwo($ffmpegObj->getFrameHeight());  
$srcFPS = $ffmpegObj->getFrameRate();  
$srcAB = intval($ffmpegObj->getAudioBitRate()/1000);  
$srcAR = $ffmpegObj->getAudioSampleRate();  

// Call our convert using exec()  
exec($ffmpegPath . " -i " . $srcFile . " -ar " . $srcAR . " -ab " . $srcAB . " -f flv -s " . $srcWidth . "x" . $srcHeight . " " . $destFile . " | " . $flvtool2Path . " -U stdin " . $destFile);  

// Make multiples function  
function makeMultipleTwo ($value)  
{  
  $sType = gettype($value/2);

  if($sType == "integer") { 
    return $value;  
  } else {  
    return ($value-1);  
  }
}
?>

How to Up Your DevOps Game with Project Gating: Zuul - A CI/CD Gating Tool

Download White Paper

Share on social media

Virtual machines, Kubernetes & Bare Metal Infrastructure

Choose from Atmosphere Cloud, Hosted, or On-Premise.
Simplify your cloud operations with our intuitive dashboard.
Run it yourself, tap our expert support, or opt for full remote operations.
Leverage Terraform, Ansible or APIs directly powered by OpenStack & Kubernetes