Automatically trimming an mp3 in PHP

前端 未结 7 2253
太阳男子
太阳男子 2020-12-04 22:15

Are there any ways to automatically trim an MP3 uploaded to a website to 30 seconds (or some other length) in PHP? If not, is there any good 3rd party services that could b

7条回答
  •  借酒劲吻你
    2020-12-04 22:55

    I put together a script that outputs a 30 second clip of an MP3 file on the fly. If you're looking to save the file, one of the other options using a class/library will probably be best. But, if you just want to play/download the preview, on the fly might be better. It will definitely save you hard drive space.

    Check it out at http://www.stephenwalcher.com/2013/06/17/how-to-extract-and-play-part-of-an-mp3-in-php/.

    Here's the code, but a deeper explanation can be found on my blog.

    $getID3 = new getID3();
    
    $id3_info = $getID3->analyze($filename);
    
    list($t_min, $t_sec) = explode(':', $id3_info['playtime_string']);
    $time = ($t_min * 60) + $t_sec;
    
    $preview = $time / 30; // Preview time of 30 seconds
    
    $handle = fopen($filename, 'r');
    $content = fread($handle, filesize($filename));
    
    $length = strlen($content);
    
    if (!$session->IsLoggedIn()) {
        $length = round(strlen($content) / $preview);
        $content = substr($content, $length / 3 /* Start extraction ~10 seconds in */, $length);
    }
    
    header("Content-Type: {$id3_info['mime_type']}");
    header("Content-Length: {$length}");
    print $content;
    

提交回复
热议问题