Sunday, January 25, 2009

Mencoder v4l2 - Solving the crackling noise in audio when recording from tv card


One of the few things that I could not get right for many months on my Ubuntu Linux installation is the ability to record television programmes from my low end Hauppauge WinTV-PCI-FM card. Playback was excellent using tvtime, in fact it was better than playing back on Windows using the manufacturer's software. However, whenever I tried recording TV using mencoder (that comes with mplayer), an obvious crackling/clicking noise was always present in the audio stream. This was a shame as the video recording offered higher quality than what I could record using Windows. Furthermore, I was able to directly record into any format that mplayer was capable of playing.

The command I used is shown below, it outputs the tv card's composite video and audio into the file, test.avi. Thinking it was something to do with my audio sampling settings, for months I was twiddling with the command used for recording to no avail by adjusting the both the sampling rates (in red below) between 38400Hz, 44100Hz and 48000Hz. In short, I was recording using PAL-BG encoding from the television signal with sound coming from the first alsa sound driver device (adevice=plughw.0,0). Read more about the various options here

$> mencoder -tv norm=PAL-BG:driver=v4l2:width=768:height=576:input=2:alsa:amode=1:adevice=plughw.0,0:audiorate=48000:volume=0:immediatemode=0 -af resample=48000:volume=-5:1 -oac mp3lame -lameopts preset=medium:vol=0:highpassfreq=0:lowpassfreq=0 -ovc lavc -lavcopts vcodec=mpeg4:vbitrate=1200 -vf harddup,crop=720:544:24:16,pp=lb -noskip -ffourcc divx -o test.avi tv://

No matter how many audio filters I applied, I could never remove the crackling noise from the audio stream. Finally, after talking to my dad about how crackling noises may enter recorded audio streams due to too high a sampling rate, I decided to lower the sampling rate from the raw audio input stream differently. Where previously I adjusted the sampling rates between only 38400Hz, 44100Hz and 48000Hz (the only values accepted by the lame audio encoder), I decided to sample the input audio stream at a slightly lower rate and then resample the stream to be acceptable to the lame audio encoder as shown below. The sampling of the audio input is set at 47950 but to be resampled to 48000 for the encoder.

$> mencoder -tv norm=PAL-BG:driver=v4l2:width=768:height=576:input=2:alsa:amode=1:adevice=plughw.0,0:audiorate=47950:volume=0:immediatemode=0 -af resample=48000:volume=-5:1 -oac mp3lame -lameopts preset=medium:vol=0:highpassfreq=0:lowpassfreq=0 -ovc lavc -lavcopts vcodec=mpeg4:vbitrate=1200 -vf harddup,crop=720:544:24:16,pp=lb -noskip -ffourcc divx -o test.avi tv://

To my surprise, that was it. All the irritating crackling noise disappeared! I can now put the command into a bash script for scheduling the recording of my television programmes in better quality and more flexibility than I can on windows.

No comments: