PHP implements a method of capturing video to designate a frame as a picture

Source: Internet
Author: User
This article is mainly for everyone in detail about the PHP interception video designated frame as a picture of the relevant information, the need for friends can refer to the next

Capture Video The specified frame is 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);

However, the problem is that the video taken by the smartphone will be rotated because of the direction of the shooting, and with meta information rotate, when you capture a 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 image accordingly.

The PHP ffmpeg extension does not know rotation information (php ffmpeg extension documentation), but it can be obtained from the FFmpeg command line:

/usr/local/ffmpeg/bin/ffprobe test.mp4-show_streams | grep rotate in a simple package of PHP 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:

$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's a problem, not all players and browsers can recognize the orientation and automatically rotate, if you want to rotate the video, can be resolved by 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 to be able to help you learn.

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.