1

I've an .m4a audio file and wish to take the front 8 secs away and keep the rest of the file intact, then once this first step is done discard the last 8 seconds of the file and keep the rest of the file intact.

So in essence the first 8 seconds will be completley discarded / removed from the file, and the file will start at the 8th second as if the previous 8 seconds never existed.
Similarly the last 8 seconds of the file will be discarded.

Do this all without re-encoding the file.

(EDIT: Other answers on here and elsewhere I have seen, but I could not get to work becasue other answers require the start and end time of the trimmed part to be given. What I need is ffmpeg to provide the end timestamp and start time stamp of the .m4a audio file without having to work this out and feed it into the command)

I have this for trimming the front of the file

ffmpeg -t 00:00:08 -acodec copy -i in_file.m4a out_file.m4a

but nothing for trimming the end of the file.

I can't get what I have to work.

(I've seen some other answers here and elsewhere, but nothing that seems to get me there)

2
  • 1
    Does this answer your question? Trim audio file using start and stop times Commented Mar 15, 2021 at 11:52
  • 1
    @philip-couling the above link does not answer the question becasue it assumes a start and end time within the audio file to trim. I needed a solution that assumes starting at the start, and knows the timestamp for the end of the file so I do not have to work it out and give it to ffmpeg
    – Kes
    Commented Mar 16, 2021 at 19:17

2 Answers 2

2

This will trim the first 8 seconds from the front of the file without re-coding, and retain everything except the first 8 seconds in the output file

ffmpeg -ss 8 -i in_file.m4a -c copy out_file.m4a

In addition, the below line will trim the last 8 seconds from the end of the audio file. Below line seems convoluted but is simplest solution I could see that will trim the end of the file:

ffmpeg -i in_file.m4a -ss 8 -i in_file.m4a -c copy -map 1:0 -map 0 -shortest -f nut - | ffmpeg -y -f nut -i - -map 0 -map -0:0 -c copy out_file.m4a
2

As well as ffmpeg, you could also use SoX which is more dedicated to audio, but you would first need to convert the audio file, to wav or mp3 I think as SoX does not work with m4a.

ffmpeg -i input.m4a output.wav

Then after installing SoX

sudo apt-get install sox
sox input.wav output.wav trim 8 -8

Kind of related, if you just wanted to remove the silent parts you could also use Librosa for this.

For using m4a format with Librosa you need, ffmpeg, and GStreamer install e.g., on Debian/Ubuntu:

sudo apt install libgstreamer1.0-0 

sudo apt install ffmpeg

Then in python you can use:

import librosa 
y, sr = librosa.core.load(m4a_audio_file, sr=16000)
yt, index = librosa.effects.trim(y)
original_duration= librosa.get_duration(y)
new_duration = librosa.get_duration(yt)
1
  • thanks. Nice answer. I looked at sox, and if it supported .m4a format, which it dosen't, I would have used it. I did not want to re-encode.
    – Kes
    Commented Mar 19, 2021 at 13:30

You must log in to answer this question.

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