What is the better way to create thumbnails from video in PHP?

本秂侑毒 提交于 2020-01-01 19:19:26

问题


I am creating a video broadcasting site, in which i need to know how to create thumbnails from the video. Please Help. Any Suggestions or References will be highly appreciated


回答1:


Here's a snippet to grab a frame from the middle of a video. It is old code I have lying around, so I'm sure it could be simplified. Be sure to adjust the ffmpeg path in line 1 to match the location of your install.

$output = shell_exec("/usr/local/bin/ffmpeg -i {$path}");
preg_match('/Duration: ([0-9]{2}):([0-9]{2}):([^ ,])+/', $output, $matches);
$time = str_replace("Duration: ", "", $matches[0]);
$time_breakdown = explode(":", $time);
$total_seconds = round(($time_breakdown[0]*60*60) + ($time_breakdown[1]*60) + $time_breakdown[2]);
shell_exec("/usr/local/bin/ffmpeg -y  -i {$input_filepath} -f mjpeg -vframes 1 -ss " . ($total_seconds / 2) . " -s {$w}x{$h} {$output_filepath}";



回答2:


You can use ffmpeg.

Once it's installed on your server, you can use it in PHP by writing a command line, and calling exec on it.

e.g.:

exec('ffmpeg -i mymovie.mov -vcodec mjpeg -vframes 1 -an -f rawvideo -s 64x64 foo.jpg');



回答3:


I know this question is old. But I came across it when I was looking for a similar solution. Here are my findings. I hope it helps someone else.

  • Html5 + JS. Here are my demo/slides on the topic.

  • HTML to Canvas library didn't go this route but maybe it will work.




回答4:


I've used ffmpeg, starting a task with exec(), and append an ampersand to the command so that teh php script can continue. If I want ffmpeg to finish first, I don't have an ampersand.

From the FAQ: "How do I encode movie to single pictures?" http://ffmpeg.org/faq.html#SEC15

ffmpeg -i movie.mpg movie%d.jpg

This generates many images.

From the docs: "Video and Audio file format conversion" http://ffmpeg.org/ffmpeg-doc.html#SEC5

ffmpeg -i foo.avi -r 1 -s WxH -f image2 -vframes 1 foo.jpeg

Replace W & H with width/height (see http://ffmpeg.org/ffmpeg-doc.html#SEC9 ), replace vframes with number of frames wanted, add %d to filename for sequence number if you're doing multiple images. To grab at a certain point in the video, use -ss (see http://ffmpeg.org/ffmpeg-doc.html#SEC8 )



来源:https://stackoverflow.com/questions/1469821/what-is-the-better-way-to-create-thumbnails-from-video-in-php

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!