如何获得视频的最高可能比特率和尺寸?

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

我正在使用一个名为 pascalbaljetmedia/laravel-ffmpeg

的包

并想为视频流创建一个 HLS 播放列表。 但首先我想检查视频比特率或 width/height 看看它是 4k、1080、720 等

那么如何计算视频比特率及其尺寸?..

获取视频信息后我想做的是:


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

有什么帮助吗?

我不使用 Laravel,但看起来 pascalbaljetmedia/laravel-ffmpegphp-ffmpeg/php-ffmpeg 的包装器,因此您应该可以使用 FFProbe 来提取它信息。

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

顺便说一下,您的代码行中有几个拼写错误,应该是 $media->addFilter('scale=' . $resolution['size']['w'] . ':' . $resolution['size']['h']);