0

I have a few video files which have "humm" sound in the audio. So, I created the following script for batch processing. I am using ffmpeg, to extract audio to .mp3, and sox to denoise, which will output noise free mp3 file.

enter image description here

mkdir -p ./tmp;
for f in *.mp4;
do
    title=${f%.mp4};
    echo "Working on $f";
    mv ./"$f" ./tmp/ && ffmpeg -i ./tmp/"$f" -f mp3 -ab 128000 -vn ./tmp/"$title"_noise.mp3;

    echo "Sox process started...";
    sox -v 0.80 ./tmp/"$title"_noise.mp3 ./tmp/"$title"_128.mp3 noisered ./noise_profile 0.20 && sox ./tmp/"$title"_128.$mp3 -C 96 ./$title.mp3;

    echo "Removing Audio from video file...";
    ffmpeg -loglevel warning -stats -y -i ./tmp/"$f" -c copy -an ./"$f";
done

But the problem is, I observed that there is 00:00:00.050s (HH:MM:SS.ms - Checked with Audacity) of delay added to the final mp3 file. I believe Sox is adding this delay.

  1. How can I solve this? Or, how can I remove/trim 00.050 seconds from the beginning and add 00.050 seconds of silence at the end of the audio track? OR
  2. Is there any other better way to finish my task?

NOTE: I am trying to work with .wav instead of .mp3 after reading below reply, if it works I will update here.)

2 Answers 2

1

This might be down to the MP3 (MPEG-1 Audio Layer III) standard:

https://lame.sourceforge.io/tech-FAQ.txt

Read the section: "Why does LAME add silence to the beginning each song?"

1
  • Thank you. I understood what happend here. I am trying to work with .wav instead of .mp3 after reading your reply, if it works I will update here. Commented Jul 7, 2020 at 2:45
0

I found Answers for my own questions. There are 2 ways to solve this. Answer for my first question is...

1. How can I solve this? Or, how can I remove/trim 00.050 seconds from the beginning and add 00.050 seconds of silence at the end of the audio track?

Answer 1: This might be destructive as I am converting mp3 multiple times. Still if someone wants to know. Use below command to trim the beginning and use pad to add silence at the end.

sox Speech_01.mp3 Speech_01_Corrected.mp3 trim 0.049 pad 0 0.050

trim 0.049 will remove 049ms of silence from the beginning. pad 0 0.050 will add 050ms of silence at the end.


Answer 2: This is what solved my problem. I followed instructions from this page, and pieced together a batch script.

2. Is there any other better way to finish my task?

As @Artem S. Tashkinov pointed out, LAME adds delay because of sample differences between .wav and .mp3 formats. (That is what I understood). So, I used only .wav as mentioned in this page.

NOTE: Below sox command was deleting a small part from the end of the audio file, for some unknown reason. So, I had to use pad to add some silence at the end of the audio, and avoid loosing any part of the audio accidentally. Through trial and error I came to know that 0.010ms is enough for my case.

# You can observe pad here, which is adding silence at the end.
sox ./tmp/"$title"_noisy.wav ./tmp/"$title"_noisy_s.wav pad 0 0.010

# Below command will delete small part from the end of output wav file,
# Hence the above command.
sox -v 0.80 ./tmp/"$title"_noisy_s.wav ./"$title".wav noisered ./allnoise_profile 0.30

Collecting noises to generate a noise profile.

In my case, there were different variations of "hmm" noise in my files. As a result selecting just one noise sample, for all files, was not working for me. Hence I copied variations which were escaping and created one .wav file (allnoise_samples.wav), using Audacity. Note: Having silence between variations didn't work for me. So, samples are continuous.

enter image description here

# Use below command to generate a noise sample file.
sox ./allnoise_samples.wav -n noiseprof ./allnoise_profile

Here is the final script for batch processing.

Working with .wav files is heavy on storage. So, I am deleting them as soon as their purpose is fulfilled.

mkdir -p ./tmp;
for f in *.mp4;
do
    title=${f%.mp4};
    echo -e "\n\n$f - Splitting Audio and video files...";
    mv ./"$f" ./tmp/"$title"_O.mp4 && ffmpeg -i ./tmp/"$title"_O.mp4 -c:a pcm_s16le -ar 128k -vn ./tmp/"$title"_noisy.wav;
    ffmpeg -i ./tmp/"$title"_O.mp4 -c copy -an ./"$title"_v.mp4;

    echo "Adding Silence at the end...";
    sox ./tmp/"$title"_noisy.wav ./tmp/"$title"_noisy_s.wav pad 0 0.010 && rm -rf ./tmp/"$title"_noisy.wav;

    echo "Sox process started...";
    sox -v 0.80 ./tmp/"$title"_noisy_s.wav ./"$title".wav noisered ./allnoise_profile 0.30 && rm -rf ./tmp/"$title"_noisy_s.wav;

    echo -e "Sox finished... \nMerging Audio and video files...";
    ffmpeg -loglevel warning -stats -y -i ./"$title"_v.mp4 -i ./"$title".wav -map 0:v -map 1:a -c:v copy -c:a aac -b:a 96k ./"$f";
    rm -rf ./"$title"_v.mp4 ./"$title".wav;
done

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.