利用php获得flv视频长度的实例代码

废话不多说了,直接给大家贴代码了,具体代码如下所示:

  1. function BigEndian2Int($byte_word, $signed = false) {
  2. $int_value = 0;
  3. $byte_wordlen = strlen($byte_word);
  4. for ($i = 0; $i < $byte_wordlen; $i++) {
  5. $int_value += ord($byte_word{$i}) * pow(256, ($byte_wordlen - 1 - $i));
  6. }
  7. if ($signed) {
  8. $sign_mask_bit = 0x80 << (8 * ($byte_wordlen - 1));
  9. if ($int_value & $sign_mask_bit) {
  10. $int_value = 0 - ($int_value & ($sign_mask_bit - 1));
  11. }
  12. }
  13. return $int_value;
  14. }
  15. //获得视频的数字时间
  16. function getFlvDuration($name){
  17. if(!file_exists($name)){
  18. return;
  19. }
  20. $flv_data_length=filesize($name);
  21. $fp = @fopen($name, 'r');
  22. $flv_header = fread($fp, 5);
  23. fseek($fp, 5, SEEK_SET);
  24. $frame_size_data_length = $this->BigEndian2Int(fread($fp, 4));
  25. $flv_header_frame_length = 9;
  26. if ($frame_size_data_length > $flv_header_frame_length) {
  27. fseek($fp, $frame_size_data_length - $flv_header_frame_length, SEEK_CUR);
  28. }
  29. $duration = 0;
  30. while ((ftell($fp) + 1) < $flv_data_length) {
  31. $this_tag_header = fread($fp, 16);
  32. $data_length = $this->BigEndian2Int(substr($this_tag_header, 5, 3));
  33. $timestamp = $this->BigEndian2Int(substr($this_tag_header, 8, 3));
  34. $next_offset = ftell($fp) - 1 + $data_length;
  35. if ($timestamp > $duration) {
  36. $duration = $timestamp;
  37. }
  38. fseek($fp, $next_offset, SEEK_SET);
  39. }
  40. fclose($fp);
  41. return $duration;
  42. }
  43. //转化为0:03:56的时间格式
  44. function getFlvTime($time){
  45. $num = $time; //phpfensi.com
  46. $sec = intval($num/1000);
  47. $h = intval($sec/3600);
  48. $m = intval(($sec%3600)/60);
  49. $s = intval(($sec%60));
  50. $tm = $h.':'.$m.':'.$s;
  51. return $tm;
  52. }