Oct 10
How to reduce noise in overscan areas (when digitizing analog video)
...without cropping, or resizing, keeping the original SD image size.
While digitizing analogue video, one of the things you'll notice is that a source like VHS introduces quite an amount of noise into your input signal and this noise plays particularly bad in relation to how modern codecs compress video.
Some people thus use a video filter to soften the overall image, bringing down the noise level, which results in better compression ratios. Aside from other bitrate reduction schemes, like horizontal compression, I would like to present yet another method to shave off a few bytes from your resulting video file, even without noticably altering the image.
The keyword here is overscan. VHS for example works on a "canvas" of 720x576 pixels, (standard-def, SD PAL), although effectively only a smaller area of that full frame is used for the image. On captured VHS you usually see this as a jittering black pillar on both sides of the frame, and even on the top and lower part of the image is a small area that is not really useful visual information.
Now, these black areas still contain a lot of unnoticeable noise, noise the compressing codec truthfully tries to capture and finally carries over into the output video. But that area is effectively black, no image information. Keeping this area just as it is would be like having the old letterbox top and bottom bars encoded into the video instead of cropping these off.
Speaking of cropping, you could, of course, simply crop the image, but that renders your video off standards in terms of size, for example 720x576 would become 704x560 which might end up displayed incorrectly on your TV.
Thus, the other option, more an optimization, is to mask the areas with real or "true black", giving the compressing codec less information to work on, what than in turn will result in smaller file sizes.
I've done a small test:
Update: Note that all commands below lack a switch to properly encode the VHS interlaced source video by storing it as interlaced frames in x264 video. You need to tell avconv+libx264 that your source material is interlaced. Read here how to encode interlaced video as x264 video.
$ avconv -i VHS.avi -acodec copy -vcodec libx264 -qscale 4 -aspect 4:3 -t 30 VHSpure.mp4
With this command I transcoded a digitized VHS video to a h.264 version:
Output #0, mp4, to 'VHSpure.mp4':
Metadata:
encoder : Lavf53.21.1
Stream #0.0: Video: libx264, yuv420p, 540x576 [PAR 64:45 DAR 4:3], q=-1--1, 25 tbn, 25 tbc
Stream #0.1: Audio: libmp3lame, 44100 Hz, stereo, 146 kb/s
Stream mapping:
Stream #0:0 -> #0:0 (ffvhuff -> libx264)
Stream #0:1 -> #0:1 (copy)
Press ctrl-c to stop encoding
frame= 749 fps= 22 q=-1.0 Lsize= 4866kB time=29.92 bitrate=1332.3kbits/s dup=1 drop=0
Then, I compared this "pure" transcode with two "optimized" transcodes. One, a video where I masked off the sides with real black. (I left the top/bottom of the image alone for now). The command I used was
$ avconv -i VHS.avi -vf "drawbox=0:0:4:576:black@1, drawbox=536:0:4:576:black@1" -acodec copy -vcodec libx264 -qscale 4 -aspect 4:3 -t 30 VHSbar4.mp4
Output #0, mp4, to 'VHSbar4.mp4':
Metadata:
encoder : Lavf53.21.1
Stream #0.0: Video: libx264, yuv420p, 540x576 [PAR 64:45 DAR 4:3], q=-1--1, 25 tbn, 25 tbc
Stream #0.1: Audio: libmp3lame, 44100 Hz, stereo, 146 kb/s
Stream mapping:
Stream #0:0 -> #0:0 (ffvhuff -> libx264)
Stream #0:1 -> #0:1 (copy)
Press ctrl-c to stop encoding
frame= 749 fps= 22 q=-1.0 Lsize= 4840kB time=29.92 bitrate=1325.2kbits/s dup=1 drop=0
See? I did 4 pixels of masking on each side. The drawbox video-filter usually draws boxes without filling them, but with such low pixel widths, the box effectively is filled. You can check what I did, by using the command above but replacing the colour with something more visible, like "red". Note that I used 4 pixels, as most codecs work with sampling areas following the power of 4 - 4, 8, 16, etc.
To test if that makes a difference, and to further mask the actual black area on the sides of my video, I increased this to 5 for another transcode:
$ avconv -i VHS.avi -vf "drawbox=0:0:5:576:black@1, drawbox=535:0:5:576:black@1" -acodec copy -vcodec libx264 -qscale 4 -aspect 4:3 -t 30 VHSbar5.mp4
Output #0, mp4, to 'VHSbar5.mp4':
Metadata:
encoder : Lavf53.21.1
Stream #0.0: Video: libx264, yuv420p, 540x576 [PAR 64:45 DAR 4:3], q=-1--1, 25 tbn, 25 tbc
Stream #0.1: Audio: libmp3lame, 44100 Hz, stereo, 146 kb/s
Stream mapping:
Stream #0:0 -> #0:0 (ffvhuff -> libx264)
Stream #0:1 -> #0:1 (copy)
Press ctrl-c to stop encoding
frame= 749 fps= 22 q=-1.0 Lsize= 4825kB time=29.92 bitrate=1321.1kbits/s dup=1 drop=0
Now for the results:
$ ls -al VHS*
-rw-rw-r-- 1 user user 4982638 Sep 24 20:00 K98pure.mp4
-rw-rw-r-- 1 user user 4956169 Sep 24 20:00 K98bar4.mp4
-rw-rw-r-- 1 user user 4940750 Sep 24 20:00 K98bar5.mp4
You see, the masking with black resulted in a video video 41888bytes, roughly 42Kb smaller than the "pure" transcode, on a 30 second clip.
Another run on different input material gave these numbers:
-rw-rw-r-- 1 user user 11061420 Sep 24 22:00 VHSpure.mp4
-rw-rw-r-- 1 user user 11047816 Sep 24 22:00 VHSbar4.mp4
-rw-rw-r-- 1 user user 11041744 Sep 24 22:00 VHSbar5.mp4
And with 5 pixels on each side as your mask would never "touch" the image area, which begins at ~7-8 pixels from the frame's edge.
And that is without throwing away visual information. So this masking trick is an effective way of optimizing your digitized video from analogue sources.
Another example command:
masking the lower part off as well, and doing a slight horizontal squeeze. It's debatable if it makes sense to compress VHS material by using horizontal squeezing. I usually opt against it with more important video, while I use it to reduce file size with less precious video material. Also note that this command (as already noted above) does not carry over the "vertical compression" (interlaced-ness) of the source video. You need to tell libx264 to use the ildct mode ("interlaced DCT"), read about interlaced x264 here and add "-flags +ilme+ildct" below:
$ avconv -i VHS.avi -vf "scale=540:576, drawbox=0:0:4:576:black@1, drawbox=536:0:4:576:black@1, drawbox=0:572:540:4:black@1, drawbox=0:568:540:4:black@1" -vcodec libx264 -qscale 4 -acodec copy -aspect 4:3 VHS.mp4