Log in

View Full Version : Black out video for a range of frames


TCmullet
27th April 2023, 22:36
I wrote this years ago when I "knew" Avisynth. Goal is to blank out a range of frames without touching the audio. It's had problems when the range was at the beginning of the file. Over the years, I chickened out and simply avoided the beginning of the file. I now can't avoid it.
Can someone figure out what's wrong with this code?

function BlackOver(clip c, int fstart, int fend)
{
# Accepts fend=0, to last frame
fend = (fend==0) ? c.FrameCount-1 : fend
before= (fstart>0) ? c.Trim(0, -fstart) : c.BlankClip(Length=0)
current=c.BlankClip(Length=fend-fstart+1)
after=(fend+1<c.FrameCount) ? c.trim(fend + 1, 0) : c.Blankclip(Length=0)
return audiodubex(before++current++after, c) # No error if no audio
}

StainlessS
28th April 2023, 06:41
function BlackOver(clip c, int fstart, int fend)
{
# Accepts fend=0, to last frame
fend = (fend==0) ? c.FrameCount-1 : fend
before= (fstart>0) ? c.Trim(0, -fstart) : c.BlankClip(Length=0)
current=c.BlankClip(Length=fend-fstart+1)
after=(fend+1<c.FrameCount) ? c.trim(fend + 1, 0) : c.Blankclip(Length=0)
return audiodubex(before++current++after, c) # No error if no audio
}


Warning, as per recent posts in Avisynth+ thread in devs forum,
c.BlankClip(Length=0)
may or may not be made illegal. (Yeah, I know, I dont like it either)

I've knocked this up without using Length=0 thingy

Function BlackenRange(clip c,Int "S", Int "E") {
/*
By Stainless @ Doom9 :- https://forum.doom9.org/showthread.php?p=1986352#post1986352

Make c clip range Black, frames S to E.
Start and End Args S & E, are similar but not exactly like trim.
S is silently limited to Range 0 -> FrameCount-1.
-ve E interpreted as a Frame Count of Abs(E) [Just like in trim]
S Defaults to 0, E defaults to -1 (ie 1 frame only)
Examples:
BlackenRange(c) # Frame 0 Only (S=0 and E=-1, both defaulted)
BlackenRange(c, 0,0) # Entire clip
BlackenRange(c, 10,0) # Frame 10 to End of Clip
BlackenRange(c, 0,-1) # Frame 0 Only (as for S and E both defaulted)
BlackenRange(c, 1,1) # Frame 1 Only
BlackenRange(c, 1,-1) # Frame 1 Only
BlackenRange(c, 1) # Frame 1 Only [Not same as Trim()], E defaults to -1 ie single frame.
BlackenRange(c, 1,-3) # Frames 1 to 3 (ie 3 frames)
BlackenRange(c, 10,20) # 11 Frames, 10 to 20
BlackenRange(c, 10,-5) # Frames 10 to 14 ie 5 frames
BlackenRange(c, 0,3) # Frames 0 to 3 ie 4 frames
BlackenRange(c, 0,-4) # Frames 0 to 3 ie 4 frames
*/
FMX = c.FrameCount-1
S = Default(S,0).Max(0).Min(FMX)
E = Default(E,-1)
E = (E==0) ? FMX : E
E = Min(((E < 0) ? S-E-1 : E),FMX)
Len = E-S+1
Assert(Len>0,"BlackenRange: Bad Range Length, E<S "+String(Len,"{Len=%.0f}"))
# 0 <= S <= E <= FMX : E is +ve END Frame number (may be 0, where S will also be 0)
cK = c.BlankClip(Length = Len) # Blackened Range
Result = (S==0) ? ck : c.Trim(0,-S) ++ cK # Before + Blackened
Result = (E==FMX) ? Result : Result ++ c.Trim(E+1,0) # Before + Blackened + After
Return Result.audiodubex(c) # No error if no audio
}


Tester

Colorbars.Trim(0,-100).KillAudio

Return BlackenRange() # Default (0,-1) ie frame 0 only
Return BlackenRange(0,0) # Entire clip
Return BlackenRange(10,0) # Frame 10 to End of Clip
Return BlackenRange(0,-1) # Frame 0 Only (as for S and E both defaulted)
Return BlackenRange(1,1) # Frame 1 Only
Return BlackenRange(1,-1) # Frame 1 Only
Return BlackenRange(1) # Frame 1 Only [Not same as Trim()], E defaults to -1 ie single frame.
Return BlackenRange(1,-3) # Frames 1 to 3 (ie 3 frames)
Return BlackenRange(10,20) # 11 Frames, 10 to 20
Return BlackenRange(10,-5) # Frames 10 to 14 ie 5 frames

Return BlackenRange(0,3) # Frames 0 to 3 ie 4 frames
Return BlackenRange(0,-4) # Frames 0 to 3 ie 4 frames


# Bad Len Error Checks, E can be -ve [Abs(E) is a frame count], BUT 0 <= E < S is an error [End precedes Start].
Return BlackenRange(20,19) # Len=0
Return BlackenRange(20,10) # Len=-9


Tested ony with supplied examples.

StainlessS
28th April 2023, 14:07
Updated, Added stuff in Blue.

TCmullet
28th April 2023, 14:13
Stainless, THANKS! This is a total rewrite it appears. Could you please add the one example that I need the most, which you didn't do? Delete 0 to 3.

The problem that that example causes me is that (and this has always been strange), is that the frames after the blackened frames get screwed up and do not play 1 to 1, but duplicate some and drop some, all within a a several frame range. This is when I'm using ffvideosource to open the file. It may or may not happen with DGSource/DGIndexNV. But it should work no matter what I use to open the file. My Blackover is somehow screwing up the frame sequencing at the start of the file. I'm wanting to blank out the last frames RIGHT before the program-proper starts.

If you try to black out frames 0 to 3 and it works for you, I'll clone your code and give it a whirl.

StainlessS
28th April 2023, 14:23
Delete 0 to 3
Not sure that I really get that ?
Aint nuttin about deleting stuff, only blacken.

If you are having problems with it, then it is your VideoSource filter that is the problem (eg DirectShowSource is terribly frame inaccurate).

The test examples show that there is no problem with provided script function.
(unless you show an example with Colorbars as I've used where it does screw up)

EDIT: Am leaving house soon.

StainlessS
28th April 2023, 14:39
Added TWO examples for blacken 0 -> 3, In Purple.

Added comment in BOLD Blue.

TCmullet
28th April 2023, 16:27
If you are having problems with it, then it is your VideoSource filter that is the problem (eg DirectShowSource is terribly frame inaccurate).
Yes, I realize that. However, I'm running 2 functions against several seconds leading into the beginning of the desired footage. One is my Blackover. The other is a function over a similar frame range to mute the sound. Its logic is extremely similar to my Blackover. I ran a couple of short runs taking each one out. In the one where I took Blackover out, the ffvideosource worked perfectly. Therefore I concluded there's something I'm doing in my Blackover function that is screwing around with the Avisynth internals and "breaking" it. OR my logic is flawed.

This isn't a super-urgent project. The black space between this broadcast and the prior content is so short that there was no keyframe, therefore I need to include the prior stuff then blank out the video and audio so I can have (1) a comfortable lead-in of black silence, and (2) I'd give ffvideosource enough frames for it to stabilize itself. But this bug causes there to never be "enough frames" no matter how much I give it. (I hope this is clear.)

StainlessS
28th April 2023, 20:12
I cant do anything to fix somebody elses source filter, but
if all of your sources are mp4, then I can suggest that LSmashVideoSource for mp4, is frame accurate. (leastwise, in my experience)

TCmullet
28th April 2023, 21:30
I cant do anything to fix somebody elses source filter, but
if all of your sources are mp4, then I can suggest that LSmashVideoSource for mp4, is frame accurate. (leastwise, in my experience)
It's a .TS, not .mp4. And my gut tells me (which could be wrong) that what my routine is doing is reflective of a bug in Avisynth. But let me try your re-write first. If it still points toward a bug, I'll try to gather resources together so that it can be reproduced by someone else (though I hope it doesn't come to that.)

(You see, if one keeps many seconds of unwanted footage ahead of the desired cut in point, it's always allowed the footage to become stable in time for the desired start point to be rendered in stable fashion. My gut says that changing the output image to black shouldn't have any bearing on the sequential decoding from the source filter. But I'll go forward as described.)

StainlessS
28th April 2023, 21:52
TS, is (I Think an ISO source), so again, try what was already suggested.

kedautinh12
29th April 2023, 02:53
With .ts ffmpeg, l-smash, DGDecNV can indexing good

StainlessS
29th April 2023, 11:57
Sorry, yes, DGIndex and DGIndexNV both good for the files they process.

kedautinh12
29th April 2023, 16:24
DGIndex i think not cause i remember some frame not correct when index .ts. L-smash and ffmpeg better

StainlessS
29th April 2023, 18:03
I've also noticed some BluRay in MKV produce garbage frames when jumping about after DGIndexNV / DGSource use.
[Not tried current one posted a few days ago in a MeGUI thread. EDIT:- https://forum.doom9.org/showthread.php?p=1986244#post1986244 ]

EDIT: If you jump a few frames before garbage, and step forward, garbage frames are decoded correctly.

Link to script added into comments in script function. also added below in BLUE.

# 0 <= S <= E <= FMX : E is +ve END Frame number (may be 0, where S will also be 0)