Learn how to crop an mp3 file with a custom length using the FFMPEG cli.

If you are working with audio files in some project for the public, it's quite probable that you will have to offer a live preview of audio that you want to show to the world, as instead of playing the whole song, just a piece of the track should be available to play in your UI:

FFMPEG create preview from audio file

FFMPEG can be used easily to create such an audio preview from the whole audio with the following command:

ffmpeg -ss <start-point> -t <clip-duration> -i input-file.mp3 -acodec copy output-file.mp3

Where the arguments are the following ones:

  • -ss hh:mm:ss[.xxx] or -ss seconds: seek to the given position of the input file either in seconds or the full-time format.
  • -t hh:mm:ss[.xxx] or -t seconds: limit the duration of the audio that will be transcoded from the input file in seconds or the full-time format.
  • -i input-file.mp3: the absolute or relative path to the input MP3 file from which you want to create the preview.
  • -acodec copy: use the same audio codec of the input file for the output file.
  • As the final positional argument, the output file that will be created as a preview.

In our case, with the given example of the Guns N' Roses song, if we want to extract the guitar solo, the following command should do the trick (420 seconds would make it start at 7:00 and a length of 105 seconds would make it finish at 8:45):

ffmpeg -ss 420 -t 105 -i "./November Rain.mp3" -acodec copy "./guitarsolo.mp3"

Or alternatively, if you want to  use the full-time format:

ffmpeg -ss 00:07:00 -t 00:01:45 -i "November Rain.mp3" -acodec copy ./guitarsolo2.mp3

Note that the value of -t option in full-format doesn't correspond to 8:45 but the duration of the piece of track that we want to extract, in this case, 105 seconds or 1 minute and 45 seconds.

Happy coding ❤️!


Senior Software Engineer at Software Medico. Interested in programming since he was 14 years old, Carlos is a self-taught programmer and founder and author of most of the articles at Our Code World.

Sponsors