PHP FFmpeg: Get First Video Frame
To get the first frame of a video in PHP, you can achieve it by using the FFmpeg library. Here is a simple PHP script example to retrieve the first frame of a video using FFmpeg:
<?php
$videoPath = 'path/to/your/video.mp4'; // 视频文件路径
// 使用 FFmpeg 获取视频第一帧的 base64 编码
$ffmpegPath = 'path/to/ffmpeg'; // FFmpeg 库路径
$cmd = "$ffmpegPath -i $videoPath -ss 00:00:01 -vframes 1 -f image2pipe -"; // 获取第一帧的命令
$imageData = shell_exec($cmd); // 执行命令,获取第一帧图像数据
// 将 base64 编码的图像数据显示出来
$imageData = base64_encode($imageData);
echo '<img src="data:image/jpeg;base64,' . $imageData . '">';
?>
In the example above, first, the paths of the video file and the FFmpeg library are specified. Then, the FFmpeg command-line tool is used to extract the first frame image data of the video and output it in base64-encoded format on the page.
Please note that using FFmpeg requires installing the FFmpeg library and having permission to execute shell commands on the server. Additionally, for safety precautions, it is recommended to check and verify user-uploaded video files to prevent malicious code injection.