Пример #1
0
 def _ConvertToRGB(self):
     if not self.IsRGB:
         arg = avisynth.AVS_Value(self.display_clip)
         arg1 = avisynth.AVS_Value(self.matrix)
         arg2 = avisynth.AVS_Value(self.interlaced)
         args = avisynth.AVS_Value([arg, arg1, arg2])
         try:
             avsfile = self.env.Invoke("ConvertToRGB24", args, 0)
             arg.Release()
             self.display_clip = avsfile.AsClip(self.env)
         except avisynth.AvisynthError, err:
             return False
Пример #2
0
class Clip(object):
    '''Basic avs script loading class with raw output
    
    Only accepts YV12 and RGB24, and reorders the later (BGR to RGB)
    
    '''
    def __init__(self, text, filename=''):
        self.error = None
        self.incorrect_colorspace = False
        self.env = avisynth.avs_create_script_environment(3)
        curdir = os.getcwdu()
        dirname, basename = os.path.split(filename)
        if os.path.isdir(dirname):
            self.env.SetWorkingDir(dirname)
        self.file = avisynth.AVS_Value(filename)
        self.name = avisynth.AVS_Value(basename)
        self.dir = avisynth.AVS_Value(dirname)
        self.env.SetGlobalVar("$ScriptFile$", self.file)
        self.env.SetGlobalVar("$ScriptName$", self.name)
        self.env.SetGlobalVar("$ScriptDir$", self.dir)
        try:
            clip = self.env.Invoke('Eval', avisynth.AVS_Value(text), 0)
        except avisynth.AvisynthError, err:
            self.error = str(err)
            os.chdir(curdir)
            return
        self.clip = clip.AsClip(self.env)
        self.vi = self.clip.GetVideoInfo()
        if self.vi.IsYV12():
            self.depth = 16
            self.real_width = self.vi.width / 2
            self.real_height = self.vi.height / 2
        elif self.vi.IsRGB24():
            self.depth = 8
            self.real_width = self.vi.width
            self.real_height = self.vi.height
            # BGR -> RGB
            clip = avisynth.AVS_Value(self.clip)
            r = self.env.Invoke("ShowRed", clip, 0)
            b = self.env.Invoke("ShowBlue", clip, 0)
            merge_args = avisynth.AVS_Value(
                [b, clip, r, avisynth.AVS_Value("RGB24")])
            clip = self.env.Invoke("MergeRGB", merge_args, 0)
            self.clip = self.env.Invoke("FlipVertical", clip,
                                        0).AsClip(self.env)
        else:
            self.incorrect_colorspace = True
        os.chdir(curdir)
Пример #3
0
 def BGR2RGB(self, clip):
     '''Reorder AviSynth's RGB (BGR) to RGB
     
     BGR -> RGB
     BGRA -> RGBA
     '''
     if self.IsRGB:
         clip = avisynth.AVS_Value(clip)
         clip = self.env.Invoke("FlipVertical", clip, 0)
         r = self.env.Invoke("ShowRed", clip, 0)
         b = self.env.Invoke("ShowBlue", clip, 0)
         if self.IsRGB24:
             merge_args = avisynth.AVS_Value([b, clip, r, avisynth.AVS_Value("RGB24")])
             return self.env.Invoke("MergeRGB", merge_args, 0).AsClip(self.env)
         else:
             merge_args = avisynth.AVS_Value([clip, b, clip, r])
             return self.env.Invoke("MergeARGB", merge_args, 0).AsClip(self.env)
Пример #4
0
 def __init__(self, text, filename=''):
     self.error = None
     self.incorrect_colorspace = False
     self.env = avisynth.avs_create_script_environment(3)
     curdir = os.getcwdu()
     dirname, basename = os.path.split(filename)
     if os.path.isdir(dirname):
         self.env.SetWorkingDir(dirname)
     self.file = avisynth.AVS_Value(filename)
     self.name = avisynth.AVS_Value(basename)
     self.dir = avisynth.AVS_Value(dirname)
     self.env.SetGlobalVar("$ScriptFile$", self.file)
     self.env.SetGlobalVar("$ScriptName$", self.name)
     self.env.SetGlobalVar("$ScriptDir$", self.dir)
     try:
         clip = self.env.Invoke('Eval', avisynth.AVS_Value(text), 0)
     except avisynth.AvisynthError, err:
         self.error = str(err)
         os.chdir(curdir)
         return
Пример #5
0
 def _ConvertToRGB(self):
     # There's issues with RGB32, we convert to RGB24 
     # AviSynth uses BGR ordering but we need RGB
     try:
         clip = avisynth.AVS_Value(self.display_clip)
         if not self.IsRGB24:
             arg1 = avisynth.AVS_Value(self.matrix)
             arg2 = avisynth.AVS_Value(self.interlaced)
             args = avisynth.AVS_Value([clip, arg1, arg2])
             clip(self.env.Invoke("ConvertToRGB24", args, 0))
         r = self.env.Invoke("ShowRed", clip, 0)
         b = self.env.Invoke("ShowBlue", clip, 0)
         merge_args = avisynth.AVS_Value([b, clip, r, avisynth.AVS_Value("RGB24")])
         avsfile = self.env.Invoke("MergeRGB", merge_args, 0)
         r.Release()
         b.Release()
         clip.Release()
         self.display_clip = avsfile.AsClip(self.env)
         return True
     except avisynth.AvisynthError, err:
         return False
