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 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog