欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2023-12-20 09:39:34
本文实例讲述了php使用ffmpeg获取视频信息并截图的方法。分享给大家供大家参考,具体如下: $movie = new ffmpeg_movie('4.mp4'...

本文实例讲述了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 "
\n";
}
$extension = "ffmpeg";
$extension_soname = $extension . "." . php_shlib_suffix;
$extension_fullname = php_extension_dir . "/" . $extension_soname;
// load extension
if (!extension_loaded($extension)) {
  dl($extension_soname) or die("can't load extension $extension_fullname\n");
}
if (php_sapi_name() != 'cli') {
  echo '
';
}
printf("ffmpeg-php version string: %s\n", ffmpeg_php_version_string);
printf("ffmpeg-php build date string: %s\n", ffmpeg_php_build_date_string);
printf("libavcodec build number: %d\n", libavcodec_build_number);
printf("libavcodec version number: %d\n", 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 "--------------------\n\n";
foreach($movies as $movie) {
  $mov = new ffmpeg_movie($movie);
  printf("file name = %s\n", $mov->getfilename());
  printf("duration = %s seconds\n", $mov->getduration());
  printf("frame count = %s\n", $mov->getframecount());
  printf("frame rate = %0.3f fps\n", $mov->getframerate());
  printf("comment = %s\n", $mov->getcomment());
  printf("title = %s\n", $mov->gettitle());
  printf("author = %s\n", $mov->getauthor());
  printf("copyright = %s\n", $mov->getcopyright());
  printf("get bit rate = %d\n", $mov->getbitrate());
  printf("has audio = %s\n", $mov->hasaudio() == 0 ? 'no' : 'yes');
  if ($mov->hasaudio()) {
    printf("get audio stream id= %s\n", $mov->getaudiostreamid());
    printf("get audio codec = %s\n", $mov->getaudiocodec());
    printf("get audio bit rate = %d\n", $mov->getaudiobitrate());
    printf("get audio sample rate = %d \n", $mov->getaudiosamplerate());
    printf("get audio channels = %s\n", $mov->getaudiochannels());
  }
  printf("has video = %s\n", $mov->hasvideo() == 0 ? 'no' : 'yes');
  if ($mov->hasvideo()) {
    printf("frame height = %d pixels\n", $mov->getframeheight());
    printf("frame width = %d pixels\n", $mov->getframewidth());
    printf("get video stream id= %s\n", $mov->getvideostreamid());
    printf("get video codec = %s\n", $mov->getvideocodec());
    printf("get video bit rate = %d\n", $mov->getvideobitrate());
    printf("get pixel format = %s\n", $mov->getpixelformat());
    printf("get pixel aspect ratio = %s\n", $mov->getpixelaspectratio());
    $frame = $mov->getframe(10);
    printf("get frame = %s\n", is_object($frame) ? 'true' : 'false');
    printf(" get frame number = %d\n", $mov->getframenumber());
    printf(" get frame width = %d\n", $frame->getwidth());
    printf(" get frame height = %d\n", $frame->getheight());
  }
  echo "\n--------------------\n\n";
}
if (php_sapi_name() != 'cli') {
  echo '';
}
/* functions */
function print_class_methods($class) {
  echo "\nmethods available in class '$class':\n";
  $methods = get_class_methods($class);
  if (is_array($methods)) {
    foreach($methods as $method) {
      echo $method . "\n";
    }
  } else {
    echo "no methods defined\n";
  }
}
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使用ffmpeg获取视频信息并截图的实现方法

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

希望本文所述对大家php程序设计有所帮助。

上一篇:

下一篇: