19

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.

Meep3D
  • 3,803
  • 4
  • 36
  • 55

7 Answers7

22

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.

Sampson
  • 265,109
  • 74
  • 539
  • 565
3

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).

Poohspear
  • 29
  • 3
  • Much as this is good script I would be aware it is available under the LGPL which I don't believe is as flexible as other licenses. – Antony Sep 04 '17 at 14:12
  • @Antony, LGPL is pretty flexible and unless you change something in the library itself, you don't need to do anything special except distributing the license text file with the package. And you can use it in commercial projects as well. – Soroush Falahati Sep 15 '17 at 15:04
  • This class does not seem to set the length of a trimmed MP3. When I open the trimmed version, the player shows the original length, the audio will break up after the trimmed length, though. – Armin Hierstetter Apr 24 '23 at 08:38
3

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;
Stephen Walcher
  • 2,565
  • 1
  • 21
  • 22
2

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.

Paul Dixon
  • 295,876
  • 54
  • 310
  • 348
2

I had problems processing some mp3s with both MP3 Class and mpgtx. The best option here is ffmpeg I think. This page has some good examples on splitting media files with ffmpeg.

Vincent
  • 16,086
  • 18
  • 67
  • 73
1

https://github.com/falahati/PHP-MP3

PHP-MP3 is a simple library for reading and manipulating MPEG audio (MP3).

Install:

composer require falahati/php-mp3:dev-master

Cut MPEG Audio:

\falahati\PHPMP3\MpegAudio::fromFile("old.mp3")->trim(10, 30)-saveFile("new.mp3");
Soroush Falahati
  • 2,196
  • 1
  • 27
  • 38
0
//Merge two files
 $path = 'path.mp3';
 $path1 = 'path1.mp3';
 $mp3 = new PHPMP3($path);

 $newpath = 'path.mp3';
$mp3->striptags();

$mp3_1 = new PHPMP3($path1);
$mp3->mergeBehind($mp3_1);
  $mp3->striptags();

 $mp3->setIdv3_2('01','Track Title','Artist','Album','Year','Genre','Comments','Composer','OrigArtist','Copyright','url','encodedBy');

$mp3->save($newpath);

//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');

//Extract the exact length of time
$path = 'path.mp3';
$mp3 = new PHPMP3($path);
$mp3->setFileInfoExact();
echo $mp3->time;
//note that this is the exact length!

source : https://github.com/thegallagher/PHP-MP3/blob/master/README.md

Pooja Khatri
  • 550
  • 5
  • 11