0

I have a folder with several hundred MP3 files in it, and I want to cut two sections out of each file. It's the same spots in each file -- from 0'00.00–0'03.81, and then from 0'08.83–0'13.95.

I'm on Windows 7, though I can easily use 8 or 10 if it helps. Not squeamish about working at a command line, either.

Any idea how I can achieve this?

6
  • I did see a program called mp3splt. command line usage is- mp3splt song.mp3 10.12 14.25 -o out.mp3 (Split song.mp3 starting at 10min 12sec, ending at 14mins 25sec and save the mp3 slice in a new file called out.mp3)" (superuser.com/questions/277678/… ( as written in a comment of mine on this question) So if you manage to try that on one file, the question is simply how to apply that to a batch of files. And the way i'd do that is dir /s/b *.mp3 >filelist.txt
    – barlop
    Commented Mar 17, 2018 at 21:30
  • Then (rather like the ancient bouncing off the command prompt technique, but without bouncing off the command prompt), i'd use some text editor that supports regexes to convert those filenames into the command you want. e.g. adding to the beginning and end of each line so each line is a command for that fille. And then rename the file as bat and execute it. The other option is for /f feeding it a list of files and letting that generate each command. But try step 1 first, that's mp3splt on one file.
    – barlop
    Commented Mar 17, 2018 at 21:33
  • I can see how I would use that to cut out the first section. But how would I use it to do the second, where really I'm dividing the file into the sections & just removing the second? The command you've given seems to want to cut out a slice and keep only it, rather than cut out a slice and remove it. I've looked at the man page for mp3splt, and I don't see what I need.
    – spoko
    Commented Mar 17, 2018 at 22:10
  • I guess having split a file up you can put it back together without the parts you don't want. askubuntu.com/questions/20507/… and stackoverflow.com/questions/62618/…
    – barlop
    Commented Mar 17, 2018 at 23:04
  • 1
    there is also ffmpeg but sometimes when not sure what specific parameters to pass to it it can be tricky moreso even than usual here! stackoverflow.com/questions/43728169/…
    – barlop
    Commented Mar 17, 2018 at 23:06

1 Answer 1

1

So, I ended up figuring out how to do this with ffmpeg. In case anybody else is wondering at some point, here's how.

Btw: These are 216 files all in one folder, and part of the filename is a sequential number. That made it a bit easier for the DOS looping.

The first thing I did was to create a plain text file, called temps.txt , which has just these two lines in it:

file temp1.mp3
file temp2.mp3

You'll see what that's for in a minute.

Then I wrote a batch file that loops through all the files in the directory (based on the sequential numbering above), and runs two ffmpeg operations on each:

FOR /L %%i IN (1,1,216) DO (
  ffmpeg.exe -y -i OriginalFile%%i.mp3 -ss 00:03.81 -to 00:08.83 -acodec copy "temp1.mp3" -ss 00:13.95 -acodec copy "temp2.mp3"
  ffmpeg.exe -y -safe 0 -f concat -i temps.txt -c copy NewFile%%i.mp3
)

The first operation grabs the two desired segments and copies them to temp1.mp3 and temp2.mp3 (I kept reusing those same two filenames). ffmpeg allows you to isolate both sections on one command line, which is nice. Also, you may note that the second part only has the -ss timestamp, and no -to stamp. That allows it to just use the End of File as the second timestamp.

The second operation tells ffmpeg to look in the temps.txt file and concatenate the two files that are listed there. Reusing the temp file names is why I was able to just have the static temps.txt file that I created above.

I wrote this fairly bluntly (hard-coding the number of files in the directory, reusing temp files, using the -y flag in the command lines), because I knew exactly what I had to work with, and I had a backup of it anyway. If you're repurposing this, you might want to be a little more delicate about those things.

Also, I lost all the metadata in this process. There's probably an easy way to preserve that, but I didn't bother figuring it out. Easy enough for me to just re-add it to the new files all at once (using MP3Tag, one of the greatest utilities of all time).

So, there you have it.

Btw, some links that helped me figure this out:

  • I started with ffmpeg - Trim audio file without re-encoding
  • Then to fiddle with the specifics I referenced ffmpeg Documentation
  • For the concatenation I got a lot from this answer to a Stack Overflow question. The question was about video, which is much more complicated, but this answer told me everything I needed to know about audio as well. Specifically, I used the second method that this answer describes.
  • And finally, I didn't really use this one, but there's more useful info about the concat command on this wiki -- including instructions for auto-generating a list file on either Windows or bash.

(Thanks @barlop for the suggestion to add those.)

1
  • Impressive.. btw, if you found any particular links useful in getting to that answer then it's worth including them
    – barlop
    Commented Mar 18, 2018 at 20:22

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .