AnimeSuki.com Forum

AnimeSuki Forum (http://forums.animesuki.com/index.php)
-   Fansub Groups (http://forums.animesuki.com/forumdisplay.php?f=17)
-   -   Avisynth Help thread (http://forums.animesuki.com/showthread.php?t=48608)

TheFluff 2007-06-03 13:02

Avisynth Help thread
 
Since several people have asked me about how to do various things with Avisynth over the last few weeks, I think it's time we had a thread like this. Post your favorite tricks, utility functions, smart ways to do common things, etc.

I'll start off with the absolutely most common question I get:
How do I overlay an AFX clip or a picture on top of a video?
As usual with Avisynth, there's more than one way to do it, but one particularly convenient way is to write a custom function to do it. I did this once and Devastator had his own variant (that I'll let him post himself), but both are hidden deep in the AFX questions thread, so I'll post my version here:
Code:

function insertsign(clip mainclip, clip overlayclip, int startframe, int "endframe") {
        endframe = default(endframe,startframe+overlayclip.framecount()-1)
        endframe = (endframe == 0) ? startframe+overlayclip.framecount()-1 : endframe
        endframe = (endframe >= mainclip.framecount()-1) ? mainclip.framecount()-1 : endframe
       
        begin        = (startframe == 1) ? mainclip.trim(0,-1) : mainclip.trim(0,startframe-1)
        middle        = mainclip.trim(startframe,endframe)
        end        = (endframe == mainclip.framecount()-1) ? blankclip(mainclip,length=0) : mainclip.trim(endframe+1,0)
       
        middleoverlay = Overlay(middle, overlayclip, mask=overlayclip.showalpha())

        final = (startframe == 0) ? middleoverlay ++ end : begin ++ middleoverlay ++ end
        return final
}

How to use it:
1. Copy-paste the code into an empty text document and rename it to .avsi (not .avs).
2. Put the .avsi in your Avisynth plugins directory.
3. Use the function insertsign(), like so:
The function takes 3 arguments (and one optional one: endframe):
Code:

insertsign(clip, overlayclip, startframe [, endframe])
Clip is the video you want to overlay the picture/AFX sign on. In 99% of all cases, this should be "last" (without the quotes).
Overlayclip is the AFX sign/picture you want to overlay. The function expects this to be a RGB32 clip with a transparency mask. See the AFX thread for details on that, or consult the manual for your image editing programs on how to make transparent PNG's.
Startframe is the frame in "clip" where the overlaying should start.
Endframe is the frame in "clip" where the overlay should end; this parameter is optional and if you don't specify it (or set it to 0), it will default to startframe+length of the overlayclip.
Example usage:
Code:

avisource("X:/stuff/source.avi")
sign1 = avisource("X:/stuff/afx-sign.avi")
logo = imagesource("X:/stuff/logo.png", pixel_type="RGB32", end=100)

insertsign(last, logo, 0)
insertsign(last, sign1, 563, 900)


Is there a way to apply a filter to only a part of a video?
Yes, that's pretty simple, but if you want to do this seriously you'd be best off with using YATTA for it. Anyway, here's how you do it (exactly like YATTA does it, but manually):
Code:

avisource("X:/stuff/example.avi")

function filterchain1(clip c) {
    c
    # do some filtering here
    return last
}

function filterchain2(clip c) {
    c
    # NASTY EVIL BLUR OR SOMETHING HERE
    return last
}

trim(0,500).filterchain1()+trim(501,550).filterchain2()+trim(551,0).filterchain1()


Is there a way to filter only part of a frame?
Yes, it's possible, but probably not worth it. If you insist on doing it anyway, here's how:
Code:

avisource("X:/stuff/example.avi") # let's assume 640x480
top = crop(0, 0, 0, -400) # top 80 lines
middle = crop(0, 80, 0, -100) # middle 300 lines
bottom = crop(0, 380, 0, 0) # bottom 100 lines (I'm probably off by 1 somewhere here, but I can't be assed to correct it)

middle = middle.crazy_evil_filter()

stackvertical(top, middle, bottom)

For more advanced operations you will simply need to split the frame into more parts, and do something like
Code:

stackvertical(stackhorizontal(a, b, c), stackhorizontal(d, e, f), stackhorizontal(g, h, i))
but the exact details are left as an exercise to the reader.


How do I insert a logo/picture before the actual video starts?
Simplest way to do it, if the video isn't VFR:
Code:

avisource("X:/stuff/episode.avi")
logo = imagesource("X:/stuff/logo.png", end=200).converttoyv12().assumefps(last)

logo = audiodub(logo, blankclip(last, length=200))

logo ++ last

How do I change the speed of a clip?
This one is just for lols, but say you wanted a video to play 50% faster:
Code:

avisource("X:/stuff/slow.avi")
assumefps(last.framerate*1.5)
assumesamplerate(int(last.samplerate*1.5))


And that's what I can come up with today. Everyone else feel free to post other contributions or ask questions.

Starks 2007-06-03 14:49

Okay, here's a question. Is it possible to append a single repeating frame onto a video using ImageSource?

Until now, I've been doing it this way.

Code:

ImageSource("image.png", end=72, pixel_type="RGB32").ConvertToYV12()
and then appending it as an avi in a separate script.

Code:

partA=AviSource("encode.avi").AssumeFPS(23.976)                                                               
partB=AviSource("image.avi").AssumeFPS(23.976)
fin=partA++partB

Is there an easier way to do this?

jfs 2007-06-03 15:22

Quote:

Originally Posted by TheFluff (Post 976257)
Is there a way to filter only part of a frame?
Yes, it's possible, but probably not worth it. If you insist on doing it anyway, here's how:
Code:

avisource("X:/stuff/example.avi") # let's assume 640x480
top = crop(0, 0, 0, -400) # top 80 lines
middle = crop(0, 80, 0, -100) # middle 300 lines
bottom = crop(0, 380, 0, 0) # bottom 100 lines (I'm probably off by 1 somewhere here, but I can't be assed to correct it)

middle = middle.crazy_evil_filter()

stackvertical(top, middle, bottom)

For more advanced operations you will simply need to split the frame into more parts, and do something like
Code:

stackvertical(stackhorizontal(a, b, c), stackhorizontal(d, e, f), stackhorizontal(g, h, i))
but the exact details are left as an exercise to the reader.

Another way of doing this, though I haven't tested it, would be to crop out the part to be filtered (or crop out from a filtered copy) and use the Overlay function to put it back in. This is probably easier for non-simple cases.
You can also load an alpha mask image with ImageReader and use that mask for the Overlay, if you want non-rectangular areas filtered.


Quote:

Originally Posted by TheFluff (Post 976257)
How do I insert a logo/picture before the actual video starts?
Simplest way to do it, if the video isn't VFR:
Code:

avisource("X:/stuff/episode.avi")
logo = imagesource("X:/stuff/logo.png", end=200).converttoyv12().assumefps(last)

logo = audiodub(logo, blankclip(last, length=200)

logo ++ last


I think this can be simplified:
Code:

avisource("X:/stuff/episode.avi")
logo = imagesource("X:/stuff/logo.png", end=200).converttoyv12().assumefps(last)

logo + last

Exploiting that AlignedSplice (single +) will insert silence as needed.


As for Starks's question, there shouldn't be anything preventing you from just:
Code:

partA=AviSource("encode.avi").AssumeFPS(23.976)                                                               
partB=ImageSource("image.png", end=72, pixel_type="RGB32", fps=23.976).ConvertToYV12()
fin=partA++partB


martino 2007-06-03 16:23

Quote:

Originally Posted by jfs (Post 976402)
I think this can be simplified:
Code:

avisource("X:/stuff/episode.avi")
logo = imagesource("X:/stuff/logo.png", end=200).converttoyv12().assumefps(last)

logo + last


You can also do:
Code:

avisource("X:/stuff/episode.avi")
logo = imagesource("X:/stuff/logo.png").converttoyv12().loop(200).assumefps(last)

logo + last

Where instead of using "end=200" you use "loop(200)" to do the trick. Same output, just slightly different syntax.

Starks 2007-06-03 16:45

Anyone know any good sites that list (and possibly explain) syntax that AviSynth can read?

martino 2007-06-03 16:55

You should take a look at the AviSynth Documentation that comes with it, then maybe AviSynth's website (now they have a new MediaWiki which is still being worked on so some stuff is still missing) might have some more stuff, but generally it's the same as in the documentation AFAIK.

(hopefully I understood correctly what you meant there)

Starks 2007-06-03 17:00

Quote:

Originally Posted by martino (Post 976490)
You should take a look at the AviSynth Documentation that comes with it, then maybe AviSynth's website (now they have a new MediaWiki which is still being worked on so some stuff is still missing) might have some more stuff, but generally it's the same as in the documentation AFAIK.

(hopefully I understood correctly what you meant there)

I guess... <_<

I've been seeing some unfamiliar chains and syntax over the past week or so...

TheFluff 2007-06-03 17:04

Quote:

Originally Posted by jfs (Post 976402)
I think this can be simplified:
Code:

avisource("X:/stuff/episode.avi")
logo = imagesource("X:/stuff/logo.png", end=200).converttoyv12().assumefps(last)

logo + last

Exploiting that AlignedSplice (single +) will insert silence as needed.

Eh, aligned splice is ++, single + is unaligned. And it doesn't work anyway, because Avisynth doesn't let you splice two clips together where one has audio and the other doesn't, you will get an error message "Splice: one clip has audio and the other doesn't (not allowed)", which is the reason I wrote it like that in the first place. :V

Quote:

Originally Posted by jfs (Post 976402)
As for Starks's question, there shouldn't be anything preventing you from just:
Code:

partA=AviSource("encode.avi").AssumeFPS(23.976)                                                               
partB=ImageSource("image.png", end=72, pixel_type="RGB32", fps=23.976).ConvertToYV12()
fin=partA++partB


Actually there IS something that prevents you from doing that, namely the fact that ImageSource's fps argument only takes integer numbers. Which is retarded, but that's the way it is, so you have to do imagesource(...).assumefps(). Otherwise yes, that works.
EDIT: ok, it didn't USE to take floats, but it does now. So yes, it works as advertised.

edogawaconan 2007-06-03 19:21

my favorite (very simple) script:
Code:

a=directshowsource("a.avi")
b=directshowsource("b.wmv",convertfps=true)
c=directshowsource("c.mp4",convertfps=true)
d=directshowsource("d.mkv",convertfps=true)
stackhorizontal(stackvertical(a,b),stackvertical(c,d))

basically plays 4 video at once. just make sure all the specs are same (or do manual adjustment as needed).
nice and easy way to compare encodes :p

Starks 2007-06-03 19:26

Quote:

Originally Posted by edogawaconan (Post 976623)
my favorite (very simple) script:
Code:

a=directshowsource("a.avi")
b=directshowsource("b.wmv",convertfps=true)
c=directshowsource("c.mp4",convertfps=true)
d=directshowsource("d.mkv",convertfps=true)
stackhorizontal(stackvertical(a,b),stackvertical(c,d))

basically plays 4 video at once. just make sure all the specs are same (or do manual adjustment as needed).
nice and easy way to compare encodes :p

Trippy... I gotta try that now. <_<

martino 2007-06-04 04:59

Quote:

Originally Posted by edogawaconan (Post 976623)
nice and easy way to compare encodes :p

Not really if you are using DirectShowSource() since in most cases the frames will not match, might be good enough for basic comparisons, but if you want to be really accurate load avss.dll from Haali's splitter directory and use DSS2() for input. It's frame accurate, but audio input doesn't work with it.

Furthermore you can also do:
Code:

a = AviSource("clip1.avi").ConvertToRGB()
b = AviSource("cllip2.avi").ConvertToRGB()
Compare(a,b)

And it will show the differences in a more "mathematical" way.

edogawaconan 2007-06-04 09:18

Quote:

Originally Posted by martino (Post 977270)
Not really if you are using DirectShowSource() since in most cases the frames will not match, might be good enough for basic comparisons, but if you want to be really accurate load avss.dll from Haali's splitter directory and use DSS2() for input. It's frame accurate, but audio input doesn't work with it.

I know, but it looks accurate enough for my recent compares (which basically a video I encoded three times then load them at once with that script). Will try DSS2() :)
by the way, is dss2()'s frame accuracy comparable to normal xvid encodes in avi? :)

Quote:

Originally Posted by martino (Post 977270)
Furthermore you can also do:
Code:

a = AviSource("clip1.avi").ConvertToRGB()
b = AviSource("cllip2.avi").ConvertToRGB()
Compare(a,b)

And it will show the differences in a more "mathematical" way.

I've seen enough math in my daily life though :heh: (that is, being having math as major :eyespin: )

martino 2007-06-04 09:57

Quote:

Originally Posted by edogawaconan (Post 977515)
I know, but it looks accurate enough for my recent compares (which basically a video I encoded three times then load them at once with that script). Will try DSS2() :)
by the way, is dss2()'s frame accuracy comparable to normal xvid encodes in avi? :)

It should have the same accuracy as when VfW is used, I only tried one sample (mkv and re-encoded to xvid in AVI) and the frames did match wherever I scrolled (IIRC, it's been a while since).

Quote:

Originally Posted by edogawaconan (Post 977515)
I've seen enough math in my daily life though :heh: (that is, being having math as major :eyespin: )

Ouch... /me puts his hat down :)

Starks 2007-06-04 23:37

What is the difference between AviSource, DirectShowSource, and DSS2?

TheFluff 2007-06-05 03:24

Avisource uses VfW codecs and the VfW framework only. Think of it like VirtualDub but in Avisynth. As long as the AVI in question doesn't have any particularly funky hacks, it will always have frame-accurate seeking.
Directshowsource uses Directshow codecs and is hence the Avisynth equivalent of Windows Media Player. It's usually NOT frame-accurate, but it does support VFR, kind of; either through converting to CFR (convertfps=true) or by just reading all frames and ignoring timecodes.
DSS2 is Haali's implementation of Directshowsource. It has the advantage of being frame-accurate, but only supports VFR by converting it to CFR; at least that was the case when I tried it last time, it might have changed.

Quarkboy 2007-06-06 13:53

I might mention that Graft (author of dvd2avi i.e. dgindex) is almost to the beta stages for DGAVCDec, a similar program which allows preindexing and then frame accurate seeking in AVC (i.e. h.264) encoded files, in the exact same manner that dgindex indexes mpeg2 .vob streams.

It's in alpha right now but it works pretty well already. Not only will it improve encoding speed considerably for such sources, but it will make using AVC sources a lot easier to work with in avisynth.

http://forum.doom9.org/showthread.php?t=122598

DarkT 2007-06-06 14:56

About the applying filters only to certain parts, there's this dude called "stickyboy" and he has a pretty good thing I use lots of times, 's called "RemapFrames" I think...

Here's how you use it:

ReplaceFramesSimple(Tcomb(mode=2,fthreshl=12, othreshl=19),
\ mappings= "[106 218]
[492 540]
[31815 33226]
[33283 33334]
[33420 33434]
[33479 33558]
[33588 33614]
[33851 33947]")

Starks 2007-06-06 23:12

What percent of encoders in the fansub community use Avisynth to the extent that Fluff, quark, or eek do?

skystrife 2007-06-06 23:41

Hello all,

I'm just starting to get into the fansubbing scene, mainly doing work on my own in order to get more experienced before actually applying for a job. I'm interested in doing timing and encoding.

Bah, enough about me.

I've got myself a HD RAW, but it's got some flaws that are really quite irritating. Rather than try to describe it using endless gobs of words that make no sense, I'll just show you. This image isn't blown up at all, and hopefully Paint doesn't barf with PNGs as much as it does with JPEGs. >_<

http://skystrife.com/images/weird_edges.png

It's on a(n?) H.264 encode, and it's quite frustrating trying to get rid of. If I aWarpSharp it to death I can get rid of most of the problem, but that in itself causes issues of its own. Would I be best off just applying the heavy aWarpSharp-ing to any areas in the video I see this issue (basically anything that has dark lines that move... -_-)? Have any of you come across this issue before (I'd bet someone has, judging by the sheer amount of experience some of the well-known people here have)?

Quarkboy 2007-06-07 04:12

Quote:

Originally Posted by skystrife (Post 981733)
Hello all,

I'm just starting to get into the fansubbing scene, mainly doing work on my own in order to get more experienced before actually applying for a job. I'm interested in doing timing and encoding.

Bah, enough about me.

I've got myself a HD RAW, but it's got some flaws that are really quite irritating. Rather than try to describe it using endless gobs of words that make no sense, I'll just show you. This image isn't blown up at all, and hopefully Paint doesn't barf with PNGs as much as it does with JPEGs. >_<

http://skystrife.com/images/weird_edges.png

It's on a(n?) H.264 encode, and it's quite frustrating trying to get rid of. If I aWarpSharp it to death I can get rid of most of the problem, but that in itself causes issues of its own. Would I be best off just applying the heavy aWarpSharp-ing to any areas in the video I see this issue (basically anything that has dark lines that move... -_-)? Have any of you come across this issue before (I'd bet someone has, judging by the sheer amount of experience some of the well-known people here have)?

That's not a problem with the encoding. It's either a problem with the original broadcast source (bad de-interlacing or upsizing) or a problem with the capture from the source itself.
The best thing to do is to find another raw without the issue. If all the raws have the same problem then it's the fault of the broadcaster themselves, and you have 2 choices:
1. Try warpsharping it to death, or some other solution such as tisophate (one of tritical's avisynth filters that's supposed to help jagged lines like that. Personally I never got it to produce satisfactory results).
2. Ignore it and blame the issue on the broadcasting company.

1. is a lot of work and might end up doing more harm then good. 2. is kind of like a cop out, but hey, you can only do so much.


All times are GMT -5. The time now is 08:16.

Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2014, vBulletin Solutions, Inc.