Log in

View Full Version : Swap all neighbour frames


nji
31st July 2025, 16:19
Hello.
The title says it all ... :)
I need to swap all neighboured frames of a clip.
(Yes, it's because someone managed to deinterlace a clip very imaginate :) ).

In principle it must be very easy.
But as there's no "for" in avisynth, I can't simply iterate and swap the frames.

I hope someone knows...

Selur
31st July 2025, 16:23
even = SelectEven()
odd = SelectOdd()
Interleave(odd, even)

would swap even and odd frames,.....
(if someone mixed up the frame order during deinterlacing using this might fix the general motion, but you would probably still have flickering,...)

nji
31st July 2025, 17:12
Oh... that was an easy one...

Actually I didn't find info about how to do that.
And if you ask a so-called AI, it proposes things like for, Seq(), Shuffle()...

However the result isn't very satisfying.
Not because of a flickering (due to interpolation effects of the codec I assume?), but as the "jumping" of the frames still persists for 1s, then 1s OK, then 1s jumping again etc.
There must be something real strange with the video.
Probably a defective device...

However thank you; I learned something :)

EDIT
I identified the cause for the strange effect after twisting neighboured frames:
In the orig clip the twisted pairs aren't regulary... every about 1s there
is an un-twisted single frame.
If I do the twisting on a regulary basis at this points the result is even worse,
until the next single frame.
The best result so far is to remove every second frame ( i.e. drop contents),
and maybe insert (invented) interpolated frames at last.
Not nice, but the best idea I came to.

Z2697
1st August 2025, 04:16
Maybe you can upload a sample?

nji
1st August 2025, 10:09
I wouldn't want to... as it is a clip from a personal family feast,
and I don't want to make it public without permission.

But actual it's unnecessary as I did the investigation already.

The movie (29.97 fps) consists of pairs of twisted frames.
Abt. ever second, there is only a single frame (can't tell so far
if it's a consecutive frame, or a duplicate frame).
Then the next second twisted pairs again etc.

EDIT
The optimal solution would be to have a parameter for the de-swap
to swap n pairs, then skip one frame, then swap n pairs etc.
... which is surely beyond my AVS capabilities... :)

Selur
1st August 2025, 13:41
If you find a pattern, you could use SelectEvery
https://avisynthplus.readthedocs.io/en/latest/avisynthdoc/corefilters/selectevery.html

johnmeyer
1st August 2025, 15:14
I wouldn't want to... as it is a clip from a personal family feast,
and I don't want to make it public without permission.
EDIT
The optimal solution would be to have a parameter for the de-swap
to swap n pairs, then skip one frame, then swap n pairs etc.
... which is surely beyond my AVS capabilities... :)I've done a lot of work swapping fields and frames. However, without a clip, I can't help.

As for being beyond AVISynth, what you describe is exactly what AVISynth does well.

StainlessS
1st August 2025, 15:32
For not really well suited for processing anything but short clips. (those that can fit uncompressed in memory entirely).

Even a 10 - 15 second clip would be helpful in providing solution (best with motion thoughout, the longer the better).

But, something like this might work,


Colorbars.KillAudio.ShowFrameNumber # numbers down RHS show original frame number.

F30 = SelectEvery(3,0)
F31 = SelectEvery(3,1)
F32 = SelectEvery(3,2)

Interleave(F31,F30,F32) # Swap F30 and F31, leave F32 alone

ScriptClip("""Subtitle(String(current_frame))""") # Show output Frame Number @ top LHS.
Return last


Without example clip, everybody is shooting in the dark.

EDIT: Old thread from 2009 using long lists of SelectEvery()
24p->25p woes - "Euro Pulldown"? :- https://forum.doom9.org/showthread.php?t=151372

EDIT: And this may be of interest {Scharfis_Brain Exotic Interlacing}
Here an explanation of Phase Shifted (AKA Perverse) Telecine [click a 2nd time for magnified image].
https://i.postimg.cc/14MJpkkm/Perverse-TC.jpg (https://postimg.cc/14MJpkkm)

Taken from Scharfis_Brain Exotic Interlacing [originally in German],
see DATA Folder @ MediaFire in my sig below this post for PDF's in English or German.
[There is also a zip containing Web page]

EDIT: The Website is no longer online, but LigH posted a link to his saved copy somewhere.

EDIT: Original web page on WayBack Machine:- https://web.archive.org/web/20140411222900/http://home.arcor.de/scharfis_brain/ExotischesInterlacing/

And a thread I started on it (forgot about it) some time ago:- https://forum.doom9.org/showthread.php?t=176104&highlight=Exotic+Interlacing

EDIT: LigH hosted original web page in German:- https://www.ligh.de/scharfis_brain/ExotischesInterlacing/

nji
1st August 2025, 16:12
I've done a lot of work swapping fields and frames. However, without a clip, I can't help.

I already posted why I can't post the clip in this case.
Moreover I don't understand why the clip should be of more help
than the precise mathematical description of the pattern I already posted.
???


As for being beyond AVISynth, what you describe is exactly what AVISynth does well.
OK, I'm no native speaker... (actually I AM, but not in English :) ),
but I've the impression that it's you that missed what I wrote about
"beyond AVISynth"...

nji
1st August 2025, 16:52
...

But, something like this might work,


Colorbars.KillAudio.ShowFrameNumber # numbers down RHS show original frame number.

F30 = SelectEvery(3,0)
F31 = SelectEvery(3,1)
F32 = SelectEvery(3,2)

Interleave(F31,F30,F32) # Swap F30 and F31, leave F32 alone

ScriptClip("""Subtitle(String(current_frame))""") # Show output Frame Number @ top LHS.
Return last

...


Gosh - that's unexpected simple!

