Coder Social home page Coder Social logo

falahati / php-mp3 Goto Github PK

View Code? Open in Web Editor NEW
45.0 3.0 17.0 37 KB

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

License: GNU Lesser General Public License v3.0

PHP 100.00%
mp3 audio-processing audio mp3-tags mpeg-audio

php-mp3's Introduction

PHP-MP3

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

This library is based on a similar project with the same name written by thegallagher which it-self was based on an script from regin.

This is not an encoder or decoder and therefore can't change the MP3 properties like bitrate, sample size, and sample rate. It's a MPEG Audio parser and therefore it should only be used to modify/create/read valid MP3 containers.

How to get

You can install and use this library with composer:

composer require falahati/php-mp3:dev-master

Features

  • Correctly identifies MPEG Audio Version 1, 2 and 2.5
  • Correctly identifies MPEG Audio Profile 1, 2 and 3
  • Correctly extracts bitrate and sample rate information
  • Correctly calculates MPEG Audio stream duration
  • Frame address recovery allows the code to correctly parse corrupt data
  • Trim (Cut) a MPEG Audio data and merge multiple MPEG audio streams
  • Ability to strip MPEG Audio data from starting and ending ID3 (or similar) metadata information

Help me fund my own Death Star

--OR--

You can always donate your time by contributing to the project or by introducing it to others.

Samples

Strip ID3 tags from a MP3 file:

\falahati\PHPMP3\MpegAudio::fromFile("old.mp3")->stripTags()->saveFile("new.mp3");

Cut a MP3 file to extract a 30sec preview starting at the 10th second:

\falahati\PHPMP3\MpegAudio::fromFile("old.mp3")->trim(10, 30)->saveFile("new.mp3");

Append memory stored MP3 data to the end of a MP3 file:

\falahati\PHPMP3\MpegAudio::fromFile("old.mp3")->append(\falahati\PHPMP3\MpegAudio::fromData(base64_decode("/**BASE64-DATA**/")))->saveFile("new.mp3");

Extracting MP3 file total duration:

echo \falahati\PHPMP3\MpegAudio::fromFile("old.mp3")->getTotalDuration();

To Do List

  • Add Unit Tests
  • Ability to load and manipulate data directly from, and to a resource
  • Ability to add simple ID3 metadata information to the MPEG Audio before saving

License

Copyright (C) 2017-2020 Soroush Falahati

This project is licensed under the GNU Lesser General Public License ("LGPL") and therefore can be used in closed source or commercial projects. However, any commit or change to the main code must be public and there should be a read me file along with the DLL clarifying the license and its terms as part of your project as well as a hyperlink to this repository. Read more about LGPL.

php-mp3's People

Contributors

benseidseid avatar falahati avatar hostcomm-steve avatar thegallagher avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

php-mp3's Issues

Using the library without loading the file into memory?

Is it at all possible? Would it be?

I'm using the library for the getTotalDuration() function, and, I just hit a (kind of) large file of 480mb that, of course, hits the allowed memory size (which is set to 512m in php.ini).

Sure, I could change the value in php.ini, and probably could use ini_set('memory_limit', '1024M') before the library runs, but I have to wonder... Is there no other way to get this info other than loading the file into memory / reading it all?

Length of a trimmed MP3 is not set correctly

When I trim an MP3 using

\falahati\PHPMP3\MpegAudio::fromFile($names[0])->trim(0, 30)->saveFile($trimmed='tmp/trimmed/trimmed.mp3');

the original length will show up in any given MP3 player when I open the trimmed file rather than 30s.

Fails on PHP 8

Steps to reproduce

  1. Clone and run a minimal test app under PHP 8.0 or above

Expected result

A two-second file called clipped.mp3 will be successfully created in the project root folder like it does when running under PHP 7.4 or lower

Actual result

The app hangs and never finishes

Fix

It looks like the bug is caused at this line, because of a breaking change in the substr PHP function where the function returns an empty string where it previously returned false. The following two-line change should fix it:

$this->memoryPointer = min($this->memoryLength, $index + $length);
$sub = substr($this->memory, $index, $length);
return empty($sub) ? false : $sub;

Trim doesn't work when start time is 0

Trying to get the first 30 seconds of a file to a new file :

$this->removeFrame($endIndex, $this->getFrameCounts() - $endIndex);

works fine, but

$this->removeFrame(0, $startIndex);

results in

Fatal error: Uncaught Error: Call to a member function getOffset() on boolean in /home/forge/simytmt.com/public/vendor/falahati/php-mp3/src/MpegAudio.php:406

Fade in and out volume when cutting mp3

