Using PHP and FFmpeg / FFprobe to get the width and height of a video
For this example I am using Moo0 on a Windows machine. Moo0 FFmpeg/FFprobe are tools used to convert video files and get information about videos. The download page is located here: https://www.moo0.com/software/FFmpeg Please take note that after you download it, to where you installed it on your machine. I am assuming you already have PHP installed on your machine and can run PHP scripts.
To get started, after you have installed it, you need to find the location of binaries folder for Moo0. After I installed it on my machine the location was here: C:\Program Files (x86)\Moo0\FFmpeg 1.07\bin\ffprobe.exe.
Now we can create the command to run that will return our width and height of our video file:
$cmd = " -show_entries stream=width,height -of default=nw=1:nk=1 a.mp4";
Next I am using the PHP function popen to run the command as a background process.
$open = popen('start /B "" "C:\Program Files (x86)\Moo0\FFmpeg 1.07\bin\ffprobe.exe"'. $cmd.'', "r");
Now we will loop through the results of the command to get the width and height of the video and close the pointer.
$dim = array();
while($bstream = fgets($open, 2048)) {
$dim[] = trim($bstream);
}
pclose($open);
Now that we have the dimensions in an array, we can print them out to see the results.
print_r($dim);
Example output of the width and height:
Array
(
[0] => 1280
[1] => 720
)
That is it. Please note that I used ffprobe.exe to get the dimensions in this example. Have a nice day. 😊
FFmpeg / FFprobe are licensed under GNU General Public License Version. Please note before use in your projects.
Comments
Post a Comment