25

I have used Audacity for removing noise from recordings before, however it has very limited command line usage. I have ~100 short lecture videos which I will be watching over the next few months and would like an easy way to clean them all at once or as needed before watching.

Is there a command line tool or popular language library I can use to do this?

15

Take a look at sox

Quoting man sox:

SoX - Sound eXchange, the Swiss Army knife of audio manipulation

[...]

SoX is a command-line audio processing  tool,  particularly  suited  to
making  quick,  simple  edits  and to batch processing.  If you need an
interactive, graphical audio editor, use audacity(1).

So, it should be a nice fit as a companion command line alternative to audaciy!


Regarding the actual task of cleaning recordings, take a look at the filter noisered for which equals the noise reduction filter Audacity:

man sox | less -p 'noisered \['

           [...]
   noisered [profile-file [amount]]
           Reduce noise in the audio signal by profiling and filtering.
           This effect is moderately effective at  removing  consistent
           background  noise such as hiss or hum.  To use it, first run
           SoX with the noiseprof effect on a  section  of  audio  that
           ideally  would  contain silence but in fact contains noise -
           such sections are typically found at the  beginning  or  the
           end  of  a recording.  noiseprof will write out a noise pro‐
           file to profile-file, or to stdout if no profile-file or  if
           `-' is given.  E.g.
              sox speech.wav -n trim 0 1.5 noiseprof speech.noise-profil
           To  actually remove the noise, run SoX again, this time with
           the noisered effect; noisered will reduce noise according to
           a  noise  profile  (which  was generated by noiseprof), from
           profile-file, or from stdin if no profile-file or if `-'  is
           given.  E.g.
              sox speech.wav cleaned.wav noisered speech.noise-profile 0
           How  much  noise  should be removed is specified by amount-a
           number between 0 and 1 with a default of 0.5.   Higher  num‐
           bers will remove more noise but present a greater likelihood
           of removing wanted components of the audio  signal.   Before
           replacing  an  original  recording with a noise-reduced ver‐
           sion, experiment with different amount values  to  find  the
           optimal one for your audio; use headphones to check that you
           are happy with the results, paying particular  attention  to
           quieter sections of the audio.

           On  most systems, the two stages - profiling and reduction -
           can be combined using a pipe, e.g.
              sox noisy.wav -n trim 0 1 noiseprof | play noisy.wav noise
           [...]
  • 5
    Unfortunately I find the description that sox uses in the manpage very noisy to read - pun intended. Is there not a much simpler way, with just one command alone to juggle around with noisereduction? – shevy Oct 16 '15 at 20:15
  • Sure, no problem - just write in one simple sentence what you want to change - but precise enough to be technically clear... Ok, that will not work like this because you need to understand what noise is, and which of the parts of the noise you want to remove, which you may reduce, and what can reduce the sound quality otherwise doing the noise reduction. To juggle around with noise reduction, you need to know your juggling clubs - the kinds of noise to juggle, and how they can be balanced when they collide in mid air. Then, sox is your toolbox and first aid kit! – Volker Siegel Oct 16 '15 at 23:14
10

The accepted answer doesn't give a practical example(see first comment to it) so I am trying to give one here. On Ubuntu with apt you should install sox and audio formats support

sox, how to

First install sox and support for formats (including mp3):

sudo apt install sox libsox-fmt-*

Then before you run your command on the file/files first you need to build a profile, make a sample of noise, this is the most important part you have to select the best time when the noise takes place, make sure you don't have voice (or the music/signal you try to keep) in this sample:

ffmpeg -i source.mp3 -vn -ss 00:00:18 -t 00:00:20 noisesample.wav

Now make a profile out of that source:

sox noisesample.wav -n noiseprof noise_profile_file

And finally run the noise reduction on the file:

sox source.mp3 output.mp3 noisered noise_profile_file 0.31

Where noise_profile_file is the profile and 0.30 is the value. Values goes best somewhere between 0.20 and 0.30, over 0.3 is very agressive, under 0.20 is kind of soft and works well for very noisy audios.

Try and play with that and if you find other setting tricks and please comment with the findings and tuning settings.

how to batch process them

If the noise is similar you can use the same profile for all the mp3s

ls -r -1 *.mp3 | xargs -L1 -I{} sox {}  {}_noise_reduced.mp3  noisered noise_profile_file 0.31

or if there is a folder structure:

tree -fai . | grep -P ".mp3$" | xargs -L1 -I{} sox {}  {}_noise_reduced.mp3  noisered noise_profile_file 0.31
  • 1
    if it's spoken audio and separate left-right channels is unnecessary, then you can append remix - to mix-down of all input channels to mono – Jake Berger Apr 21 at 13:03
  • 1
    @EduardFlorinescu Your answer is spot on. I had a static noise recording on the background of each recording. I read your answer and, used the first 2 seconds from my sound file to create a profile and finally, used it to remove the noise from the recording. Thanks so much for this. – Abrar Hossain May 26 at 21:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.