I really like your class and I use it to create preview files from my band's mp3 on our homepage. Could you probably enhance the class to change the volume, like a fade in / fade out?

Would make this even more useful for us musicians!

Trimming audio is not working

$mp3 = \falahati\PHPMP3\MpegAudio::fromFile($request->song)->trim(1, 30)->saveFile(storage_path('app/public/PreviewSong').'/new.mp3');

I am using the above code to trim audio but trim is now working. The whole audio file saved to the destination. Kindly help ASAP @falahati

Removal of ID3 metadata information

Hello, First of all great job with this project.

I wonder if it is possible to remove existing ID3 metadata information from the resulting file.
I noticed that it keeps the metadata of the last file when I join multiple files.
My code is something like this:

$mpeg_audio = new MpegAudio();
foreach ($files as $file_path) {
  $mpeg_audio->append($mpeg_audio->fromFile($file_path));
}
$mpeg_audio->saveFile($final_path);

Installing with composer doesn't work

I tried

composer require falahati/php-mp3

And I get

  [InvalidArgumentException]                                                                                                                                                                                          
  Could not find a matching version of package falahati/php-mp3. Check the package spelling, your version constraint and that the package is available in a stability which matches your minimum-stability (stable). 

Php 8.1 File append not working

Hello
I am trying to append multiple mp3 files (same encoding), but the files do not get appended.
Only the first file is saved in the final output.
Example:

$file_mp3= \falahati\PHPMP3\MpegAudio::fromFile("audio1.mp3");
$file_mp3->append(\falahati\PHPMP3\MpegAudio::fromFile("audio2.mp3"));
$file_mp3->saveFile("final_file.mp3");

No errors, only the first mp3 is added in the final file, and the final saved file size is equal to file 1.

Can you please help?

Wrong length for MPEG-2 Layer 3

Rename the attached test.txt to test.mp3 (Github doesn't allow MP3 attachment).

The MP3 is detected as MPEG-2 Layer 3:

falahati\PHPMP3\MpegAudioFrameHeader Object
(
    [bitRate:falahati\PHPMP3\MpegAudioFrameHeader:private] => 48000
    [sampleRate:falahati\PHPMP3\MpegAudioFrameHeader:private] => 11025
    [version:falahati\PHPMP3\MpegAudioFrameHeader:private] => 2
    [profile:falahati\PHPMP3\MpegAudioFrameHeader:private] => 3
    [duration:falahati\PHPMP3\MpegAudioFrameHeader:private] => 0.1045
    [offset:falahati\PHPMP3\MpegAudioFrameHeader:private] => 3336
    [length:falahati\PHPMP3\MpegAudioFrameHeader:private] => 627
    [padding:falahati\PHPMP3\MpegAudioFrameHeader:private] => 1
    [errorProtection:falahati\PHPMP3\MpegAudioFrameHeader:private] => 1
    [privateBit:falahati\PHPMP3\MpegAudioFrameHeader:private] =>
    [copyrighted:falahati\PHPMP3\MpegAudioFrameHeader:private] => 1
    [original:falahati\PHPMP3\MpegAudioFrameHeader:private] => 1
    [mode:falahati\PHPMP3\MpegAudioFrameHeader:private] => 3
    [middleSideStereoJoining:falahati\PHPMP3\MpegAudioFrameHeader:private] =>
    [intensityStereoMode:falahati\PHPMP3\MpegAudioFrameHeader:private] => 0
)

The current length calculation in MpegAudioFrameHeader::tryParse() is:

if ($frame->profile == self::Profile_1) {
	$frame->length = (((12 * $frame->bitRate) / $frame->sampleRate) + $frame->padding) * 4;
} else if ($frame->profile == self::Profile_2 || $frame->profile == self::Profile_3) {
	$frame->length = ((144 * $frame->bitRate) / $frame->sampleRate) + $frame->padding;
}

I cannot find a mention of this in the ISO, but I've seen several sources claim that MPEG-2 Layer 3 samples per frame is acutally 576 / 8 = 72 instead of 1152 / 8 = 144, for example, https://stackoverflow.com/questions/62536328/mpeg-2-and-2-5-problems-calculating-frame-sizes-in-bytes.

Also, claims that padding should be part of the denominator (144 * bitRate / (sampleRate + padding) instead of 144 * bitRate / sampleRate + padding), for example, https://stackoverflow.com/questions/57103676/formula-from-mp3-frame-length.

When I try to append the test MP3 to another MP3 it fails because of this check in MpegAudio::read():

if ($this->memoryPointer >= $this->memoryLength) {
	return false;
}

Updating the frame length calculation to $frame->length = 72 * $frame->bitRate / ($frame->sampleRate + $frame->padding); fixes this issue.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.