2

I have a folder with several MP3 files that I need to extract 10-15 seconds of audio from. I would also like to rename these by appending sample-(name).mp3 to the converted files.

How can I do this via Shell Script?

  • What is (name)? Is that the name of the mp3 file without .mp3 (i.e. do you just want to stick sample- in front)? Or is that some data you get from the MP3 (via ID3)? What determines whether it is 10 or 15 seconds, or something in between? Do you know in advance, does it have to be random, is it a fixed amount that you don't know yet right now? Where should it start? – Anthon May 15 '15 at 4:21
  • Anthon - multiple file names such as file-description-other.mp3, want the output to be sample-file-description-other.mp3 – Designs Edge May 15 '15 at 4:24
2

If you want/need to use sox for this you can use its trim command:

for i in *.mp3
do 
  sox "$i" sample-"$i" trim 0 10
done

The splitting you can also do with the commandline utility that is part of mp3splt. You explicitly set the output file with -o, so the originals are not touched, just remove them after you are done with them. This allows you to incorporate tags defined in the file in the output name than sox does (what you don't seem to need right now):

for i in *.mp3
do 
  mp3splt "$i" 00.00.00 00.10.00 -o sample-"${i%.mp3}"
done

Note that -o normally works with @ based directives to include tag elements in the output name and appends .mp3. Without ${i%.mp3} you would get .mp3.mp3 files.

Times are dot separated, don't try to use : instead, you get a less than useful error message that you don't have enough split points.

I would not remove the input files until you have tested that the scripts works. Also note that if you stop it halfway, or add files later, that you cannot re-run it without removing any sample- files first. You might want to specify a directory before sample-..., to keep things apart.

  • Installed - any idea the CL syntax other than -o? Just need 0-10 seconds. – Designs Edge May 15 '15 at 4:32
  • @DesignsEdge I updated it with the details for that. The tricky part is that -o normally takes some ID3 based @ directives and appends .mp3 which you have to cut – Anthon May 15 '15 at 4:40
  • @DesignsEdge also put in a version that should work with sox – Anthon May 15 '15 at 4:56
  • You are the man! But in a bash script and poof! Thanks for teaching me something new, it is much appreciated! – Designs Edge May 15 '15 at 5:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.