[英]Extract frame from video - PHP-FFMpeg
我在一个laravel项目中使用php ffmpeg,做多个事情探测,提取帧和编码。 从上传的视频文件创建框架时出现问题。 这是框架的创建方式:
$video = $ffmpeg->open($destinationPath.'/'.$filename);
$video
->frame(FFMpeg\Coordinate\TimeCode::fromSeconds(10))
->save(public_path().$frame_path);
这有时会起作用并创建框架,但其他时间则不然。 我注意到当我尝试打开.mov文件时会出现此错误。
您的ffmpeg版本可能不支持源视频文件中使用的编解码器,因此无法解压缩视频并提取图像。
您可以尝试从命令行处理文件,看看是否可以通过这种方式提取图像,ffmpeg可能会为您提供有关该问题的更多信息。
用于从视频文件中提取png帧的示例命令行
ffmpeg -y -ss 30 -i [source_file] -vframes 1 [target_file]
如果输出名称是变量,请添加-f image2
作为输出选项。
默认情况下,PHP-FFMpeg库会在输入文件之前附加-ss参数,该文件要求时间戳准确以获取帧。 在mkv文件的情况下我遇到了这个问题。 无法准确搜索mkv和mov等文件。
https://github.com/PHP-FFMpeg/PHP-FFMpeg/blob/master/src/FFMpeg/Media/Frame.php#L79
您需要将true
作为第二个参数传递给save函数,以便给出最接近给定点的Frame。 它改变了ffmpeg命令中-ss参数的位置。
-ss position(输入/输出)
当用作输入选项(在-i之前)时,在此输入文件中寻找位置。 请注意,在大多数格式中,无法精确搜索,因此ffmpeg将在位置之前寻找最接近的搜索点。 当启用转码和-accurate_seek(默认值)时,搜索点和位置之间的这个额外段将被解码并丢弃。 在进行流复制时或使用-noaccurate_seek时,它将被保留。
当用作输出选项(在输出文件名之前)时,解码但丢弃输入,直到时间戳到达位置。
position必须是持续时间规范,请参阅ffmpeg-utils(1)手册中的(ffmpeg-utils)持续时间部分。
这是我用过PHP的代码:
https://totaldev.com/extract-image-frame-video-php-ffmpeg/
<?php
// Full path to ffmpeg (make sure this binary has execute permission for PHP)
$ffmpeg = "/full/path/to/ffmpeg";
// Full path to the video file
$videoFile = "/full/path/to/video.mp4";
// Full path to output image file (make sure the containing folder has write permissions!)
$imgOut = "/full/path/to/frame.jpg";
// Number of seconds into the video to extract the frame
$second = 0;
// Setup the command to get the frame image
$cmd = $ffmpeg." -i \"".$videoFile."\" -an -ss ".$second.".001 -y -f mjpeg \"".$imgOut."\" 2>&1";
// Get any feedback from the command
$feedback = `$cmd`;
// Use $imgOut (the extracted frame) however you need to
// ...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.