willothewisp Posted June 10, 2013 Share Posted June 10, 2013 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 Quote Link to comment https://forums.phpfreaks.com/topic/278990-mp3-files-selecting-a-start-and-end-point-using-php/ Share on other sites More sharing options...
requinix Posted June 10, 2013 Share Posted June 10, 2013 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. Quote Link to comment https://forums.phpfreaks.com/topic/278990-mp3-files-selecting-a-start-and-end-point-using-php/#findComment-1435170 Share on other sites More sharing options...
willothewisp Posted June 13, 2013 Author Share Posted June 13, 2013 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 Quote Link to comment https://forums.phpfreaks.com/topic/278990-mp3-files-selecting-a-start-and-end-point-using-php/#findComment-1435757 Share on other sites More sharing options...
taquitosensei Posted June 13, 2013 Share Posted June 13, 2013 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 Quote Link to comment https://forums.phpfreaks.com/topic/278990-mp3-files-selecting-a-start-and-end-point-using-php/#findComment-1435758 Share on other sites More sharing options...
willothewisp Posted June 14, 2013 Author Share Posted June 14, 2013 Thanks taquitosensei, I hadn't thought of that. It works thru the command line so I guess it could be to do with permissons. I'll get my hosts to investigate. Thanks, Kelly Quote Link to comment https://forums.phpfreaks.com/topic/278990-mp3-files-selecting-a-start-and-end-point-using-php/#findComment-1435919 Share on other sites More sharing options...
willothewisp Posted June 14, 2013 Author Share Posted June 14, 2013 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? Quote Link to comment https://forums.phpfreaks.com/topic/278990-mp3-files-selecting-a-start-and-end-point-using-php/#findComment-1435978 Share on other sites More sharing options...
kicken Posted June 14, 2013 Share Posted June 14, 2013 I was hoping I could get shell_exec to return me a success or fail status Use exec() rather than shell_exec, then you can get back the return code from ffmpeg and check for success. You could also test for the output file using file_exists to make sure it was created. Quote Link to comment https://forums.phpfreaks.com/topic/278990-mp3-files-selecting-a-start-and-end-point-using-php/#findComment-1435982 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.