Welcome to Doom9's Forum, THE in-place to be for everyone interested in DVD conversion.

Before you start posting please read the forum rules. By posting to this forum you agree to abide by the rules.

 

Go Back   Doom9's Forum > Capturing and Editing Video > Avisynth Usage

Reply
 
Thread Tools Search this Thread Display Modes
Old 5th December 2012, 12:01   #61  |  Link
StainlessS
HeartlessS Usurer
 
StainlessS's Avatar
 
Join Date: Dec 2009
Location: Over the rainbow
Posts: 8,281
Modified script from post #52 to allow choosing crop mode. (Only use CropExact[0] if RGB)

Code:
# EDIT To Suit

VID = False                                                                 # True == Video : False == Pics, see IN_FILES directories/wildcards

IN_FILES = (VID) ? "D:\VID\*.AVI|MPG|MPEG" : "D:\PIC\*.BMP|JPG|JPEG|PNG"   # Added '|' Pipe separator for multiple Wildcard in Extensions ONLY.
AUTOCROP            = True                                                  # Crop off black borders.
AUTOCROP_THRESH     = 0.0   # Default 0.0 allows QueryBorderCop to auto set super sensitive mode for single images, use default 0.0 normally
SHOWBORDER          = True                                                  # True Show replaced border in Red, else Black. (Test AUTOCROP)
AUTOLEVEL_STRENGTH  = 1.0                                                   # AutoLevel Strength 0.0 -> 1.0
SHOWAUTOLEVEL       = True                                                  # Right Hand Side Only AutoLeveled
#
FAR                 = 4.0/3.0                                               # Required Frame Aspect Ratio
SzLimit             = 1024
DEBUG               = True                                                  # Debug
CROPMODE            = 2                                                     # 0=CropExact(RGB Only), 1=CropLess, 2=CropMore, 3=CropPlus
# ---------------------
BORDER      = (SHOWBORDER) ? $FF0000: (VID) ? $101010 : $000000             # Color for replaced borders.
SUBS        = True          # Subtitles on clips/images
DEBUG_QBC   = True                                                          # Debug QueryBorderCrop
DEBUG_QLMM  = True                                                          # Debug QueryLumaMinMax
FPS         = (VID) ? 30000.0/1001.0 : 1.0                                  # Whatever.
OUT_FILE    = "FILE.List"   # Dont need to change really, just a name.
#--------------------------------------------------------------------------------
GScript("""
    picclip = 0                         # dummy for now
    result=RT_WriteFileList(IN_FILES,OUT_FILE)                                  # Create a listing file of Pic files
    Assert((result!=0), "No files found")
    FILELIST=RT_ReadTxtFromFile(OUT_FILE)                                       # Get list of files
    FILES=RT_TxtQueryLines(FILELIST)                                            # Query Number of lines in String ie number of files.
    if(DEBUG){RT_Debug("Found files = " + String(FILES))}
    # Prescan to ascertain max wid/hit of all files, also Autocrop & Auto Levels
    MAXH=0 MAXW=0                                                               # init max wid and hit
    CROPCOORDS=""
    AUTOLEVELS=""
    AUTOLEVEL=(AUTOLEVEL_STRENGTH>0.0)                                                              # Adjust using Levels.
    For(i=0,FILES-1) {
        FN=RT_TxtGetLine(FILELIST,i)                                            # Filename of avi file i
        if(DEBUG){RT_Debug(String(i)+") Getting File dimensions",FN)}
        if(VID) {
            k=DirectshowSource(FN)
            k=k.ConvertToYUY2().Trim(0,0)
        } else {
            k=ImageReader(FN,end=0).ConvertToRGB32()                            # With Bmap, some come up as RGB24 bit some RGB32.
        }
        OrgH=k.height    OrgW=k.width
        if(DEBUG){RT_Debug ("Orig Width =",String(orgW),"Orig Height =",String(OrgH))}
        if (AUTOCROP) {
            if(AUTOCROP_THRESH == 0.0) { # Call without Thresh arg, allows use of auto super sensitive setting for single frame iamges
                QBC = k.QueryBorderCrop(samples=24,laced=VID,debug=(DEBUG&&DEBUG_QLMM))
            } else { # Will prevent auto super sensitive from working
                QBC = k.QueryBorderCrop(samples=24,Thresh=AUTOCROP_THRESH,laced=VID,debug=(DEBUG&&DEBUG_QLMM))
            }
            Eval(QBC)
            k=k.Crop(QBCropXM,QBCropYM,QBCropWM,QBCropHM)
            QBC = RT_TxtGetLine(QBC,CROPMODE)                           # remember required mode, CropMore etc
            CROPCOORDS=RT_TxtAddStr(CROPCOORDS,QBC)                     # Avisynth Bugfix, same as CROPCOORDS = CROPCOORDS + QBC + Chr(10)
        }
        if (AUTOLEVEL) {
            QLMM = k.QueryLumaMinMax(debug=(DEBUG&&DEBUG_QLMM))
            Eval(QLMM)
            AUTOLEVELS=RT_TxtAddStr(AUTOLEVELS,QLMM)                    # Avisynth Bugfix, same as AUTOLEVELS = AUTOLEVELS + QLMM + Chr(10)
        }
        H=k.height    W=k.width
        If(MAXH<H) {MAXH=H}
        If(MAXW<W) {MAXW=W}        
        if(DEBUG){RT_Debug("MAX Width So Far =",String(MAXW),"MAX Height So Far =",String(MAXH))}
        if(DEBUG){RT_Debug(" ----------------------------- ")}       # line separator
    }
    maxFAR = MAXW / MAXH

    if(FAR >= maxFAR) {
        canvasheight = (MAXH > SzLimit) ? SzLimit : MAXH            
        canvasheight    = ((canvasheight  + 1) / 2) * 2
        canvaswidth     = Int((canvasheight * FAR + 2) / 4) * 4
    } else {
        canvaswidth = (MAXW > SzLimit) ? SzLimit : MAXW             
        canvaswidth    = ((canvaswidth  + 2) / 4) * 4
        canvaswidth = (canvaswidth > SzLimit) ? SzLimit : canvaswidth
        canvasheight   = Int((canvaswidth * FAR + 1) / 2) * 2        
    }
    
    if(DEBUG){RT_Debug("SETTING CanvasWidth =",String(canvaswidth)," CanvasWeight =",String(canvasheight))}
    if(DEBUG){RT_Debug(" ----------------------------- ")}       # line separator
    For(i=0,FILES-1) {
        FN=RT_TxtGetLine(FILELIST,i)                                            # Filename of avi file i
        if(DEBUG){RT_Debug(string(i)+ ")","Processing File",FN)}
        if(VID) {
            k=DirectshowSource(FN).ConvertToYUY2().Trim(0,0)
        } else {
            k=ImageReader(FN,end=0).ConvertToRGB32()                            # With Bmap, RGB24 problems with addborders
        }
        OrgH=k.height    OrgW=k.width
        if (AUTOLEVEL) {
            # We do AUTOLEVELS before crop, because of SHOWAUTOLEVEL
            QAT = RT_TxtGetLine(AUTOLEVELS,i)
            Eval(QAT)
            if(k.IsYUV()) {
                CSMin = 16
                CSMax = 235
            } else {        
                CSMin = 0
                CSMax = 255
            }
            TMP_K=k         
            ALMin = Int(CSMin - ((CSMin - QLMMMin) * AUTOLEVEL_STRENGTH) + 0.5) # Round Up
            ALMax = Int(CSMax - ((CSMax - QLMMMax) * AUTOLEVEL_STRENGTH))       # Round down
            k = k.Levels(ALMin,1.0,ALMax,CSMin,CSMax,Coring=False) # DONT USE Coring 
            if(SHOWAUTOLEVEL) {
                tmpw=int((k.width/4)*2)
                Left = TMP_K.Crop(0,0,tmpw,-0)
                Right= k.Crop(tmpw,0,-0,-0)
                k=StackHorizontal(Left,Right)               
            }
        }

        if (AUTOCROP) {
        QBC = RT_TxtGetLine(CROPCOORDS,i)
        QBCTrailer=Select(CROPMODE,"=","L=","M=","P=")
            QBC1=StrReplace(QBC,QBCTrailer,"=")                    # Change all to style without a trailer eg QBCropXM to QBCropX.
            QBC2=StrReplace(QBC1,"QBC","C")                        # Change all style QBCropX to CropX  
            Eval(QBC2)                                             # Set variables in Avisynth, ie CropX etc
            k=k.Crop(CropX,CropY,CropW,CropH)       
        }
        
        iWidth=k.Width()        iHeight=k.Height()
        
        cFAR    = float(canvaswidth) / canvasheight
        iFar    = float(iWidth) / iHeight
        Left=0 Top=0 Right=0 Bot=0

        if(iFAR >= cFAR) {
            oWid    =   canvaswidth
            oHit    =   (oWid*iHeight/iWidth/4)*4
            Top     =   (canvasheight-oHit) / 2
            Bot     =   canvasheight - oHit - Top
        } else {
            oHit    =   canvasheight
            oWid    =   (oHit*iWidth/iHeight/4)*4
            Left    =   (canvaswidth-oWid) / 2
            Right   =   canvaswidth - oWid - Left           
        }
        
        RT_Debug("Resize","oWid="+String(oWid),"oHit="+String(oHit),"Left="+String(Left),"Top="+String(Top),"Rgt="+String(Right),"Bot="+String(Bot))
        k = k.Spline36Resize(oWid,oHit).addborders(Left,Top,Right,Bot,BORDER).assumefps(fps)
        
        if(SUBS) {
            k=k.subtitle(String(i) + "/"+String(FILES-1) +" " + FN)
            k=k.subtitle("Orig  width=" + string(orgW) +" "  + "Orig  height=" + string(orgH) +" FAR="+String(Float(orgW) /orgH, "%.2f"),20,20)
            if (AUTOCROP) {k=k.subtitle("Crop width=" + string(iWidth) +" "  + "Crop  height=" + string(iHeight) +" FAR="+String(Float(iWidth) /iHeight, "%.2f"),20,40)
            } else {k=k.subtitle("NO AUTOCROP",20,40)}
            k=k.subtitle("New width=" + string(oWid) +" " + "New height=" + string(oHit)+" FAR="+String(Float(oWid)/oHit,"%.2f"),20,60)         
            k=k.subtitle("Out width=" + string(k.Width) +" " + "Out height=" + string(k.Height)+" FAR="+String(Float(k.Width)/k.Height,"%.2f"),20,80)         
            if(AUTOLEVEL) {
                k=k.subtitle("Auto Levels(" + String(ALMin) + ",1.0," + String(ALMAx) + "," + String(CSMin) + "," + String(CSMax) + ")",20,100)
                k=k.subtitle("QLMMMin = " + String(QLMMMin) + " QLMMMax = " +String(QLMMMAx),20,120)
                k=k.subtitle("AUTOLEVEL @ " + String(AUTOLEVEL_STRENGTH*100.0,"%.0f") + "%",20,140)
            } else {k=k.subtitle("NO AUTOLEVEL",20,100)}
        }
        picclip = (IsClip(picclip)) ? picclip ++ k : k  # Append to  clip so far
        if(!VID){k=k.ConvertToRGB24()}
        if(DEBUG){RT_Debug(" ")}       # line separator
        if(DEBUG){RT_Debug(" ----------------------------- ")}       # line separator
    }
""")
PicClip
return Last


