getID3获取本地或远程视频时长

发布于:2025-03-29 ⋅ 阅读:(20) ⋅ 点赞:(0)

音频文件也可使用,使用ffmeg安装太复杂了

附ffmpeg方式:centos下安装ffmpeg_yum安装ffmpeg-CSDN博客

使用composer先安装

composer require james-heinrich/getid3

获取本地视频

 //获取本地视频
 $video_path = $_SERVER['DOCUMENT_ROOT'].$params['video'];
 $getid3 = new getID3();
 $info = $getid3->analyze($video_path);
 var_dump($info);

获取远程视频信息,也是生成临时文件,再删除,如果再上传本地的时候直接使用本地获取即可

//远程视屏地址 
$remotefilename = 'https://vdept3.bdstatic.com/mda-qfvaqpnzzj3qf31w/cae_h264/1719738331410781347/mda-qfvaqpnzzj3qf31w.mp4?v_from_s=hkapp-haokan-hbf&auth_key=1743144763-0-0-fcab6e8d4022a8b4d186e26431dcc39b&bcevod_channel=searchbox_feed&pd=1&cr=0&cd=0&pt=3&logid=3163875783&vid=16648779981624158842&klogid=3163875783&abtest=';
 if ($fp_remote = fopen($remotefilename, 'rb')) {
     $localtempfilename = tempnam('', 'getID3');
      if ($fp_local = fopen($localtempfilename, 'wb')) {
              while ($buffer = fread($fp_remote, 8192)) {
                fwrite($fp_local, $buffer);
              }
      fclose($fp_local);
      $getID3 = new getID3();
      $ThisFileInfo = $getID3->analyze($localtempfilename);
      $second = floor($ThisFileInfo['playtime_seconds']) ?? 0; //秒数向下取整
      var_dump($ThisFileInfo);//打印信息
      unlink($localtempfilename);
   }
      fclose($fp_remote);
    }

打印出来的视频信息

array(19) {
  ["GETID3_VERSION"] => string(19) "1.9.23-202310190849"
  ["filesize"] => int(8323282)
  ["filepath"] => string(35) "C:/Users/PC-8625/AppData/Local/Temp"
  ["filename"] => string(11) "getFCB0.tmp"
  ["filenamepath"] => string(47) "C:/Users/PC-8625/AppData/Local/Temp/getFCB0.tmp"
  ["avdataoffset"] => int(87891)
  ["avdataend"] => int(8323282)
  ["fileformat"] => string(3) "mp4"
  ["audio"] => array(10) {
    ["dataformat"] => string(3) "mp4"
    ["bitrate"] => int(65622)
    ["codec"] => string(19) "ISO/IEC 14496-3 AAC"
    ["sample_rate"] => float(44100)
    ["channels"] => int(2)
    ["bits_per_sample"] => int(16)
    ["lossless"] => bool(false)
    ["channelmode"] => string(6) "stereo"
    ["compression_ratio"] => float(0.046500850340136)
    ["streams"] => array(1) {
      [0] => array(9) {
        ["dataformat"] => string(3) "mp4"
        ["bitrate"] => int(65622)
        ["codec"] => string(19) "ISO/IEC 14496-3 AAC"
        ["sample_rate"] => float(44100)
        ["channels"] => int(2)
        ["bits_per_sample"] => int(16)
        ["lossless"] => bool(false)
        ["channelmode"] => string(6) "stereo"
        ["compression_ratio"] => float(0.046500850340136)
      }
    }
  }
  ["video"] => array(8) {
    ["dataformat"] => string(9) "quicktime"
    ["rotate"] => int(0)
    ["resolution_x"] => int(640)
    ["resolution_y"] => int(360)
    ["fourcc"] => string(4) "avc1"
    ["fourcc_lookup"] => string(16) "H.264/MPEG-4 AVC"
    ["frame_rate"] => float(25)
    ["bitrate"] => float(291412.00549507)
  }
  ["tags"] => array(1) {
    ["quicktime"] => array(1) {
      ["encoding_tool"] => array(1) {
        [0] => string(44) "Multimedia Cloud Transcode (cloud.baidu.com)"
      }
    }
  }
  ["comments"] => array(1) {
    ["language"] => array(1) {
      [0] => string(12) "Undetermined"
    }
  }
  ["encoding"] => string(5) "UTF-8"
  ["mime_type"] => string(9) "video/mp4"
  ["quicktime"] => array(13) {
    ["hinting"] => bool(false)
    ["controller"] => string(8) "standard"
    ["ftyp"] => array(7) {
      ["hierarchy"] => string(4) "ftyp"
      ["name"] => string(4) "ftyp"
      ["size"] => int(32)
      ["offset"] => int(0)
      ["signature"] => string(4) "isom"
      ["unknown_1"] => int(512)
      ["fourcc"] => string(4) "isom"
    }
    ["timestamps_unix"] => array(2) {
      ["create"] => array(3) {
        ["moov mvhd"] => int(-2082844800)
        ["moov trak tkhd"] => int(-2082844800)
        ["moov trak mdia mdhd"] => int(-2082844800)
      }
      ["modify"] => array(3) {
        ["moov mvhd"] => int(-2082844800)
        ["moov trak tkhd"] => int(-2082844800)
        ["moov trak mdia mdhd"] => int(-2082844800)
      }
    }
    ["time_scale"] => int(44100)
    ["display_scale"] => float(1)
    ["video"] => array(5) {
      ["rotate"] => int(0)
      ["resolution_x"] => int(640)
      ["resolution_y"] => int(360)
      ["frame_rate"] => float(25)
      ["frame_count"] => int(4613)
    }
    ["stts_framecount"] => array(2) {
      [0] => int(4613)
      [1] => int(7947)
    }
    ["audio"] => array(5) {
      ["bitrate"] => int(65622)
      ["codec"] => string(3) "mp4"
      ["sample_rate"] => float(44100)
      ["channels"] => int(2)
      ["bit_depth"] => int(16)
    }
    ["comments"] => array(1) {
      ["encoding_tool"] => array(1) {
        [0] => string(44) "Multimedia Cloud Transcode (cloud.baidu.com)"
      }
    }
    ["free"] => array(4) {
      ["hierarchy"] => string(4) "free"
      ["name"] => string(4) "free"
      ["size"] => int(8)
      ["offset"] => int(87875)
    }
    ["mdat"] => array(4) {
      ["hierarchy"] => string(4) "mdat"
      ["name"] => string(4) "mdat"
      ["size"] => int(8235399)
      ["offset"] => int(87883)
    }
    ["encoding"] => string(5) "UTF-8"
  }
  ["playtime_seconds"] => float(184.529)
  ["bitrate"] => float(357034.00549507)
  ["tags_html"] => array(1) {
    ["quicktime"] => array(1) {
      ["encoding_tool"] => array(1) {
        [0] => string(44) "Multimedia Cloud Transcode (cloud.baidu.com)"
      }
    }
  }
  ["playtime_string"] => string(4) "3:05"
}