首页 > 开发 > PHP > 正文

php使用ffmpeg获取视频信息并截图的实现方法

2024-05-04 22:01:32
字体:大 中 小
来源:转载
供稿:网友

本文实例讲述了php使用ffmpeg获取视频信息并截图的方法。分享给大家供大家参考,具体如下:

  1. $movie = new ffmpeg_movie('4.mp4'); 
  2.  
  3. $width=$movie->getFrameWidth(); 
  4.  
  5. $height=$movie->getFrameHeight(); 
  6.  
  7. $count= $movie->getFrameCount(); 
  8.  
  9. print $count . ''; 
  10.  
  11. $n = round ( $count/16 ); 
  12.  
  13. print $n . ''; 
  14.  
  15. for ( $i = 1; $i <= 1; $i ++ ) { 
  16.  
  17.   $img = 'screencap' . $i . '.png'; 
  18.  
  19.   $x = $n * $i; 
  20.  
  21.   $f = $movie->getFrame($x); 
  22.  
  23.   $gd_image = $f->toGDImage(); 
  24.  
  25.   imagepng($gd_image, $img); 
  26.  
  27.   imagedestroy($gd_image); 
  28.  
  29.   echo " 
  30.  
  31. /n"; 
  32.  
  33. } 
  34.  
  35. $extension = "ffmpeg"; 
  36.  
  37. $extension_soname = $extension . "." . PHP_SHLIB_SUFFIX; 
  38.  
  39. $extension_fullname = PHP_EXTENSION_DIR . "/" . $extension_soname; 
  40.  
  41. // load extension 
  42.  
  43. if (!extension_loaded($extension)) { 
  44.  
  45.   dl($extension_soname) or die("Can't load extension $extension_fullname/n"); 
  46.  
  47. } 
  48.  
  49. if (php_sapi_name() != 'cli') { 
  50.  
  51.   echo ' 
  52.  
  53. '; 
  54.  
  55. } 
  56.  
  57. printf("ffmpeg-php version string: %s/n", FFMPEG_PHP_VERSION_STRING); 
  58.  
  59. printf("ffmpeg-php build date string: %s/n", FFMPEG_PHP_BUILD_DATE_STRING); 
  60.  
  61. printf("libavcodec build number: %d/n", LIBAVCODEC_BUILD_NUMBER); 
  62.  
  63. printf("libavcodec version number: %d/n", LIBAVCODEC_VERSION_NUMBER); 
  64.  
  65. print_class_methods("ffmpeg_movie"); 
  66.  
  67. print_class_methods("ffmpeg_frame"); 
  68.  
  69. // get an array for movies from the test media directory 
  70.  
  71. $movies = getDirFiles(dirname(__FILE__) . '/tests/test_media'); 
  72.  
  73. echo "--------------------/n/n"; 
  74.  
  75. foreach($movies as $movie) { 
  76.  
  77.   $mov = new ffmpeg_movie($movie); 
  78.  
  79.   printf("file name = %s/n", $mov->getFileName()); 
  80.  
  81.   printf("duration = %s seconds/n", $mov->getDuration()); 
  82.  
  83.   printf("frame count = %s/n", $mov->getFrameCount()); 
  84.  
  85.   printf("frame rate = %0.3f fps/n", $mov->getFrameRate()); 
  86.  
  87.   printf("comment = %s/n", $mov->getComment()); 
  88.  
  89.   printf("title = %s/n", $mov->getTitle()); 
  90.  
  91.   printf("author = %s/n", $mov->getAuthor()); 
  92.  
  93.   printf("copyright = %s/n", $mov->getCopyright()); 
  94.  
  95.   printf("get bit rate = %d/n", $mov->getBitRate()); 
  96.  
  97.   printf("has audio = %s/n", $mov->hasAudio() == 0 ? 'No' : 'Yes'); 
  98.  
  99.   if ($mov->hasAudio()) { 
  100.  
  101.     printf("get audio stream id= %s/n", $mov->getAudioStreamId()); 
  102.  
  103.     printf("get audio codec = %s/n", $mov->getAudioCodec()); 
  104.  
  105.     printf("get audio bit rate = %d/n", $mov->getAudioBitRate()); 
  106.  
  107.     printf("get audio sample rate = %d /n", $mov->getAudioSampleRate()); 
  108.  
  109.     printf("get audio channels = %s/n", $mov->getAudioChannels()); 
  110.  
  111.   } 
  112.  
  113.   printf("has video = %s/n", $mov->hasVideo() == 0 ? 'No' : 'Yes'); 
  114.  
  115.   if ($mov->hasVideo()) { 
  116.  
  117.     printf("frame height = %d pixels/n", $mov->getFrameHeight()); 
  118.  
  119.     printf("frame width = %d pixels/n", $mov->getFrameWidth()); 
  120.  
  121.     printf("get video stream id= %s/n", $mov->getVideoStreamId()); 
  122.  
  123.     printf("get video codec = %s/n", $mov->getVideoCodec()); 
  124.  
  125.     printf("get video bit rate = %d/n", $mov->getVideoBitRate()); 
  126.  
  127.     printf("get pixel format = %s/n", $mov->getPixelFormat()); 
  128.  
  129.     printf("get pixel aspect ratio = %s/n", $mov->getPixelAspectRatio()); 
  130.  
  131.     $frame = $mov->getFrame(10); 
  132.  
  133.     printf("get frame = %s/n", is_object($frame) ? 'true' : 'false'); 
  134.  
  135.     printf(" get frame number = %d/n", $mov->getFrameNumber()); 
  136.  
  137.     printf(" get frame width = %d/n", $frame->getWidth()); 
  138.  
  139.     printf(" get frame height = %d/n", $frame->getHeight()); 
  140.  
  141.   } 
  142.  
  143.   echo "/n--------------------/n/n"; 
  144.  
  145. } 
  146.  
  147. if (php_sapi_name() != 'cli') { 
  148.  
  149.   echo ''; 
  150.  
  151. } 
  152.  
  153. /* FUNCTIONS */ 
  154.  
  155. function print_class_methods($class) { 
  156.  
  157.   echo "/nMethods available in class '$class':/n"; 
  158.  
  159.   $methods = get_class_methods($class); 
  160.  
  161.   if (is_array($methods)) { 
  162.  
  163.     foreach($methods as $method) { 
  164.  
  165.       echo $method . "/n"; 
  166.  
  167.     } 
  168.  
  169.   } else { 
  170.  
  171.     echo "No Methods Defined/n"; 
  172.  
  173.   } 
  174.  
  175. } 
  176.  
  177. function getDirFiles($dirPath) 
  178.  
  179. { 
  180.  
  181.   if ($handle = opendir($dirPath)) 
  182.  
  183.   { 
  184.  
  185.     while (false !== ($file = readdir($handle))) { 
  186.  
  187.       $fullpath = $dirPath . '/' . $file; 
  188.  
  189.       if (!is_dir($fullpath) && $file != "CVS" && $file != "." && $file != "..") 
  190.  
  191.         $filesArr[] = trim($fullpath); 
  192. //Vevb.com 
  193.     } 
  194.  
  195.     closedir($handle); 
  196.  
  197.   } 
  198.  
  199.   return $filesArr; 
  200.  
  201. } 
  202.  
  203. ?> 

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表