Function StrReplace(string s,string find,string replace) # Repeated, string replacements
# Original:- http://forum.doom9.org/showthread.php?p=1540504#post1540504           By Vampiredom, Gavino, IanB
#{i=s.FindStr(find)return(i==0?s: s.LeftStr(i-1)+replace+s.MidStr(Strlen(find)+i).StrReplace(find,replace))}
# Converted to use RT_Stats RT_StrAddStr() to avoid 2.58/2.6a3 string concatenation bug:-
{i=s.FindStr(find) return(i==0?s:RT_StrAddStr(s.LeftStr(i-1),replace,s.MidStr(Strlen(find)+i).StrReplace(find,replace)))}

Function StrReplaceDeep(string s,string find,string replace) # DEEP Repeated, string replacements
# When replacing eg pairs of SPACE characters by single SPACE, StrReplace can leave some pairs non replaced.
# Assumes that you want to rescan where replaced string could constitute part of a new find substring,
# Length of replace must be shorter than length of find, else will call StrReplace instead.
# Based On:- http://forum.doom9.org/showthread.php?p=1540504#post1540504           By Vampiredom, Gavino, IanB
# {i=s.FindStr(find)return i==0?s:s.LeftStr(i-1)+( strlen(replace)<strlen(find)? \
# StrReplaceDeep(replace+s.MidStr(Strlen(find)+i),find,replace) : replace+StrReplace(s.MidStr(Strlen(find)+i),find,replace))}
# Converted to use RT_Stats RT_StrAddStr() to avoid 2.58/2.6a3 string concatenation bug:-
{i=s.FindStr(find)return i==0?s:RT_StrAddStr(s.LeftStr(i-1),( strlen(replace)<strlen(find)? \
StrReplaceDeep(RT_StrAddStr(replace,s.MidStr(Strlen(find)+i)),find,replace) : \
RT_StrAddStr(replace,StrReplace(s.MidStr(Strlen(find)+i),find,replace))))}
EDITED: Added CROPMODE and AUTOCROP_THRESH removed.

EDIT: Put back AUTOCROP_THRESH again, Dang !

EDIT: For much better image splicer see ImageSplicer.avs included with RT_Stats here:
http://forum.doom9.org/showthread.php?t=165479
__________________
I sometimes post sober.
StainlessS@MediaFire ::: AND/OR ::: StainlessS@SendSpace

"Some infinities are bigger than other infinities", but how many of them are infinitely bigger ???

Last edited by StainlessS; 31st May 2013 at 20:36.
StainlessS is offline   Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 03:12.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2020, vBulletin Solutions Inc.