Search code examples
phpffmpegmp3ffmpeg-php

FFMPEG-PHP Code example to trim MP3 file


I've searched here and online,. and have not found clear PHP examples for taking a single MP3 file, and trimming it to a smaller file (removing both beginning and end portions and retaining only the content between a specified start time and end time)..

What I'm trying to do is pass the start time, end time and mp3 file name to a PHP script, that will then use FFMPEG-PHP to effectively trim or concat that orig file.. without re-encoding it (and retaining all the orig. file metadata, etc.)

If you provide an example of how to process an MP3 file like this, within one PHP file, it will help a lot! I'm trying my best to help out our church, and simply don't know FFMPEG-PHP enough to sort this out solo.. :)

Thanks!!


Solution

  • I usually use Sox for my audio editing, it is a free command line tool that you can use with php with exec(); The command to crop an audio file:

    $ sox input.wav output.wav trim 0 10
    

    where 0 is the start point and 10 is the end. As far as retaining meta data, you will have to try it and let me know.

    http://www.thegeekstuff.com/2009/05/sound-exchange-sox-15-examples-to-manipulate-audio-files/