如果还未安装ffmpeg
, 首先需要安装ffmpeg
,不同平台有不同的安装方式,可以参考 https://upwqy.com/details/422.html
1、 使用命令行
$file = '';// 视频地址
ob_start();
$ffmpegCmd = 'ffmpeg -i "%s" 2>&1';
passthru(sprintf($ffmpegCmd, $file));
$videoInfo = ob_get_contents();
ob_end_clean();
// 使用输出缓冲,获取ffmpeg所有输出内容
$ret = [];
// Duration: 00:33:42.64, start: 0.000000, bitrate: 152 kb/s
if (preg_match('/Duration: (.*?), start: (.*?), bitrate: (\\d*) kb\\/s/', $videoInfo, $matches)) {
$ret['duration'] = explode('.', $matches[1])[0]; // 视频长度
$duration = explode(':', $matches[1]);
$ret['seconds'] = $duration[0] * 3600 + $duration[1] * 60 + $duration[2]; // 转为秒数
$ret['start'] = $matches[2]; // 开始时间
}
if (isset($ret['seconds'], $ret['start'])) {
$ret['play_time'] = $ret['seconds'] + $ret['start']; // 实际播放时间
}
$header_array = get_headers($file, true);
$ret['size'] = $header_array['Content-Length'];
通过执行以上代码将获取视频的时长、秒、大小。
视频地址可以是本地视频或者远程视频地址
array:5 [
"duration" => "00:19:31"
"seconds" => 1171.68
"start" => "0.000000"
"play_time" => 1171.68
"size" => "193227373"
]
2、使用 php-ffmpeg/php-ffmpeg
首先引入php-ffmpeg
依赖
composer require php-ffmpeg/php-ffmpeg
$url = 'input.mp4';// 替换为真实视频链接
// $ffmpeg = \FFMpeg\FFMpeg::create([
// 'ffmpeg.binaries' => '/usr/bin/ffmpeg',
// 'ffprobe.binaries' => '/usr/bin/ffprobe',
// 'timeout' => 3600, // The timeout for the underlying process
// 'ffmpeg.threads' => 12, // The number of threads that FFMpeg should use
// ]);
$ffmpeg = \FFMpeg\FFMpeg::create();
$video = $ffmpeg->open($url);
$data= [
'width' => $video->getStreams()->videos()->first()->get('width'),
'height' => $video->getStreams()->videos()->first()->get('height'),
'duration' => $video->getStreams()->videos()->first()->get('duration'),
];
dump($data);
通过以上代码执行后输出视频的宽,高,时长
array:4 [
"width" => 1920
"height" => 1080
"duration" => "1171.680000"
]
注意:如果ffmpeg
没有配置系统环境变量,需要在create
时指定ffmpeg
的安装路径
另外通过打印 dump($video->getStreams()->videos()->first());
可以查看视频的其他属性,然后通过 $video->getStreams()->videos()->first()->get('属性名')
的方式获取指定属性值
FFMpeg\FFProbe\DataMapping\Stream {#729
-properties: array:36 [
"index" => 0
"codec_name" => "h264"
"codec_long_name" => "H.264 / AVC / MPEG-4 AVC / MPEG-4 part 10"
"profile" => "Main"
"codec_type" => "video"
"codec_time_base" => "1/50"
"codec_tag_string" => "avc1"
"codec_tag" => "0x31637661"
"width" => 1920
"height" => 1080
"coded_width" => 1920
"coded_height" => 1088
"closed_captions" => 0
"has_b_frames" => 1
"pix_fmt" => "yuv420p"
"level" => 41
"color_range" => "tv"
"color_space" => "bt709"
"color_transfer" => "bt709"
"color_primaries" => "bt709"
"chroma_location" => "left"
"refs" => 1
"is_avc" => "true"
"nal_length_size" => "4"
"r_frame_rate" => "25/1"
"avg_frame_rate" => "25/1"
"time_base" => "1/25000"
"start_pts" => 0
"start_time" => "0.000000"
"duration_ts" => 29292000
"duration" => "1171.680000"
"bit_rate" => "997979"
"bits_per_raw_sample" => "8"
"nb_frames" => "29292"
"disposition" => array:12 [
"default" => 1
"dub" => 0
"original" => 0
"comment" => 0
"lyrics" => 0
"karaoke" => 0
"forced" => 0
"hearing_impaired" => 0
"visual_impaired" => 0
"clean_effects" => 0
"attached_pic" => 0
"timed_thumbnails" => 0
]
"tags" => array:4 [
"creation_time" => "2022-08-01T08:42:55.000000Z"
"language" => "eng"
"handler_name" => "\x1FMainconcept Video Media Handler"
"encoder" => "AVC Coding"
]
]
}
发布时间 : 2023-02-28,阅读量:1205