phplaravelffmpegvideo-processingffmpeg-php

How to get Highest possible bit rate and dimensions of a video?


I am using a package called pascalbaljetmedia/laravel-ffmpeg

And want to create an HLS playlist for video streaming. but first i want to check the video bit-rate or width/height to see if it's a 4k, 1080, 720 etc.

So how do i calculate the video bitrate and it's dimensions?..

Here is what i want to do after getting the video information:


$video = FFMpeg::fromDisk('videos') 
               ->open('original_video.mp4') 
               ->exportForHLS();

$resolutions = [];

//check if we can make 4k version
if ( ( $bitrate >= 14000 )  ||  ( $width >= 3840 && $height >= 2160 ) ) 
{
     // Then it's a 4k video
    // We make a 4k version of HLS

    $resolutions[] = ['q' => 14000, 'size' => [ 'w' => 3840, 'h' => 2160]];

}


//check if we can make HD version
if(  ( $bitrate >= 5800 )  ||  ( $width >= 1920 && $height >= 1080 )  )

{
     // Then it's a HD video
    // We make a HD version of HLS
    $resolutions[] = ['q' => 5800, 'size' => [ 'w' => 1920, 'h' => 1080]];

}

 
//Lastly we loop through and add formarts
foreach($resolutions as $resolution){
    $video->addFormat($resolution['q'], function($media) {
        $media->addFilter('scale='. $resolution['size']['w].':'. $resolution['size']['h]);
    });
}

$video->save('video_name.m3u8');

Any help?.


Solution

  • I don't use Laravel, but it looks like pascalbaljetmedia/laravel-ffmpeg is a wrapper for php-ffmpeg/php-ffmpeg, so you should be able to use FFProbe to extract this information.

    $ffprobe = FFMpeg\FFProbe::create();
    $video = $ffprobe->streams('original_video.mp4')->videos()->first();
    $width = $video->get('width');
    $height = $video->get('height');
    $bitrate = $video->get('bit_rate');
    

    Incidentally, there are a couple of typos in your line of code which should be $media->addFilter('scale=' . $resolution['size']['w'] . ':' . $resolution['size']['h']);.