«

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

时间:2024-3-1 12:23     作者:韩俊     分类: PHP


本文实例讲述了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_fullnamen");
}
if (php_sapi_name() != 'cli') {
  echo '
';
}
printf("ffmpeg-php version string: %sn", FFMPEG_PHP_VERSION_STRING);
printf("ffmpeg-php build date string: %sn", FFMPEG_PHP_BUILD_DATE_STRING);
printf("libavcodec build number: %dn", LIBAVCODEC_BUILD_NUMBER);
printf("libavcodec version number: %dn", 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 "--------------------nn";
foreach($movies as $movie) {
  $mov = new ffmpeg_movie($movie);
  printf("file name = %sn", $mov->getFileName());
  printf("duration = %s secondsn", $mov->getDuration());
  printf("frame count = %sn", $mov->getFrameCount());
  printf("frame rate = %0.3f fpsn", $mov->getFrameRate());
  printf("comment = %sn", $mov->getComment());
  printf("title = %sn", $mov->getTitle());
  printf("author = %sn", $mov->getAuthor());
  printf("copyright = %sn", $mov->getCopyright());
  printf("get bit rate = %dn", $mov->getBitRate());
  printf("has audio = %sn", $mov->hasAudio() == 0 ? 'No' : 'Yes');
  if ($mov->hasAudio()) {
    printf("get audio stream id= %sn", $mov->getAudioStreamId());
    printf("get audio codec = %sn", $mov->getAudioCodec());
    printf("get audio bit rate = %dn", $mov->getAudioBitRate());
    printf("get audio sample rate = %d n", $mov->getAudioSampleRate());
    printf("get audio channels = %sn", $mov->getAudioChannels());
  }
  printf("has video = %sn", $mov->hasVideo() == 0 ? 'No' : 'Yes');
  if ($mov->hasVideo()) {
    printf("frame height = %d pixelsn", $mov->getFrameHeight());
    printf("frame width = %d pixelsn", $mov->getFrameWidth());
    printf("get video stream id= %sn", $mov->getVideoStreamId());
    printf("get video codec = %sn", $mov->getVideoCodec());
    printf("get video bit rate = %dn", $mov->getVideoBitRate());
    printf("get pixel format = %sn", $mov->getPixelFormat());
    printf("get pixel aspect ratio = %sn", $mov->getPixelAspectRatio());
    $frame = $mov->getFrame(10);
    printf("get frame = %sn", is_object($frame) ? 'true' : 'false');
    printf(" get frame number = %dn", $mov->getFrameNumber());
    printf(" get frame width = %dn", $frame->getWidth());
    printf(" get frame height = %dn", $frame->getHeight());
  }
  echo "n--------------------nn";
}
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 Definedn";
  }
}
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教程

热门推荐