So for n twisted frame pairs and then one frame as-is it would be:

F2n.0 = SelectEvery(2n,0)
F2n.1 = SelectEvery(2n,1)
F2n.2 = SelectEvery(2n,2)
F2n.3 = SelectEvery(2n,3)
...
F2n.2n-3 = SelectEvery(2n,2n-3)
F2n.2n-2 = SelectEvery(2n,2n-2)
F2n.2n-1 = SelectEvery(2n,2n-1) # the very last, leave alone

Interleave(F2n.1,F2n.0,...,F2n.2n-2,F2n.2n-3, F2n.2n-1) # Swap n pairs, leave F2n.2n-1 alone

Right?

Probably no first price for elegancy... :)

However I just noticed that the above replacement scheme may be shifted in the clip.
I.e. it all begins with p twisted pairs, then one leave-alone frame, then as above.
EDIT:
In theory it could even start "in the middle" of one of the beginning p pairs...

StainlessS
1st August 2025, 17:31
However I just noticed that the above replacement scheme may be shifted in the clip.

I guess that you could prepend some dummy frames at start of clip, so as to align.

Avisource("...")
Offset = ??? # some number
Prepend = Last.Blankclip(length=Offset)
Last = PrePend ++ Last
# ...
# SelectEvery etc
# ...
Trim(Offset,0) # chop off original prepended blankclip


EDIT:
In theory it could even start "in the middle" of one of the beginning p pairs...
Yeah, that would be even more awkward. [fix it later, somehow]

nji
1st August 2025, 17:46
Metal man... you're my hero! :)

Thank you one more time, also for the provided additional material.

wonkey_monkey
1st August 2025, 19:09
I already posted why I can't post the clip in this case.
Moreover I don't understand why the clip should be of more help
than the precise mathematical description of the pattern I already posted.
???

This:

Abt. ever second, there is only a single frame (can't tell so far
if it's a consecutive frame, or a duplicate frame).

is nowhere near a "precise mathematical description."

nji
1st August 2025, 20:02
For the general alg the actual values are anything but significant.

But I've got the impression you just want to be right...

StainlessS
2nd August 2025, 13:16
But I've got the impression you just want to be right...
Nope, loose problem description just results in confusion about what the problem is and hence what a viable solution might be.
Facts matter.
You can't provide 10 seconds of motion without any people in shot ?

Selur
2nd August 2025, 20:50
If you want a programming language, use Vapousynth which is build on Python,.....

RetsimLegin
3rd August 2025, 08:43
This is what I'd try. Caveat: I have neither interest nor expertise in scripting and don't use "raw" avisynth at all. This uses gui apps. If that's somewhere you have no wish to go, then don't read the rest.

Step 1. Speed the video up so it runs 2x faster while keeping the fps unchanged i.e. skips alternate frames completely. (I'd use avidemux).
Step 2. Double the fps using an AI Based frame rate duplicator. I have WinxAI for this purpose. That will produce a video that still runs fast but with the missing frames regenerated - in my experience remarkably well.
Step 3. Slow the video down by halving the fps, which will double its duration back to original. (avidemux)

Can't say how well this would cope with your footage, without trying it.

nji
3rd August 2025, 10:13
Yes, I described that at the end of a post above (https://forum.doom9.org/showthread.php?p=2021314#post2021314).

... and actually this is the best result I got so far
(although my hearts bleeds... having to drop valid contents...).

So why don't the SelectEvery(), Trim() algs do?

I identified the actual values of p = 0, and n = 7.
The "to-leave-frame" is not at the end of the pattern, but at the start.
This does well for few seconds, but then it goes "out of sync".
Obviously the pattern in the orig movie isn't regulary,
but consists of jumps and duplicates (which is known of cheap
devices and bad transformations).
Nevertheless I tried manually to identify a kind of "complex pattern"...
but no chance!
On top of the "scattered pattern" the shaking of the device is superimposed.
The log of the analyzer of a stabilizing run is of no help either.

However, the movie can be made viewable at least by decimating by 2,
and reinventing the removed frames.

Thank you for all the helpful posts! :)

Hopeful the thread is of general use...

wonkey_monkey
3rd August 2025, 12:12
Double the fps using an AI Based frame rate duplicator.

This is a terrible idea. The video already has all the frames; just not (necessarily) in the right order.

Why would you throw out half of the perfectly good frames and replace them with guesswork?

RetsimLegin
3rd August 2025, 13:32
Because it would work and produce a perfectly watchable result - likely more watchable than as it stands. If the existing swap pattern is to any degree random (as it appears to be), then it's going to be nigh-on impossible to restore it other than frame by frame, manually.

The AI tool I use "invents" frames that are all but indistinguishable from the real thing. It's not guesswork; it's interpolation of before and after, but not blended or smeared; it's a photo-quality re-drawing of the various objects with proper outlines and proper motion.

It's only a terrible idea if the outcome is bad. When I've used it, it isn't. Not at all.

Example from something I'm working on right now. This source had a duplicated frame and a skipped frame (meaning a jerk in motion on playback). Here are 4 consecutive frames, one of which is fabricated. Find it.

https://drive.google.com/file/d/1bC1jC4jFbM5JJbfa09mOM6JYfcpcVAVa/view?usp=sharing

and another

https://drive.google.com/file/d/1YfC46sij4SFLXkGeAace_KNivqqu5Ejc/view?usp=sharing

nji
3rd August 2025, 15:34
I don't like too much repeating myself, but that's exacty the way I achieved acceptable results.
Still I don't need that AI thing, but used one of the very good interpolation algs from AVS.

RetsimLegin
3rd August 2025, 16:16
@nji Sure. I was reacting to the "terrible idea" comment above mine. As you have presumably found out - it's not necessarily so.