1

I've got a folder full of FFMPEG batch files that I've written to do various conversion tasks. These allow me to drag and drop a bunch of files and do a conversion - for example, decompressing MP3 to WAV files, cropping XVID files etc.

Now I want to write one that splits an MP3 into smaller chunks. There's a neat FFMPEG command for this

ffmpeg -f mp3 -i /path/to/file.mp3 -t 00:00:00 -ss 00:10:00 -y /path/to/save.mp3

However, this only extracts the first 10 minutes. To go through the whole MP3 I'd have to write similar statements for every 10 minutes of the MP3's length. Is there a way to tokenise this in batch files?

2 Answers 2

1

Well, there's two ways to go about this:

  1. You can provide the batch file with command line arguments ffchunk.cmd *start* *end* which will give you flexibility in determining the chunk lengths. The batch file would look something like this:

    `ffmpeg -f mp3 -i /path/to/file.mp3 -t 00:%1:00 -ss 00:%2:00 -y
    /path/to/save%1-%2.mp3`
    

    , where the %1 and %2 represent the first and second arguments given at the command line, respectively. You would invoke it as ffchunk 00 10.

  2. Provide a loop inside the batch file to iterate as much as you need:

     `@echo off
     setlocal ENABLEEXTENSIONS
     setlocal ENABLEDELAYEDEXPANSION
    
     for /L %%i in (1,1,10) do (
      set /a j=%%i-1
      set k=%%i
      if !j! lss 10 set j=!j!0
      if !k! lss 10 set k=!k!0
      ffmpeg -f mp3 -i /path/to/file.mp3 -t 00:!j!:00 -ss 00:!k!:00 -y /path/to/save!j!-!k!.mp3
     )`
    
1
  • Thanks scott. Good answer re: looping and and iterating. I'm accepting the other answer as it's a simpler way to do what I'm after, but I'm also giving yours an upvote.
    – roryok
    Sep 26, 2013 at 8:48
1

You could use ffmpeg's segment muxer. Something like:

ffmpeg -i input.mp3 -map 0 -c copy -f segment -segment_time 600 out-%02d.mp3

...will produce a number of files along the lines of out-00.mp3, out-01.mp3, and so on, each 600 seconds long (except for the final part, which will be the remainder). There are accuracy issues using the segment muxer with videos (though these can be overcome, see the documentation I linked to), but I've never had any problems when working with audio files.

5
  • That seems like the best way to do it. a neat, one line ffmpeg command. thanks!
    – roryok
    Sep 26, 2013 at 8:49
  • EDIT: I get a bunch of errors with that 'unrecognized option -c', if I change that to -acodec 'unrecognized option -segment_time'
    – roryok
    Sep 26, 2013 at 8:58
  • @royok what version of ffmpeg are you using? Make sure you're using an up-to-date version (works for me with the static version linked to here).
    – evilsoup
    Sep 26, 2013 at 9:19
  • I am using an older version. but when I upgrade to that static version I get other errors associated with using a drag and drop batch file. ffmpeg seems to take the name of the batch file as a parameter rather than the name of the dropped file!
    – roryok
    Sep 26, 2013 at 13:46
  • Dang, sorry to hear that. Sounds like you might want to hit up the ffmpeg users mailing list (they will insist that you use the most recent version though). More than once I've brought issues up there, and then some developer has fixed it within the week.
    – evilsoup
    Sep 26, 2013 at 16:51

You must log in to answer this question.

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