Splitting an Audio File Into Equal-Lenght Segments Using FFmpeg
Asked Answered
H

1

7

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). πŸ˜€

Hardball answered 29/4, 2021 at 15:46 Comment(0)
S
17

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.
St answered 29/4, 2021 at 16:30 Comment(11)
Magnificent. Thank you so much for this clear and straightforward explanation. I owe you for saving my time. πŸ˜€ – Hardball
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: mega.nz/folder/ssljmAaA#7Z8XqHTRDYoo3Q8rzMNhig Any suggestions? Thanks! – Hardball
@G-Power Can you show your ffmpeg command and the complete log? – St
Sure. Here's the link to the log: mega.nz/file/… Sorry I couldn't just paste it here. It's too long so stackoverflow doesn't let me. Cheers, and sorry for answering late. – Hardball
@G-Power 1. Download 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. – St
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? – Hardball
@G-Power I forgot to mention that MP3 requires a delay/silence to the beginning of each file. See the LAME Technical FAQ, 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. – St
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.πŸ˜€ – Hardball
@G-Power Need to see the new command with the complete log. – St
Here's the log: mega.nz/file/… I just copied the whole PS output and dumped it in a log file. Thanks again. :) – Hardball
@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. – St

© 2022 - 2024 β€” McMap. All rights reserved.