Intercepting video specifies frames as pictures, and PHP ffmpeg extensions are perfectly implemented:
$movie = new Ffmpeg_movie ($video _filepath);
$ff _frame = $movie->getframe (1);
$GD _image = $ff _frame->togdimage ();
$img = "./test.jpg";
Imagejpeg ($gd _image, $img);
Imagedestroy ($GD _image);
But here's the problem, the video that the smartphone takes, because the camera is in a different direction, the video will be rotated, and with meta information rotate, when you're intercepting the frame picture relative to the video, if there is a video of rotate information, the frame is rotated, so you need to rotate the captured picture accordingly.
Then the PHP ffmpeg extension does not know rotation information (PHP ffmpeg extended document), but can be obtained through the FFmpeg command line:
/usr/local/ffmpeg/bin/ffprobe Test.mp4-show_streams | grep rotate
Simple encapsulation with PHP is as follows:
function get_video_orientation ($video _path) {
$cmd = "/usr/local/ffmpeg/bin/ffprobe". $video _path. "-show_streams 2>/dev/null";
$result = Shell_exec ($cmd);
$orientation = 0;
if (Strpos ($result, ' tag:rotate ')!== FALSE) {
$result = explode ("\ n", $result);
foreach ($result as $line) {
if (Strpos ($line, ' tag:rotate ')!== FALSE) {
$stream _info = explode ("=", $line);
$orientation = $stream _info[1];
}
}
}
return $orientation;
}
Use the Imagerotate () function to rotate the screenshot:
$movie = new Ffmpeg_movie ($video _filepath);
$frame = $movie->getframe (1);
$GD = $frame->togdimage ();
if ($orientation = $this->get_video_orientation ($video _filepath)) {
$GD = Imagerotate ($GD, 360-$orientation, 0);
}
$img = "./test.jpg";
Imagejpeg ($GD, $img);
Imagedestroy ($GD _image);
Finally, there is a problem, not all players and browsers can recognize the video orientation and automatic rotate, if you want to rotate, you can use the FFmpeg command to resolve:
/usr/local/ffmpeg/bin/ffmpeg-i input.mp4-vf ' transpose=3 '-metadata:s:v:0 rotate=0-codec:v libx264-strict-2-y output . mp4
The following documents are not familiar to ffmpeg commands and parameters: