6

So far I have been using the following command to split an audio file from S to E:

ffmpeg -i input.mp3 -ss S -to E -c copy output.mp3

Due to the fact that I need to split an audio file into multiple segments, each having a different length, I have to use the above command multiple times for only a single file.

So, is there a way to split a single audio file into multiple segments using ffmpeg, but using only a single command? Note that the segments are not of the same length.

2
  • Are the segments overlapping?
    – Gyan
    Commented Oct 9, 2019 at 15:28
  • @Gyan the next segment's starting time is the same as the previous segment ending time. for example seg1 from 0 to 8 and seg2 from 8 to 22 and so on. Commented Oct 9, 2019 at 15:41

2 Answers 2

8

If the segments are non-overlapping, you can use the segment muxer.

ffmpeg -i input.mp3 -vn -c copy -f segment -segment_times 0,8,22 output%d.mp3
5
  • thanks for the answer! is there a way to specify an output name for each segment, instead of having it indexed with %d? Commented Oct 9, 2019 at 16:01
  • No, the filename template is limited to sequences or datetime
    – Gyan
    Commented Oct 9, 2019 at 16:54
  • i just tried the same and main segment command is -f segment -segment_times 0,98,230, and as an output i got two output, from which first is not playing and another one is full audio without splitting, what i'm doing wrong?? Commented Jun 26, 2021 at 5:07
  • ohk, i just solved it by removing 0 from -segment_times and now this portion is something like -segment_times 98,230, but why with 0, it's not working for me?? Commented Jun 26, 2021 at 5:17
  • even sometimes command throws error [segment @ 0x7b483fce00] dimensions not set Could not write header for output file #0 (incorrect codec parameters ?): Invalid ar, i tried to search error but can't find anything about it(error occurs only with some audio files,) one thing i found in log is this - [mp3 @ 0x7aed7c1c00] Could not find codec parameters for stream 1 (Video: png, none(pc)): unspecified size Consider increasing the value for the 'analyzeduration' (0) and 'probesize' (5000000) options Input #0, mp3, from '/storage/emulated/0/Download/Hui Malang - Malang 128 Kbps.mp3' Commented Jun 26, 2021 at 9:25
3

Sure, just give it more output files:

ffmpeg -i input.mp3 -ss S -to E -c copy output1.mp3 -ss S -to E -c copy output2.mp3 …

Options after the input file actually pertain to the output file (so the -c, -ss and -to options are for the output file). And you can have multiple output files.

(Unlike the segments muxer, you can have overlapping output this way if you want. Or different codecs, or metadata. But @Gyan's answer with the segment muxer is easier if its restrictions are OK for you).

You must log in to answer this question.

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