Пример #6
0
 def __init__(self, script, filename='', workdir='', env=None, fitHeight=None, 
              fitWidth=None, oldFramecount=240, display_clip=True, reorder_rgb=False, 
              matrix=['auto', 'tv'], interlaced=False, swapuv=False):
     # Internal variables
     self.initialized = False
     self.error_message = None
     self.current_frame = -1
     self.pBits = None
     self.display_clip = None
     self.ptrY = self.ptrU = self.ptrV = None
     # Avisynth script properties
     self.Width = -1
     self.Height = -1
     self.WidthSubsampling = -1
     self.HeightSubsampling = -1
     self.Framecount = -1
     self.Framerate = -1.0
     self.FramerateNumerator = -1
     self.FramerateDenominator = -1
     self.Audiorate = -1.0
     self.Audiolength = -1
     #~ self.AudiolengthF = None
     self.Audiochannels = -1
     self.Audiobits = -1
     self.IsAudioFloat = None
     self.IsAudioInt = None
     self.IsRGB = None
     self.IsRGB24 = None
     self.IsRGB32 = None
     self.IsYUV = None
     self.IsYUY2 = None
     self.IsYV24 = None
     self.IsYV16 = None
     self.IsYV12 = None
     self.IsYV411 = None
     self.IsY8 = None
     self.IsPlanar = None
     self.IsInterleaved = None
     self.IsFieldBased = None
     self.IsFrameBased = None
     self.GetParity  = None
     self.HasAudio = None
     self.HasVideo = None
     self.Colorspace = None
     self.ffms_info_cache = {}
     
     # Create the Avisynth script clip
     if env is not None:
         if isinstance(env,avisynth.PIScriptEnvironment):
             self.env = env
         else:
             raise TypeError("env must be a PIScriptEnvironment or None")
     else:
         if isinstance(script,avisynth.PClip):
             raise ValueError("env must be defined when providing a clip") 
         try:
             self.env=avisynth.avs_create_script_environment(3)
         except OSError:
             return
         if hasattr(self.env, 'GetError'):
             self.error_message = self.env.GetError()
             if self.error_message: return
     if isinstance(script,avisynth.PClip):
         self.clip=script
     else:
         if type(script) != unicode:
             f=unicode(script)
         else:
             f = script
         # vpy hack, remove when VapourSynth is supported
         if os.name == 'nt' and filename.endswith('.vpy'):
             f = ur'AviSource("{0}")'.format(filename)
         arg=avisynth.AVS_Value(f)           #assign to AVSValue
         scriptdirname, scriptbasename = os.path.split(filename)
         curdir = os.getcwdu()
         workdir = os.path.isdir(workdir) and workdir or scriptdirname
         if os.path.isdir(workdir):
             self.env.SetWorkingDir(workdir)
         self.file = avisynth.AVS_Value(scriptbasename)
         self.name = avisynth.AVS_Value(filename)
         self.dir = avisynth.AVS_Value(scriptdirname)
         self.env.SetGlobalVar("$ScriptFile$", self.file)
         self.env.SetGlobalVar("$ScriptName$", self.name)
         self.env.SetGlobalVar("$ScriptDir$", self.dir)
         arg2=avisynth.AVS_Value(filename)
         args=avisynth.AVS_Value([arg,arg2])
         try:
             avsfile=self.env.Invoke("eval",args,0) #use eval to load it
             self.clip=avsfile.AsClip(self.env)
         except avisynth.AvisynthError, err:
             fontSize=24
             self.error_message = str(err)
             lineList = []
             yLine = 0
             nChars = 0
             for errLine in str(err).split('\n'):
                 lineList.append('Subtitle("""%s""",y=%i,size=%i,text_color=$FF0000,align=8)' % (errLine, yLine, fontSize))
                 yLine += fontSize
                 nChars = max(nChars, len(errLine))
             eLength = oldFramecount
             eWidth = nChars * fontSize / 2
             eHeight = yLine + fontSize/4
             firstLine = 'BlankClip(length=%(eLength)i,width=%(eWidth)i,height=%(eHeight)i)' % locals()
             errText = firstLine + '.'.join(lineList)
             arg = avisynth.AVS_Value(errText)
             try:
                 avsfile=self.env.Invoke("eval",arg,0) #use eval to load it
                 self.clip=avsfile.AsClip(self.env)
             except avisynth.AvisynthError, err:
                 return
Пример #7
0
             nChars = max(nChars, len(errLine))
         eLength = oldFramecount
         eWidth = nChars * fontSize / 2
         eHeight = yLine + fontSize/4
         firstLine = 'BlankClip(length=%(eLength)i,width=%(eWidth)i,height=%(eHeight)i)' % locals()
         errText = firstLine + '.'.join(lineList)
         arg = avisynth.AVS_Value(errText)
         try:
             avsfile=self.env.Invoke("eval",arg,0) #use eval to load it
             self.clip=avsfile.AsClip(self.env)
         except avisynth.AvisynthError, err:
             return
     finally:
         os.chdir(curdir)
     if not self.env.GetVar("last").IsClip():#.AsClip(self.env)
         self.env.SetVar("last",avisynth.AVS_Value(self.clip))
 
 # Set the video properties
 self.vi=self.clip.GetVideoInfo()
 self.HasVideo = self.vi.HasVideo()
 if not self.HasVideo:
     self.clip = None
     errText = 'MessageClip("No video")'
     arg = avisynth.AVS_Value(errText)
     try:
         avsfile = self.env.Invoke("eval", arg, 0)
         self.clip = avsfile.AsClip(self.env)
     except avisynth.AvisynthError, err:
         return
     if not self.env.GetVar("last").IsClip():#.AsClip(self.env)
         self.env.SetVar("last",avisynth.AVS_Value(self.clip))