浏览文章
文章信息
PHP截取视频帧做封面
13239
1、安装ffmpeg拓展
https://bbs.aiweline.com/thread-120745.htm
2、示例代码
$fileUrl = $this->getRequest()->getParam('video'); $frame_time = $this->getRequest()->getParam('time')??0; $width = $this->getRequest()->getParam('w')??640; $height = $this->getRequest()->getParam('h')??360; $hdrs = @get_headers($fileUrl); $isUrl = is_array($hdrs) ? preg_match('/^HTTP\\/\\d+\\.\\d+\\s+2\\d\\d\\s+.*$/', $hdrs[0]) : false; if (!$isUrl) { exit(http_response_code(404)); } $urlArr = parse_url($fileUrl); $host_url = $urlArr['scheme'] . '://' . $urlArr['host']; if (!empty($fileUrl)) { $filePath = BP . '/pub' . str_replace('/pub', '', $urlArr['path']); if (is_file($filePath)) { try { $pathParts = pathinfo($filePath); $filename = $pathParts['dirname'] . "/" . $pathParts['filename'] . "_"; $destFilePath = $filename . $frame_time ."_{$width}_{$height}.jpg"; $command = "/usr/bin/ffmpeg -i {$filePath} -y -f image2 -ss {$frame_time} -vframes 1 -s {$width}x{$height} {$destFilePath}"; exec($command); exit(str_replace(BP, $host_url, $destFilePath)); } catch (Exception $e) { exit(http_response_code(404)); } } } exit(http_response_code(404));