intercepts the specified frame of the video into an image, the php ffmpeg extension has been 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);
However, here comes the problem. Due to the different shooting directions, the video taken by the smartphone will be rotated and carry the meta information rotate. When you intercept the frame picture relative to the video, if there is a video with rotate information, the frame will also be Rotated, so you need to rotate the captured image accordingly.
Then the php ffmpeg extension cannot obtain the rotation information (php ffmpeg extension documentation), but it can be obtained through the ffmpeg command line:
/usr/local/ffmpeg/bin/ffprobe test.mp4 -show_streams | grep rotate
Use php to simply encapsulate it 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 another trouble. Not all players and browsers can recognize the orientation of the video and automatically rotate it. If you want to rotate the video, you can solve it through the ffmpeg command:
/usr/local/ffmpeg/bin/ffmpeg -i input.mp4 -vf 'transpose=3' -metadata:s:v:0 rotate=0
The above is the entire content of this article. I hope it will be helpful to everyone in learning PHP programming.