Home > Backend Development > PHP Tutorial > PHP intercepts specified frames of video into images, _PHP tutorial

PHP intercepts specified frames of video into images, _PHP tutorial

WBOY
Release: 2016-07-12 08:52:05
Original
967 people have browsed it

php intercepts the specified frame of the video into a picture,

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);
Copy after login

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;
}
Copy after login

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);
Copy after login

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.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1127878.htmlTechArticlephp intercepts the specified frame of the video as a picture, intercepts the specified frame of the video as an image, the php ffmpeg extension has been perfectly implemented: $movie = new ffmpeg_movie($video_filePath);$ff_frame = $movie-getFrame(1...
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template