Deinterlacing MPEG2 files.
April 17, 2009 6:19 PM   Subscribe

How can I deinterlace the MPEG files I have from my PVR?

I use mpeg streamclip to grab MPEG2 transport streams from my PVR box and trim and demux them. Then I use mplex to remux them, into a nice MPEG that XBMC (and just about everything else) can play.

The problem I have is that the files remain interlaced, and gives that icky sawtooth look on any serious horizontal motion. 2.6Mb example file here. Mostly it's ignorable, but surely this is fixable. Can anyone suggest a way to do this - ideally with free (as in beer) software, or software I already have, I have macOS and linux machines available - but if this'll take any significant processing power a mac option would be preferred.
posted by pompomtom to Computers & Internet (11 answers total)
I'll take a look at your sample. Are you set on any particular output format?
posted by Inspector.Gadget at 6:21 PM on April 17, 2009

Ta. I'm not especially worried about format, as long as XBMC (on a modded original xbox) and VLC will play them, and they look good, I'm happy. Interlacing aside, the format they're in is good because chucking them on to DVDs for friends and family is a piece of piss.
posted by pompomtom at 6:28 PM on April 17, 2009

Avidemux runs on Mac and Linux with a QT GUI, and will work well for this.

First, open your mpeg video. It will be - assuming the sample, which contains some interlaced credits but a progressive picture-in-picture effect, is a good representative - PAL 25 fps, 720x576 scaled on playback to 1024x576, with about 16 pixels of black bars on each side. The audio will be MPEG-1 Layer II (MP2) audio, and Avidemux correctly (!) detects the delay (small favors, etc.). To get this to be progressive, do the following:

1. Set a video output codec. Doesn't matter what for now, we do this only to get access to the filters menu.

2. Open video filters. Browse around a bit to get to know where the relatively few filters are.

3. Chain two up in this order:
a. First (top) Crop 16 pixels from both the left and right sides. If you're worried about slightly overcropping, do 8 from each, but I'll assume 16. This is important, as you need mod16 video. Don't crop if you want MPEG-2 output.

b. Second (bottom) add the YADIF deinterlacing filter: Top Field First, Spatial and Temporal check (no bob or skip bob, forget which): If this goes well, you should get or be able to open a preview window showing 688x576 video that is encoded progressively (test by using the seek bar). Close that window. Deinterlace for all output formats, including MPEG-2 (where it will be the only filter).

3. You have an optional step here. If XBMC supports anamorphic video other than MPEG-2, skip it. Players that support non-anamorphic video only will display this file incorrectly, so to work around this you can resize in Avidemux to 896x576 - the full 16:9 dimensions of the original, minus ~16 pixels cropped from each side. Make sure to use Mplayer resize and Lanczos3 to avoid (most) ugly artifacts. Don't resize if you want MPEG-2 output. If you do resize, do this as the last (bottom most) filter.

4. OK, so you have properly filtered video. You can either copy the audio or recode it to mp3. I recommend one of the following combinations of audio and video:
  • Xvid and mp3 in an AVI, MKV, or MP4 container. Set pixel aspect ratio in encoder config to PAL 16:9 unless you resized as a filter, in which case leave it alone.
  • Xvid and original mp2 in an AVI container. Set pixel aspect ratio in encoder config to PAL 16:9 unless you resized as a filter, in which case leave it alone.
  • x264 and mp3 in an MP4 or MKV container. Set pixel aspect ratio in encoder config to PAL 16:9 unless you resized as a filter, in which case leave it alone
  • x264 and original mp2 in a MKV container. Set pixel aspect ratio in encoder config to PAL 16:9 unless you resized as a filter, in which case leave it alone.
  • DVD (mpeg2enc) and original mp2 audio in an MPEG-PS (A+V) container, which is just an mpeg. * Don't resize or crop in this case, just deinterlace, you already have DVD-compliant streams and you want to basically duplicate them except for interlacing. Set 16:9 flag in encoder; it's a different looking interface than above and doesn't have a PAL designation.
5. Pick a bitrate and appropriate level of complexity; this is stuff that'll take a quick google to get the hang of.

6. Avidemux has already detected the audio delay of this file correctly, and assuming it continues to work in this way, don't worry about setting an audio delay manually unless you notice A/V sync problems.

7. File -> Save -> Video..., pick an output, and let the encoder do it's thing. Make sure to set an appropriate format in the container box before you do this. For multiple jobs, play around with the job queue so you can set up a bunch and run them while you sleep.
posted by Inspector.Gadget at 7:09 PM on April 17, 2009

Get avidemux at or from your distro's repos, by the way. Make sure to use the QT interface, as the GTK+ one is falling out of date.

Also, if after your first encode, you notice that some blacks are too gray, you can use ColorYUV (Levels TV -> PC) as your very first Avidemux filter; I'm not sure what XBMC does internally or anything about your display so you'll need to judge this by eye. If it looks OK, don't worry about it.
posted by Inspector.Gadget at 7:12 PM on April 17, 2009

Oh, final note: You can crop horizontally before deinterlacing, but never crop vertically or resize at all before doing so, as that will ruin the field lines and make deinterlacing impossible. Normally I would have cropped after deinterlacing but here because it's only horizontal it'll save YADIF some search time and may speed up your encode.
posted by Inspector.Gadget at 7:14 PM on April 17, 2009

You're aware that VLC has real-time deinterlacing, right? It's a much better option than permanently altering your video, IMO.
posted by archagon at 8:05 PM on April 17, 2009

Deinterlacing it before playback (that is, re-encoding it) will cause the same distortion on high motion scenes that you get playing it back to a progressive scan. For that reason, it's generally recommended you de-interlace at playback. VLC can do this, it's in the Video menu. The rationale is future playback devices may handle deinterlacing for you, so you shouldn't bother with re-encoding (a permanent loss of quality).

If you really want to re-encode it and keep it simple, use ffmpeg (you can install it from MacPorts).

$ ffmpeg -i source.mpeg -deinterlace new.avi

You'll probably need to adjust some settings to get the quality you want from the re-encode. The manpage for ffmpeg is straightforward.
posted by cj_ at 8:08 PM on April 17, 2009

Maybe you could try HandBrake - it has built in deinterlacing options, and recent versions can encode from a wide range of sources.
posted by fearthehat at 8:47 PM on April 17, 2009

If you can put them on a DVD which doesn't have these artifacts, without re-encoding, then something is off in your playback, or mplex isn't properly signalling the interlacing.
posted by Pronoiac at 2:35 PM on April 18, 2009

XBMC doesn't handle deinterlacing on the fly? VLC does (Bob and Weave) and it works pretty well for ordinary MPEG-2 streams, in my opinion anyway.

But nthing avidemux if you want them playable on "whatever."
posted by ostranenie at 8:45 PM on April 18, 2009

Jes' deinterlacer for Mac sounds like the easy way out: Via Apple
posted by KimG at 1:53 PM on April 19, 2009

« Older Ahh, the price you pay for individuality...   |   In Dublin for the longest day... Newer »
This thread is closed to new comments.