Jump to content

MP3 files - selecting a start and end point using PHP


willothewisp

Recommended Posts

Hi,

 

I've created a site for a client of mine which allows them to upload MP3 tracks to sell online. The client currently uploads 2 files; one containing the entire MP3 track AND a 30-second sample of the track. They create these using software on their PCs and then upload.

However, they would like to be able to upload only the full track, then to select a portion from it to use as the 30-seocnd sample. In this way they'll only have to upload one MP3 file instead of two.

The reason they want to do this is they're on slow broadband and they have a lot of tracks to upload, so want to speed up the process somehow.

 

Can anyone tell me if there's a way of doing this within PHP?

 

Thanks very much!

 

Willo

Link to comment
Share on other sites

You wouldn't want to do it with purely PHP as you'd have to decode the MP3.

 

Get the ffmpeg program. That can tell you the exact length and, I have no doubt, extract a portion of it. Then execute it with functions like exec() or even ` backticks.

Link to comment
Share on other sites

Hi Requinix,

 

Thanks for the pointer. I've had a look and I think it can do what I need. I've installed it as a PHP extension but I'm having a little trouble making it work.

What I'm doing is as follows:

echo shell_exec("ffmpeg -ss 90 -t 30 -y -i inputfile.mp3 -acodec copy outputfile.mp3");

 

So I'm selecting a start point of 90 seconds and a time (duration) of 30 seconds. I'm also telling it to copy the file without changing the bitrate of the file, which is what the -acodec parameter is for. However, it's not creating the output file. Can anyone see anything wrong with the above code, or suggest why it might not be creating the output file?

 

Thanks,

 

Willo

Link to comment
Share on other sites

What happens if you run your ffmpeg command on the command line? That would be the first thing to check. If that works then it's probably an issue with permissions for php creating the file.

 

Hi Requinix,

 

Thanks for the pointer. I've had a look and I think it can do what I need. I've installed it as a PHP extension but I'm having a little trouble making it work.

What I'm doing is as follows:

echo shell_exec("ffmpeg -ss 90 -t 30 -y -i inputfile.mp3 -acodec copy outputfile.mp3");

 

So I'm selecting a start point of 90 seconds and a time (duration) of 30 seconds. I'm also telling it to copy the file without changing the bitrate of the file, which is what the -acodec parameter is for. However, it's not creating the output file. Can anyone see anything wrong with the above code, or suggest why it might not be creating the output file?

 

Thanks,

 

Willo

Link to comment
Share on other sites

OK, I found out what the problem was. I needed to give the full path to ffmpeg to make it run using PHP. So it ended up being $output = shell_exec(`/usr/bin/ffmpeg -ss 90 -t 30 -y -i inputfile.mp3 outputfile.mp3 2>&1`);
 

Now, my next question is, I need to check, using PHP that the 30-second sample of the file I've created above has actually been created. I was hoping I could get shell_exec to return me a success or fail status, but I don't know how to do it. I've tried shell_exec("/usr/bin/ffmpeg -ss 90 -t 30 -y -i inputfile.mp3 outputfil.mp3 2>&1 1> /dev/null") but that returns me a load of info about the conversion (great), but no simple flag I can pick up to say 'this has been successful',

 

It's been a long week and I might be being stupid ;-) but can anyone suggest how I might get it to return me something I can use?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.