4

I want to split an audio file into several equal-length segments using FFmpeg. I want to specify the general segment duration (no overlap), and I want FFmpeg to render as many segments as it takes to go over the whole audio file (in other words, the number of segments to be rendered is unspecified). Also, since I am not very experienced with FFmpeg (I only use it to make simple file conversions with few arguments), I would like a description of the code you should use to do this, rather than just a piece of code that I won't necessarily understand, if possible. Thank you in advance.

P.S. Here's the context for why I'm trying to do this: I would like to sample a song into single-bar loops automatically, instead of having to chop them manually using a DAW. All I want to do is align the first beat of the song to the beat grid in my DAW, and then export that audio file and use it to generate one-bar loops in FFmpeg.

In the future, I will try to do something like a batch command in which one can specify the tempo and key signature, and it will generate the loops using FFmpeg automatically (as long as the loop is aligned to the beat grid, as I've mentioned earlier).

GPWR
  • 186
  • 3
  • 11

1 Answers1

12

You can use the segment muxer. Basic example:

ffmpeg -i input.wav -f segment -segment_time 2 output_%03d.wav
  • -f segment indicates that the segment muxer should be used for the output.
  • -segment_time 2 makes each segment 2 seconds long.
  • output_%03d.wav is the output file name pattern which will result iin output_000.wav, output_001.wav, output_002.wav, and so on.
llogan
  • 121,796
  • 28
  • 232
  • 243
  • 1
    Magnificent. Thank you so much for this clear and straightforward explanation. I owe you for saving my time. – GPWR Apr 29 '21 at 18:13
  • I just noticed, when "rebuilding" the song from the outputted segments in my DAW, that there is a huge clip at the beginning of each sample. Here's a link to the audio files I generated, so you can see what I'm talking about: https://mega.nz/folder/ssljmAaA#7Z8XqHTRDYoo3Q8rzMNhig Any suggestions? Thanks! – GPWR Apr 29 '21 at 18:40
  • @G-Power Can you show your `ffmpeg` command and the complete log? – llogan Apr 29 '21 at 19:14
  • Sure. Here's the link to the log: https://mega.nz/file/t1EzHKIR#1AiaI4SfnQyOKybIATM2pArIUAvXC6uaoaSgDUWaV18 Sorry I couldn't just paste it here. It's too long so stackoverflow doesn't let me. Cheers, and sorry for answering late. – GPWR May 04 '21 at 14:03
  • @G-Power 1. [Download](https://www.gyan.dev/ffmpeg/builds/) a more recent ffmpeg (git version). 2. Your ffmpeg is using the mp3_mf encoder which may be problematic. Try adding output option `-c:a libmp3lame` for one command, and `-c:a copy` for another command. See which outputs sound better. – llogan May 04 '21 at 16:10
  • Hello. Thanks again for your clear explanations. So I downloaded the newest version of FFmpeg (I think it was updated on the fifth of may, actually) and I tried both "libmp3lame" and "copy". "copy" is horrible (I mean for me) but libmp3lame seems to work fairly well (the clipping is negligible, but it's still there). The problem is that the clips are a fraction of a second longer than given, so there's a silence at the beginning of each segment. Thank you so much again, llogan. P.S. Maybe a little late to ask, but am I using the right tool? – GPWR May 07 '21 at 12:08
  • @G-Power I forgot to mention that MP3 requires a delay/silence to the beginning of each file. See the [LAME Technical FAQ](https://lame.sourceforge.io/tech-FAQ.txt), specifically *Why is a decoded MP3 longer than the original .wav file?*, *Why does LAME add silence to the beginning each song?*, and *Why cant MP3 files be seamlessly spliced together?*. `ffmpeg` can be the right tool, but you just have to be aware of any caveats with whatever formats you use. I suggest using WAV as your intermediate files and then export the final audio as MP3 or whatever. – llogan May 07 '21 at 17:33
  • Very nice. When I use .wav as the input format, there is no clipping or "padding" in the output files, whatever the format I export to. My only problem left (for now , hehe) is that my outputted files are not exactly the same length. But there is no silencing so, when I put them head-to-tail, I can "reconstruct" the initial song without clipping nor silencing, so we're almost there. I went through the article about LAME, which was very interesting, but did not find an answer (yet) to this phenomenon. Have an idea? BTW, thx so much for helping me out with this. It's very appreciated. – GPWR May 09 '21 at 13:54
  • @G-Power Need to see the new command with the complete log. – llogan May 10 '21 at 16:23
  • Here's the log: https://mega.nz/file/tltkwDrb#trWxC9X1st0JR65nLdY8EB8HVb1LOXfgIA6wwveHS5w I just copied the whole PS output and dumped it in a log file. Thanks again. :) – GPWR May 10 '21 at 18:01
  • @G-Power Answer is to avoid any format that requires priming samples such as MP3. Output WAV. Then when you concatenate everything you can encode the final, combined output as MP3 if you need to. – llogan May 10 '21 at 18:10