Using ffmpeg to cut audio from/to position

42,192

Solution 1

With FFmpeg the ordering of parameters is significant. All of the parameters that come directly before an input will apply to that input. The same is true for an output... the parameters directly before it apply to the output.

Consider this command line:

ffmpeg -ss 132 -i input.mp3 output.mp3

-ss is the parameter to seek, so FFmpeg will seek the input file to 132 seconds in and treat that effectively at 00:00:00. The rest of your FFmpeg commands relative to the output don't know or care where that input came from or how it was seeked. Therefore, when you use -to or -t, the times or lengths given need to be relative to the input. That is, if you want seconds 132 through 139, you want to seek the input to 132 (-ss 132 -i input.mp3), and then run the output for 7 seconds (-t 7 output.mp3 or -to 00:00:07 output.mp3).

You can read more about this, as well as details about frame-accurate or not (for re-encoding or not) on the documentation: https://trac.ffmpeg.org/wiki/Seeking

As for -to not being there...

As I shown above, I have the latest version of the software.

You absolutely positively do not remotely have the latest version of FFmpeg. You might have the latest build of whatever branch whatever package manager has, and it may have been built this year, but if you check the download page, the latest release as of this writing is 3.3.4. https://www.ffmpeg.org/download.html

You can either deal with your package manager and dependency hell, or depending on your licensing restrictions, snag a recent static build: https://www.johnvansickle.com/ffmpeg/

Finally, consider -acodec copy to ensure you're not hurting the quality of your audio further by transcoding, since you're keeping the same format.

Solution 2

This works for me

ffmpeg -ss 60 -i input-audio.aac -t 15 -c copy output.aac
  • -ss 60 means, "start from second 60"
  • -t 15 audio output length in seconds.. in this case, 15 seconds..

Solution 3

ffmpeg -i "original.mp3" -ss 60 -to 70 "new.mp3"

This works for me.

Solution 4

To Trim Audio file using FFmpeg

startDuration = 00:00:00, endDuration = 00:00:10

audioPath = audio.mp3, mp3Output = outputFile.mp3

cmd = "-ss $startDuration -i $audioPath -c copy -t $endDuration $mp3Output"

Share:
42,192
Mike
Author by

Mike

Updated on July 09, 2022

Comments

  • Mike
    Mike almost 2 years

    I need to cut parts from an audio file from position to position. When I tried this command

    ffmpeg -ss 132 -t 139 -i original.mp3 new.mp3
    

    it started at the second 132, and added the next 139 seconds to the new file.

    What I need is from second 132 to second 139.

    And when I tried

    ffmpeg -ss 132 -to 139 -i original.mp3 new.mp3
    

    it gives me an error:

    Unrecognized option 'to'
    Failed to set value '139' for option 'to'
    

    (I don't want to tell how many seconds to take, just from/to).

    -- I have ffmpeg version 0.8.20-6:0.8.20-0+deb7u1 (built on Jan 19 2017 11:13:36 with gcc 4.7.2). And when I try to update it (apt-get install ffmpeg), it tells me "ffmpeg is already the newest version".

  • Brad
    Brad almost 7 years
    Note that this is a highly inefficient way to seek. It's far more efficient to seek the input before starting the output.
  • llogan
    llogan almost 7 years
    Sorry. Didn't know you were sensitive to minor edits to improve accuracy.
  • Brad
    Brad almost 7 years
    @LordNeckbeard I'm not, but that was more than a minor edit, didn't add anything of substance to what I already wrote, and added some additional opinions from you that would have been attributed to me. (Not that I disagree with you re:bugginess and what not. I just would have preferred you kept the commentary as a comment attributed to you.)
  • Rahul Kadukar
    Rahul Kadukar about 5 years
    @Brad that depends on what the original poster wanted. Putting -ss before -i means the seek happens first and it is a quicker way of arriving at the starting timestamp. However, this may not always produce a frame accurate cut. If you are using ffmpeg 2.1 or greater then -ss before -i will be fast, since -ss is now frame accurate. See this link for more details trac.ffmpeg.org/wiki/Seeking
  • chankruze
    chankruze almost 3 years
    ffmpeg -ss <T0> -i <input file> -t <T1 - T0(duration in seconds)> <out file> or ffmpeg -ss <T0> -i <input file> -t <T0(format 00:00:00) + (T1 - T0)> <out file> for example ffmpeg -ss 00:15:30 -i "a.mp3" -t 25 "b.mp3" or ffmpeg -ss 00:15:30 -i "a.mp3" -t 00:00:25 "b.mp3"