doufu1970 2013-12-28 15:39
浏览 45
已采纳

获取媒体文件持续时间(服务器端)

I'm looking for a way to get an accurate duration of media files. I've researched many solutions online including the getID3 library but ID3 tags are too easily edited or may not be populated in certain cases. I'm on a shared host at the moment so the ffmpeg library isn't an option either as I can't install anything on this host (GoDaddy).

I'm primarily looking at MP3, WMV, WAV, MP4, MPEG, AVI, and MOV files at the moment. Is there a way to get their duration using, preferably, PHP? I'd be happy with any solution that works though.

Will truly appreciate help.

Thanks

  • 写回答

1条回答 默认 最新

  • du8980919 2014-01-01 16:41
    关注

    This one looks like it supports multiple media formats.

    http://www.daniweb.com/web-development/php/threads/428301/getting-video-duration-without-ffmpeg

        function getDuration($file){
            if (file_exists($file)){
                ## open and read video file
                $handle = fopen($file, "r");
    
                ## read video file size
                $contents = fread($handle, filesize($file));
                fclose($handle);
                $make_hexa = hexdec(bin2hex(substr($contents,strlen($contents)-3)));
                if (strlen($contents) > $make_hexa){
                    $pre_duration = hexdec(bin2hex(substr($contents,strlen($contents)-$make_hexa,3))) ;
                    $post_duration = $pre_duration/1000;
                    $timehours = $post_duration/3600;
                    $timeminutes =($post_duration % 3600)/60;
                    $timeseconds = ($post_duration % 3600) % 60;
                    $timehours = explode(".", $timehours);
                    $timeminutes = explode(".", $timeminutes);
                    $timeseconds = explode(".", $timeseconds);
                    $duration = $timehours[0]. ":" . $timeminutes[0]. ":" . $timeseconds[0];}
                    return $duration;
                } else {
                    return false;
                }
            }
    

    In order to use it just do the following:

    ## first, define video file and location
    $video_file = "somedirectory/video.flv";
    ## call out the function
    echo getDuration($video_file);
    

    I copped that from the link above. I tried it on a couple of media types that you mentioned and it seemed to work fine.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 宇视监控服务器无法登录
  • ¥15 PADS Logic 原理图
  • ¥15 PADS Logic 图标
  • ¥15 电脑和power bi环境都是英文如何将日期层次结构转换成英文
  • ¥20 气象站点数据求取中~
  • ¥15 如何获取APP内弹出的网址链接
  • ¥15 wifi 图标不见了 不知道怎么办 上不了网 变成小地球了
  • ¥50 STM32单片机传感器读取错误
  • ¥50 power BI 从Mysql服务器导入数据,但连接进去后显示表无数据
  • ¥15 (关键词-阻抗匹配,HFSS,RFID标签天线)