Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Automatically trimming an mp3 in PHP

Tags:

php

trim

upload

mp3

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 be integrated (transparently to the user) to achieve the same effect?

Thanks.

like image 646
Meep3D Avatar asked Sep 10 '09 12:09

Meep3D


4 Answers

You could try the MP3 Class on PHPClasses. It features the following example:

require_once './class.mp3.php';
$mp3 = new mp3;
$mp3->cut_mp3('input.mp3', 'output.mp3', 0, -1, 'frame', false);

In this case, the 'frame' can be substituted with 'second' to base the cut on a time-frame.

like image 75
Sampson Avatar answered Oct 14 '22 19:10

Sampson


I used PHP MP3 for my project.

<?php
//Extract 30 seconds starting after 10 seconds.
$path = 'path.mp3';
$mp3 = new PHPMP3($path);
$mp3_1 = $mp3->extract(10,30);
$mp3_1->save('newpath.mp3');
?>

For your case you can use extract(0,30) or extract(30,60).

like image 40
Poohspear Avatar answered Oct 14 '22 17:10

Poohspear


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;
like image 3
Stephen Walcher Avatar answered Oct 14 '22 18:10

Stephen Walcher


In Debian/ubuntu try installing mpgtx:

apt-get install mpgtx

mptsplit input.mp3 [00:00:00-00:00:30] -o output.mp3

I'm sure you'll find mpgtx available in other fine Linux distros too, or just install from source.

like image 2
Paul Dixon Avatar answered Oct 14 '22 18:10

Paul Dixon