View Full Version : Overlaying two MPEG2 sources


colinb
1st April 2005, 21:51
I have used Avisyth only briefly a couple of years ago, so I'm a bit of a novice. Is it possible to do the following with Avisynth? I'll explain the reason for this afterwards.

I need to open two separate .mpv files and skip forward to a particular hh:mm:ss.ff (a different value for either file). I then need to read a frame from file 1 and file 2, placing the frame from file 1 in a fully-opaque layer on top of the frame from file2. I then want to punch a rectanglar hole in the top layer so that the bottom layer shows through the hole. Then flatten to a single layer and write the resulting frame to an uncompressed video output AVI file. Then take the next frame from both input files and repeat the above for N frames.

The colour space needs to be kept constant throughout all of this.

The resaon why I want to do this is that I have a good quality recording of a DVB-T broadcast but in a couple of places the signal was corrupted leaving about 15 frames with large pixelated areas on those frames.

Fortunatley that program was repeated a few days later, but it was at a much lower bitrate and had a channel logo in the top left corner. The original program did not have any pixelation at the top left.

I therefore thought it would be a good idea to take a frame from the lower quality repeat show and layer it on top of the corresponding corrupted frame from the original show. Then punch a hole in the top layer around the logo which would leave the logo-free area showing through. I could use this "repaired" sequence of frames to replace the damaged ones in the original MPEG file. The 15 frames would be of lower quality because it would have gone through an extra encoding to MPEG2, but it looks a lot more pleasant than the pixelation.

I tried taking bitmap snapshots of each frame and layering them manually in Photoshop and it seemed to work well, but when I inserted the repaired frames into the original file there was a difference in colour saturation compared to the original - I assume that this was because the color space was changed to RGB when the snapshot was taken and then back to the MPEG2 color space again. So I guess I need to keep the whole procedure in the MPEG2 color space throughout.

Then I thought of Avisynth. The above procedure is so mechanical that AviSynth might be an ideal solution.

Is it possible to do this in AviSynth?

Thanks

colinb
1st April 2005, 21:57
Sorry I forgot to mention the video format is PAL 720x576.
I used Tmpgenc to re-encode the repaired frames to MPEG2.

matrix
2nd April 2005, 00:48
Create a mask in paint. A simple white bmp the size of your videos, and put a black rectangle in the area where the logo is.
Then use something like this:

orig=mpeg2source("your file.d2v")
fix=mpeg2source("your repaired file.d2v")
mask=imagereader("C:\mask.bmp")
part1=orig.trim(0,110)
part2=overlay(fix.trim(131,132),orig.trim(111,112),mask=mask,x=0,y=0)
part3=orig.trim(113,230)
part4=overlay(fix.trim(251,252),orig.trim(231,232),mask=mask,x=0,y=0)
part5=orig.trim(233,0)

return part1+part2+part3+part4+part5

In this example, frames 111, 112 and 231, 232 from your original file, are corrupt, and you want to replace them with frames 131, 132, and then 251, 252 from the other file.

edit

Sorry, the mask should be black, with a white rectangle.

Mug Funky
3rd April 2005, 11:14
how about:

clip=mpeg2source("source.d2v")
patch=mpeg2source("fix.d2v").crop(0,0,-60,0) # or whatever distance from the side

clip1 = clip.trim(0,xxxx-1) # xxxx = in-point in frames for your fix
clip2 = clip.trim(xxxx,yyyy) #yyyy = out-point in frames
clip3 = clip.trim(yyyy+1,0) # 0 will automatically go to the last frame

fix = stackhorizontal(patch,clip2.crop(clip2.width-60,0,0,0))

return clip1+fix+clip3


this is completely untested, so let me know if it works :)

btw, why re-encode the fixed frames, rather than keeping them uncompressed, or in a lossless format like huffyuv?

colinb
8th April 2005, 13:01
Thanks for the replies. I only need to extract (and re-encode)the frames covering the pixelation - usually from 5 to 16 frames long, so I used a variation of matrix's script to just return those frames (which of course encoded in tmpgenc in a couple of secs!)

I then cut the original mpeg and insert the newly tmpgenc-rendered fixed frames. So the final mpeg file hold the complete program from the DVB-T broadcast with no pixelation and only a handfull of frames were re-encoded.

Works very well. Thanks again.