[FFmpeg-user] trim and pad audio without reencode

Moritz Barsnick barsnick at gmx.net
Sat Apr 11 16:04:44 EEST 2020


On Wed, Apr 08, 2020 at 14:38:23 +0300, Alexander Machev wrote:
> I want to align the audio stream in the input to has the exact same
> duration as the video stream. So, if audio length is lower than video, fill
> the gap with silence. If audio stream is over length than video, cut the
> audio to fit the video length.
>
> It seems apad and atrim filters can do the job, but they will force
> reencoding of the whole audio stream.
>
> Is it possible to achieve this without reencoding everything ?

For trimming: There's a muxer flag (fflags) "shortest", which should
avoid copying more audio data when using the "copy" codec. A quick test
- I created a file with 15s video and 20s audio - shows that the audio
is trimmed even when using "-c copy".

In tems of adding/padding: That might me possible if you can extract
the audio, concat silent audio to the end, and remux it with the video.
[*] You can create silent audio with ffmpeg (same channel count, same
sample rate), and add it to the extracted audio with the concat
demuxer. I can't think of a way how all this can be done in one step
though.

Cheers,
Moritz
[*] De- and re-muxing may lose audio sync, if I remember correctly.


More information about the ffmpeg-user mailing list