PHP intercept video Specify frame for picture _php tips

Source: Internet
Author: User
Tags explode imagejpeg learn php learn php programming php programming

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

The above is the entire content of this article, I hope that you learn PHP programming help.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.