Welcome 微信登录

首页 / 网页编程 / PHP / php使用ffmpeg获取视频信息并截图的实现方法

本文实例讲述了php使用ffmpeg获取视频信息并截图的方法。分享给大家供大家参考,具体如下:
$movie = new ffmpeg_movie("4.mp4");$width=$movie->getFrameWidth();$height=$movie->getFrameHeight();$count= $movie->getFrameCount();print $count . "";$n = round ( $count/16 );print $n . "";for ( $i = 1; $i <= 1; $i ++ ) {$img = "screencap" . $i . ".png";$x = $n * $i;$f = $movie->getFrame($x);$gd_image = $f->toGDImage();imagepng($gd_image, $img);imagedestroy($gd_image);echo "
";}$extension = "ffmpeg";$extension_soname = $extension . "." . PHP_SHLIB_SUFFIX;$extension_fullname = PHP_EXTENSION_DIR . "/" . $extension_soname;// load extensionif (!extension_loaded($extension)) {dl($extension_soname) or die("Can"t load extension $extension_fullname
");}if (php_sapi_name() != "cli") {echo "";}printf("ffmpeg-php version string: %s
", FFMPEG_PHP_VERSION_STRING);printf("ffmpeg-php build date string: %s
", FFMPEG_PHP_BUILD_DATE_STRING);printf("libavcodec build number: %d
", LIBAVCODEC_BUILD_NUMBER);printf("libavcodec version number: %d
", LIBAVCODEC_VERSION_NUMBER);print_class_methods("ffmpeg_movie");print_class_methods("ffmpeg_frame");// get an array for movies from the test media directory$movies = getDirFiles(dirname(__FILE__) . "/tests/test_media");echo "--------------------

";foreach($movies as $movie) {$mov = new ffmpeg_movie($movie);printf("file name = %s
", $mov->getFileName());printf("duration = %s seconds
", $mov->getDuration());printf("frame count = %s
", $mov->getFrameCount());printf("frame rate = %0.3f fps
", $mov->getFrameRate());printf("comment = %s
", $mov->getComment());printf("title = %s
", $mov->getTitle());printf("author = %s
", $mov->getAuthor());printf("copyright = %s
", $mov->getCopyright());printf("get bit rate = %d
", $mov->getBitRate());printf("has audio = %s
", $mov->hasAudio() == 0 ? "No" : "Yes");if ($mov->hasAudio()) {printf("get audio stream id= %s
", $mov->getAudioStreamId());printf("get audio codec = %s
", $mov->getAudioCodec());printf("get audio bit rate = %d
", $mov->getAudioBitRate());printf("get audio sample rate = %d 
", $mov->getAudioSampleRate());printf("get audio channels = %s
", $mov->getAudioChannels());}printf("has video = %s
", $mov->hasVideo() == 0 ? "No" : "Yes");if ($mov->hasVideo()) {printf("frame height = %d pixels
", $mov->getFrameHeight());printf("frame width = %d pixels
", $mov->getFrameWidth());printf("get video stream id= %s
", $mov->getVideoStreamId());printf("get video codec = %s
", $mov->getVideoCodec());printf("get video bit rate = %d
", $mov->getVideoBitRate());printf("get pixel format = %s
", $mov->getPixelFormat());printf("get pixel aspect ratio = %s
", $mov->getPixelAspectRatio());$frame = $mov->getFrame(10);printf("get frame = %s
", is_object($frame) ? "true" : "false");printf(" get frame number = %d
", $mov->getFrameNumber());printf(" get frame width = %d
", $frame->getWidth());printf(" get frame height = %d
", $frame->getHeight());}echo "
--------------------

";}if (php_sapi_name() != "cli") {echo "";}/* FUNCTIONS */function print_class_methods($class) {echo "
Methods available in class "$class":
";$methods = get_class_methods($class);if (is_array($methods)) {foreach($methods as $method) {echo $method . "
";}} else {echo "No Methods Defined
";}}function getDirFiles($dirPath){if ($handle = opendir($dirPath)){while (false !== ($file = readdir($handle))) {$fullpath = $dirPath . "/" . $file;if (!is_dir($fullpath) && $file != "CVS" && $file != "." && $file != "..")$filesArr[] = trim($fullpath);}closedir($handle);}return $filesArr;}?>
运行效果如下图所示:

更多关于PHP相关内容感兴趣的读者可查看本站专题:《PHP图形与图片操作技巧汇总》、《php操作office文档技巧总结(包括word,excel,access,ppt)》、《php日期与时间用法总结》、《php面向对象程序设计入门教程》、《php字符串(string)用法总结》、《php+mysql数据库操作入门教程》及《php常见数据库操作技巧汇总》
希望本文所述对大家PHP程序设计有所帮助。