PHP實現通過推流地址截取視頻圖片。
解決思路:通過推流地址獲取ts文件,將ts文件保存爲mp4,再通過ffmpeg獲取視頻圖片。
<?php
// 推流地址
$url = 'http://hls.xxxx.com/openlive/fcdeb23ec83745e9885750ea0378eff2.m3u8';
// 獲取ts文件地址
$indexPage = file_get_contents($url);
preg_match('/.*\.ts/', $indexPage, $matches);
if(empty($matches)) {
die('m3u8 文件格式錯誤');
}
// 獲取ts文件內容
$content = file_get_contents($matches[0]);
// 將ts文件保存爲mp4文件
$fp = @fopen('tmp/1.mp4', 'a');
fwrite($fp, $content);
fclose($fp);
// 通過ffmpeg獲取視頻圖片
$movie = new ffmpeg_movie('tmp/1.mp4');
$ff_frame = $movie->getFrame(1);
$gd_image = $ff_frame->toGDImage();
$img="tmp/test.jpg";
imagejpeg($gd_image, $img);
imagedestroy($gd_image);