PHP method to intercept specified frames of video into images

墨辰丷
Release: 2023-03-29 13:26:01
Original
2274 people have browsed it

This article mainly introduces the relevant information of php to intercept the specified frame of the video into a picture. Friends who need it can refer to

Intercepting the specified frame of the video into a picture. 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, the problem comes. Due to different shooting directions, the video will be rotated and carry meta information rotate. When you intercept the frame picture relative to the video, if there is a video with rotate information , the frame is also rotated, so you need to rotate the captured image accordingly.

Then the php ffmpeg extension cannot obtain the rotation information (php ffmpeg extension document), but it can be obtained through the ffmpeg command line:

/usr/local/ffmpeg/bin/ffprobe test .mp4 -show_streams | grep rotateUse 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 one trouble, not all Both players and browsers can recognize the orientation of the video and automatically rotate it. If you want to rotate the video, you can use the ffmpeg command:

/usr/local/ffmpeg/bin /ffmpeg -i input.mp4 -vf 'transpose=3' -metadata:s:v:0 rotate=0

Summary: The above is the entire content of this article, I hope it can It will be helpful to everyone’s study.

Related recommendations:

phpSimple example sharing of adding data to xml files

Based on PHP Website directory scanning index tool example

PHP block query implementation method analysis

The above is the detailed content of PHP method to intercept specified frames of video into images. For more information, please follow other related articles on the PHP Chinese website!

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