How-to: Clean Up Video Soundtrack using ffmpeg, Audacity and Openshot

Clean Up Video Soundtrack using ffmpeg, Audacity and Openshot
Let's say I have some HD video shot on my Samsung Galaxy SII: for a phone it's a good video camera and ordinarily picks up pretty good sound, but, thanks to the venue (your typical tin-shed sports centre) and the other activities (toddlers on bouncy castle, bless 'em) the sound quality was terrible.

I'm not planning to broadcast this in HD any time soon, but as it's educational material, it would be good if folks could hear what the instructor has to say. Most video editors (certainly on Linux) have limited capability for sound. You can chop it, splice it, remove it - but clean it? No. But it is possible to clean up the sound track using a sound editor.

I believe in using the right tool for the job, so I lifted out and cleaned the audio track in Audacity, then put it back with the original visuals using Openshot.

Note: if you are going to edit any video for length or sequence, either do it before or after you clean the audio. Don't trim either one separately or you'll no longer have sound synced to the pictures!

Lift Out the Dirty Audio First step is to lift the dirty audio track from the source video. Using the venerable ffmpeg, which is a command-line tool, copy the original dirty audio as-is to a lossless .m4a format file.

Note the warning:
This program is only provided for compatibility and will be removed in a future release. Please use avconv instead.

But it's here for now and remains the Swiss-army knife of digital audio-video.

Enter, for example:

ffmpeg -i 20130531_100935.mp4 -vn -acodec copy 20130531_100935_dirty_audio.m4a

which will output:

ffmpeg version 0.8.6-6:0.8.6-0ubuntu0.12.10.1, Copyright (c) 2000-2013 the Libav developers built on Apr 2 2013 17:07:34 with gcc 4.7.2 *** THIS PROGRAM IS DEPRECATED *** This program is only provided for compatibility and will be removed in a future release. Please use avconv instead.

Seems stream 1 codec frame rate differs from container frame rate: 60000.00 (60000/1) -> 1000.00 (1000/1) Input #0, mov,mp4,m4a,3gp,3g2,mj2, from '20130531_100935.mp4': 
major_brand : 3gp4 
minor_version : 768 
compatible_brands: 3gp43gp6 
Duration: 00:03:05.44, start: 0.000000, bitrate: 2858 kb/s Stream #0.0(eng): Audio: aac, 44100 Hz, mono, s16, 60 kb/s Stream #0.1(eng): Video: h264 (Baseline), yuv420p, 720x480, 2793 kb/s, 29.59 fps, 1k tbr, 30k tbn, 60k tbc Output #0, ipod, to '20130531_100935_dirty_audio.m4a': 

Metadata: major_brand : 3gp4 
minor_version : 768 
compatible_brands: 3gp43gp6 
encoder : Lavf53.21.1 Stream #0.0(eng): Audio: libvo_aacenc, 44100 Hz, mono, 60 kb/s 
Stream mapping: Stream #0.0 -> #0.0 
Press ctrl-c to stop encoding size= 1464kB time=185.45 bitrate= 64.7kbits/s 
video:0kB audio:1373kB global headers:0kB muxing overhead 6.596438%

This very quickly produces a separate audio file from the video soundtrack.

Clean Audio in Audacity
Import the .m4a file into Audacity where you can do more or less what you like with it - noise removal, normalise, limiter, equalise - just don't trim or shorten the run length.

At this point, I confess, I could have just imported the video .mp4 file into Audacity directly; the audio editor understands so many digital formats, that it looks inside the video container, uses ffmpeg itself, pulls in the audio track and ignores the rest. I just thought it was worth showing you how ffmpeg does it's thing.

Either way, with a buffered copy of the audio in Audacity, you can go ahead and clean it up.

In my example I ran some noise removal - not too extreme, just good enough was 'just good enough' - ran the hard limiter to cut unwanted peaks, normalised back to -0.3Db, and that was about it.

Happy with the audio quality, I exported it back out in the same lossless format from Audacity.  

Remaster Video with Clean Audio
I fired up a new project in the Openshot video editor then dragged and dropped the original video file and cleaned up audio file into the project files area and from there, dropped each into a new track in the Edit area - making sure the start positions are aligned in the timelign.

Importantly, mute the original audio on the video track - click on the little speaker/volume icon and the sound wave graphic changes to three dashes (muted). I could click the eye icon on the audio track to hide the video - since there isn't any it won't make any difference, but I could use this in Openshot to mix a soundtrack from one file with the visuals and sound of another.

After giving it a quick play in the preview window, checking sound and visuals are still in sync and the sound is still satisfactory, it's a simple process to use the Openshot presets to render the remastered project to a new video file.

Here, you can pretty much use whatever formats Openshot supports to render video as you want it. I'm taking an HD copy to play on the laptop and a standard defintion (DVD PAL widescreen) to load onto the our coaching webpage.

What matters is that we can use GUI tools to front-end all those difficult command-line utilities for transcoding audio and video. I don't need a degree in astrophysics to operate them, either. That, my friends, is progress. RC