fredag den 19. oktober 2018

Careful with audio resampling using FFmpeg

In my line of work transcoding videos for dr.dk/pirattv i use FFmpegextensively. I have written a tool in C# that automates this task and in doing so, i discovered that FFmpeg is not a good choice for downsampling audio. The downsampling does not suffer from aliasing because the signal is properly attenuated at the Nyquist frequency – but the quality of this lowpass filter is terrible. The problem lies with the lowpass filter which is by no means steep enough so it unnecessarily cuts a lot of high frequencies fairly far from the Nyquist frequency. The only good thing about the filter, is that the resampling is really fast. I have not found a setting in FFmpeg that forces it to use a better filter so i wanted to find a better way:
From looking at this awesome site i learned that one of the very best resamplers around: SSRC – is free and even open source! So now i treat the audio separately from the video and do all downsampling using SSRC which preserves the treble which is indeed audible when transcoding music.

Update: I ended up using SoX, because I needed more than just great resampling. SSRC is in theory slightly better but the difference is inaudible to me and SoX provides me with an array of additional possibilities.
Update II – FFmpeg now includes the SoX resampler – but only utilizes it if you tells it to – the default resampler is still the simple one with its limitations.
To use the better resampler from SoX, eg resampling to 44.1 kHz, add the following to your command line:
-af aresample=resampler=soxr -ar 44100

Ingen kommentarer:

Send en kommentar