View Full Version : Any tool for Autocrop and Resize based on ffmpeg
gurusinghbrar
30th August 2012, 07:27
hi all..
anyone know about any tool which autocrop and resize the input video and show only the output values for use in ffmpeg base encoding,,or tool like megui resize or crop tool wich run separately not included in megui.
or any one have any batch file which detect video like avi,mkv,mp4 file and load into .avs file and use autocrop and autoresize filter/method on it and save the out values(crop and resuze value only) in text file...
thanx..
StainlessS
30th August 2012, 15:26
Maybe of interest: QueryborderCrop(), Requires RT_Stats v1.03 plug & GScript for Avisynth.
http://forum.doom9.org/showthread.php?p=1586857#post1586857
And examples using it:
http://forum.doom9.org/showthread.php?p=1586860#post1586860
http://forum.doom9.org/showthread.php?p=1589022#post1589022
EDIT: Or this modified script:
GSCript("""
RELATIVE= true # True show width as eg -8, False as eg 640
DEBUG = True # Show output in DebugView
OUTFILE = "CROP.TXT" # Crop log
VideoFileName =LCase("D:\TEST_VOBS\PAL-P_169_P\ReturnOfTheKing_Extended.d2v")
MPEG2Source(VideoFileName,cpu2="xxxxxx")
QBC=QueryBorderCrop(Relative=RELATIVE,Debug=DEBUG) # Get Return string and initialize QBCropX type variables in Avisynth
Eval(QBC) # Set crop variables
#Spline36Resize(640,480,QBCropX,QBCropY,QBCropW,QBCropH) # Crop Exact, DO NOT USE IN CROP, OK in Resize
#Crop(QBCropXL,QBCropYL,QBCropWL,QBCropHL) # CropLess
Crop(QBCropXM,QBCropYM,QBCropWM,QBCropHM) # CropMore
#Crop(QBCropXP,QBCropYP,QBCropWP,QBCropHP) # CropPlus
A = RT_TxtGetLine(QBC,0) # Exact
B = RT_TxtGetLine(QBC,1) # CropLess
C = RT_TxtGetLine(QBC,2) # CropMore
D = RT_TxtGetLine(QBC,3) # CropPlus
Subtitle(A,y=0)
Subtitle(B,y=20)
Subtitle(C,y=40)
Subtitle(D,y=60)
RT_TxtWriteFile(QBC,OUTFILE) # write log file
return last
""")
With results in Debugview:
00000004 15:35:38 RT_Debug: QBC:
00000005 15:35:38 RT_Debug: QBC: QueryBorderCrop v1.08 - 29 Aug 2012 - By StainlessS
00000006 15:35:38 RT_Debug: QBC:
00000007 15:35:38 RT_Debug: QBC: Samples=24 Thresh=-32.00 Laced=true Matrix=2
00000008 15:35:38 RT_Debug: QBC: XMod=2 YMod=4 WMod=4 HMod=4
00000009 15:35:38 RT_Debug: QBC: Relative=true Prefix=QBCrop RLBT=15 Ignore=0.20
00000010 15:35:38 RT_Debug: QBC:
00000011 15:35:38 RT_Debug: QBC: AutoThresh Using Avisynth Planar Luma Sampling
00000012 15:35:39 RT_Debug: QBC: AutoThresh 1 ) [715 ] YPlaneMin=16
00000013 15:35:39 RT_Debug: QBC: AutoThresh 2 ) [1430 ] YPlaneMin=16
00000014 15:35:39 RT_Debug: QBC: AutoThresh 3 ) [2145 ] YPlaneMin=16
00000015 15:35:39 RT_Debug: QBC: AutoThresh 4 ) [2860 ] YPlaneMin=16
00000016 15:35:40 RT_Debug: QBC: AutoThresh 5 ) [3575 ] YPlaneMin=16
00000017 15:35:40 RT_Debug: QBC: AutoThresh 6 ) [4290 ] YPlaneMin=16
00000018 15:35:40 RT_Debug: QBC: AutoThresh 7 ) [5004 ] YPlaneMin=16
00000019 15:35:40 RT_Debug: QBC: AutoThresh 8 ) [5719 ] YPlaneMin=16
00000020 15:35:41 RT_Debug: QBC: AutoThresh 9 ) [6434 ] YPlaneMin=16
00000021 15:35:41 RT_Debug: QBC: AutoThresh 10) [7149 ] YPlaneMin=16
00000022 15:35:41 RT_Debug: QBC: AutoThresh 11) [7864 ] YPlaneMin=16
00000023 15:35:41 RT_Debug: QBC: AutoThresh 12) [8579 ] YPlaneMin=16
00000024 15:35:42 RT_Debug: QBC: AutoThresh 13) [9294 ] YPlaneMin=16
00000025 15:35:42 RT_Debug: QBC: AutoThresh 14) [10009] YPlaneMin=16
00000026 15:35:42 RT_Debug: QBC: AutoThresh 15) [10724] YPlaneMin=16
00000027 15:35:42 RT_Debug: QBC: AutoThresh 16) [11439] YPlaneMin=16
00000028 15:35:42 RT_Debug: QBC: AutoThresh 17) [12154] YPlaneMin=16
00000029 15:35:43 RT_Debug: QBC: AutoThresh 18) [12869] YPlaneMin=16
00000030 15:35:43 RT_Debug: QBC: AutoThresh 19) [13583] YPlaneMin=16
00000031 15:35:43 RT_Debug: QBC: AutoThresh 20) [14298] YPlaneMin=16
00000032 15:35:43 RT_Debug: QBC: AutoThresh 21) [15013] YPlaneMin=16
00000033 15:35:44 RT_Debug: QBC: AutoThresh 22) [15728] YPlaneMin=16
00000034 15:35:44 RT_Debug: QBC: AutoThresh 23) [16443] YPlaneMin=16
00000035 15:35:44 RT_Debug: QBC: AutoThresh 24) [17158] YPlaneMin=16
00000036 15:35:44 RT_Debug: QBC: AutoThresh Thresh=48.00
00000037 15:35:44 RT_Debug: QBC:
00000038 15:35:48 RT_Debug: QBC: Top 2) [1430 ] AveY=65.15 CY=75
00000039 15:35:48 RT_Debug: QBC: Bot 2) [1430 ] AveY=61.18 CH=-76
00000040 15:35:48 RT_Debug: QBC: Lft 2) [1430 ] AveY=48.26 CX=46
00000041 15:35:49 RT_Debug: QBC: Rgt 2) [1430 ] AveY=48.14 CW=-77
00000042 15:35:49 RT_Debug: QBC: Lft 3) [2145 ] AveY=120.76 CX=0
00000043 15:35:49 RT_Debug: QBC: Rgt 3) [2145 ] AveY=106.21 CW=0
00000044 15:36:02 RT_Debug: QBC: Active Frames (where image coords first found) = 2
00000045 15:36:02 RT_Debug: QBC: Sampled Borders: X=0 Y=75 W=0(720) H=-76(425)
00000046 15:36:02 RT_Debug: QBC:
00000047 15:36:02 RT_Debug: QBC: CropExact: X = 0 Y =75 W =720(0) H =425(-76)
00000048 15:36:02 RT_Debug: QBC: CropLess: XL= 0 YL=72 WL=720(0) HL=428(-76)
00000049 15:36:02 RT_Debug: QBC: CropMore: XM= 0 YM=76 WM=720(0) HM=424(-76)
00000050 15:36:02 RT_Debug: QBC: CropPlus: XP= 2 YP=80 WP=716(-2) HP=416(-80)
00000051 15:36:02 RT_Debug: QBC:
00000052 15:36:02 RT_Debug: QBC: Return: QBCropX=0 QBCropY=75 QBCropW=0 QBCropH=-76
00000053 15:36:02 RT_Debug: QBC: Return: QBCropXL=0 QBCropYL=72 QBCropWL=0 QBCropHL=-76
00000054 15:36:02 RT_Debug: QBC: Return: QBCropXM=0 QBCropYM=76 QBCropWM=0 QBCropHM=-76
00000055 15:36:02 RT_Debug: QBC: Return: QBCropXP=2 QBCropYP=80 QBCropWP=-2 QBCropHP=-80
00000056 15:36:02 RT_Debug: QBC: Return: QBCropThresh=48.000000
And the Log File written here as "Crop.Txt"
QBCropX=0 QBCropY=75 QBCropW=0 QBCropH=-76
QBCropXL=0 QBCropYL=72 QBCropWL=0 QBCropHL=-76
QBCropXM=0 QBCropYM=76 QBCropWM=0 QBCropHM=-76
QBCropXP=2 QBCropYP=80 QBCropWP=-2 QBCropHP=-80
QBCropThresh=48.000000
EDIT: Does not actually do the crop
EDIT: Now it does.
StainlessS
30th August 2012, 20:25
Or this script which scans all avi/mpg files in a directory and writes names and crop coords to a text file.
# EDIT To Suit
INFILE = "D:\VID\*.AVI|MP*G" # Video Directory and wildcards
OUTDIR = "D:\VID\" # Directory for output Crop Log and Temp Files
OUTLOG = "CROP.TXT" # Crop log
RELATIVE = True # True show width as eg -8, False as eg 640
CROPCOORDS = 2 # 0=CropExact (RESIZE ONLY), 1=CropLess, 2=CropMore, 3=CropPlus
DEBUG = False # Show output in DebugView
# ----------------------------------------------------------
GSCript("""
LISTNAME = OUTDIR + "FILE.TXT"
Result=RT_WriteFileList(INFILE,LISTNAME) # Write list file of video file names
Assert((Result!=0), "No files found")
FILELIST=RT_ReadTxtFromFile(LISTNAME) # Read list of names
NFILES=RT_TxtQueryLines(FILELIST) # Number of files in list
CROPTXT="" # Init output log
For(i=0,NFILES-1) {
FN=RT_TxtGetLine(FILELIST,i) # The file name of video clip
V=DirectshowSource(FN).ConvertToYV12() # Load file, Convert to whatever your required colorspace (affects cropping)
QBC=V.QueryBorderCrop(relative=RELATIVE,debug=DEBUG) # Inquire all modes cropping
COORDS=RT_TxtGetLine(QBC,CROPCOORDS) # The cropping mode that is required
WRCROP=String(i)+" ) "+COORDS+Chr(9)+FN # Glue coords and filename together
CROPTXT=RT_TxtAddStr(CROPTXT,WRCROP) # Add it to the log string
}
RT_TxtWriteFile(CROPTXT,OUTDIR+OUTLOG) # Write log file
""")
Return ColorBars() # Dummy return clip
Crop Log
0 ) QBCropXM=18 QBCropYM=0 QBCropWM=-18 QBCropHM=0 D:\VID\1.mpg
1 ) QBCropXM=2 QBCropYM=0 QBCropWM=-2 QBCropHM=0 D:\VID\Lexx-S02xE01-Mantrid.avi
2 ) QBCropXM=2 QBCropYM=0 QBCropWM=-2 QBCropHM=0 D:\VID\Lexx-S02xE02-Terminal.avi
3 ) QBCropXM=6 QBCropYM=0 QBCropWM=-6 QBCropHM=-4 D:\VID\Lexx-S02xE03-Lyekka.avi
Modify it to output whatever format you like, eg batch file or multiple batch files, or batch of avs files.
EDIT: QueryBorderCrop(), comes with RT_Stats v1.03.
gurusinghbrar
31st August 2012, 14:22
thanks @StainlessS for reply but idon't know more about this gscript, and how to use it with avisynth,, can u guide me how to use this with avisynth,,, and how to use this in dos batch file...
StainlessS
31st August 2012, 21:05
GScript available here:-
http://forum.doom9.org/showthread.php?t=147846&highlight=gscript
To make a batch, you just need to have lines of the batch file broken up into string parts so that you can insert values
in between and join together into full line strings,
if more than one line batch file then, do the same with other lines and join together into a multiline string where each line
is separated by a linefeed [Chr(10)].
Then save the whole string to a file. Will take some work but assuming you can do a little Avisynth script, should not
be too difficult.
Good Luck.
EDIT: This function here by Vampiredom, could be VERY handy to do what you want.
http://forum.doom9.org/showthread.php?t=147846&highlight=gscript
Give me a little time and I'll show how you can use it.
StainlessS
1st September 2012, 00:04
Try This:
# EDIT To Suit
DIR = "D:\VID\" # Video Directory
INFILE = "*.AVI|MP*G" # Input file wildcards, AVI or MPG or MPEG
OUTDIR = "D:\OUT\" # Output and Temp Directory
TEMPLATE = "D:\TEMT\TEMPLATE_AVS.TEMT" # AVS file TEMPLATE, could be DOS, or whatever.
OUTLOG = "OUTLOG.TXT" # Crop log
RELATIVE = True # True show width as eg -8, False as eg 640
CROPCOORDS = 2 # 0=CropExact (RESIZE ONLY), 1=CropLess, 2=CropMore, 3=CropPlus
DEBUG = False # Show output in DebugView
SIZEX = 640 # Resize X size for AVS file
SIZEY = 480 # Resize Y size for AVS file
# ----------------------------------------------------------
Function StrReplace(string s,string find,string replace){ # repeated, string replacements
# http://forum.doom9.org/showthread.php?t=147846&highlight=gscript 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))}
# ----------------------------------------------------------
GSCript("""
LISTNAME = OUTDIR + "FILELIST.TXT"
Result=RT_WriteFileList(DIR+INFILE,LISTNAME) # Write list file of video file names
Assert((Result!=0), "No files found")
FILELIST=RT_ReadTxtFromFile(LISTNAME) # Read list of names
NFILES=RT_TxtQueryLines(FILELIST) # Number of files in list
TEMT=RT_ReadTxtFromFile(TEMPLATE) # Read Template avs/batch file.
LOGTXT="" # Init output log
For(i=0,NFILES-1) { # Step through list of file names
FN=RT_TxtGetLine(FILELIST,i) # The file name of video clip including full path
DirectshowSource(FN).ConvertToYV12() # Load file, Convert to whatever your required colorspace (affects cropping)
QBC=QueryBorderCrop(relative=RELATIVE,debug=DEBUG) # Inquire all modes cropping
Eval(QBC) # Set variables in Avisynth
COORDS=RT_TxtGetLine(QBC,CROPCOORDS) # The cropping mode that is required (2=CropMore)
T=TEMT # Make a copy of the Template avs/batch
# -----------------------------------------------------
# Now we need to Edit the variables in copy of template file
T=StrReplace(T,"[VideoFile]",FN) # Replace ALL occurences of video file name incl full path.
T=StrReplace(T,"[SIZEX]",String(SIZEX)) # Resize X dim
T=StrReplace(T,"[SIZEY]",String(SIZEY)) # Resize Y dim
T=StrReplace(T,"[CROPX]",String(QBCROPXM)) # Crop X, in this case using CropMore
T=StrReplace(T,"[CROPY]",String(QBCROPYM)) # Crop Y, in this case using CropMore
T=StrReplace(T,"[CROPW]",String(QBCROPWM)) # Crop W, in this case using CropMore
T=StrReplace(T,"[CROPH]",String(QBCROPHM)) # Crop H, in this case using CropMore
# -----------------------------------------------------
OFN = StrReplace(FN,DIR,"") # Out filename excluding Input dir path
RT_TxtWriteFile(T,OUTDIR+OFN+".AVS") # Write AVS/Batch file to OUTDIR
LOGTXT=RT_TxtAddStr(
\ LOGTXT,
\ String(i)+" ) "+FN,
\ "Cropping = "+COORDS,
\ "AVS File = "+OUTDIR+OFN+".AVS",
\ "",
\ T,
\ "---------------"
\ )
}
RT_TxtWriteFile(LOGTXT,OUTDIR+OUTLOG) # Write log file
""")
Return MessageClip (String(NFILES)+" Processed OK",width=320, height=200,shrink=false,text_color=$FFFF00,halo_color=$000000,bg_color=$5555FF)
FileList produced
D:\VID\1.mpg
D:\VID\Lexx-S02xE01-Mantrid.avi
D:\VID\Lexx-S02xE02-Terminal.avi
D:\VID\Lexx-S02xE03-Lyekka.avi
Outlog show all 4 Generated AVS files in one (I will not show them seperately, they are the same).
0 ) D:\VID\1.mpg
Cropping = QBCropXM=18 QBCropYM=0 QBCropWM=-18 QBCropHM=0
AVS File = D:\OUT\1.mpg.AVS
VideoFile="D:\VID\1.mpg"
DirectShowSource(VideoFile).ConvertToYV12().Trim(0,0)
Spline36Resize(640,480,18,0,-18,0)
Return Last
---------------
1 ) D:\VID\Lexx-S02xE01-Mantrid.avi
Cropping = QBCropXM=2 QBCropYM=0 QBCropWM=-2 QBCropHM=0
AVS File = D:\OUT\Lexx-S02xE01-Mantrid.avi.AVS
VideoFile="D:\VID\Lexx-S02xE01-Mantrid.avi"
DirectShowSource(VideoFile).ConvertToYV12().Trim(0,0)
Spline36Resize(640,480,2,0,-2,0)
Return Last
---------------
2 ) D:\VID\Lexx-S02xE02-Terminal.avi
Cropping = QBCropXM=2 QBCropYM=0 QBCropWM=-2 QBCropHM=0
AVS File = D:\OUT\Lexx-S02xE02-Terminal.avi.AVS
VideoFile="D:\VID\Lexx-S02xE02-Terminal.avi"
DirectShowSource(VideoFile).ConvertToYV12().Trim(0,0)
Spline36Resize(640,480,2,0,-2,0)
Return Last
---------------
3 ) D:\VID\Lexx-S02xE03-Lyekka.avi
Cropping = QBCropXM=6 QBCropYM=0 QBCropWM=-6 QBCropHM=-4
AVS File = D:\OUT\Lexx-S02xE03-Lyekka.avi.AVS
VideoFile="D:\VID\Lexx-S02xE03-Lyekka.avi"
DirectShowSource(VideoFile).ConvertToYV12().Trim(0,0)
Spline36Resize(640,480,6,0,-6,-4)
Return Last
---------------
Works pretty good. :)
EDIT: Oops, nearly forgot, here's the Template file, "TEMPLATE_AVS.TEMT"
VideoFile="[VideoFile]"
DirectShowSource(VideoFile).ConvertToYV12().Trim(0,0)
Spline36Resize([SIZEX],[SIZEY],[CROPX],[CROPY],[CROPW],[CROPH])
Return Last
gurusinghbrar
1st September 2012, 16:05
@stailless hey sory bro,, can't under stand how to use with avisynth please tel me to step to run this script with avs synth, i download the plugin and then install it in plugin and then "i dont know how to run this script in avisynth" :)
StainlessS
1st September 2012, 16:10
Right click avs, select "Open With" and select a media player (NOT VLC, as it does all decoding internally and does not understand
AVS), eg Windows Media Player, Gom, MediaPlayer Classic Home Cinema, or whatever. Or in VirtualDub or any other program
that loads AVI files.
gurusinghbrar
1st September 2012, 16:16
ok, let me explain, fist i install the plugin and then make a avs file and copy ur script in file.avs file, and then copy a avi video with in the same file.avs folder and then run the avs file in media player classic,, but it say error in line 58.
StainlessS
1st September 2012, 16:19
See the script before "# ----------------------------------------------------------"
EDIT that to wherever your files are etc.
DIR = "D:\VID\" # Video Directory
OUTDIR = "D:\OUT\" # Output and Temp Directory
TEMPLATE = "D:\TEMT\TEMPLATE_AVS.TEMT" # AVS file TEMPLATE, could be DOS, or whatever.
You also need the Template file too, TEMPLATE_AVS.TEMT
StainlessS
1st September 2012, 16:35
This works if you want everything in same directory
DIR = ".\" # Video Directory
INFILE = "*.AVI|MP*G" # Input file wildcards, AVI or MPG or MPEG
OUTDIR = ".\" # Output and Temp Directory
TEMPLATE = ".\TEMPLATE_AVS.TEMT" # AVS file TEMPLATE, could be DOS, or whatever.
".\" is current dir.
gurusinghbrar
1st September 2012, 16:42
ok now new error ...
my avs file content is ...
LoadPlugin("C:\Program Files\AviSynth 2.5\plugins\ffms2.dll")
LoadPlugin("C:\Program Files\AviSynth 2.5\plugins\AutoCrop.dll")
LoadPlugin("C:\Program Files\AviSynth 2.5\plugins\mt_masktools-25.dll")
LoadPlugin("C:\Program Files\AviSynth 2.5\plugins\mt_masktools-26.dll")
LoadPlugin("C:\Program Files\AviSynth 2.5\plugins\RemoveGrainSSE3.dll")
LoadPlugin("C:\Program Files\AviSynth 2.5\plugins\RepairSSE3.dll")
LoadPlugin("C:\Program Files\AviSynth 2.5\plugins\GScript.dll")
Import("C:\Program Files\AviSynth 2.5\plugins\LSFmod.avs")
Import("C:\Program Files\AviSynth 2.5\plugins\ResizeKAR.avs")
DIR = ".\" # Directory
INFILE = "*.AVI|MP*G" # input file wildcards
OUTDIR = ".\" # Output and Temp Directory
TEMPLATE = ".\TEMPLATE_AVS.TEMT" # AVS file TEMPLATE, could be DOS, or whatever.
OUTLOG = "OUTLOG.TXT" # Crop log
RELATIVE = True # True show width as eg -8, False as eg 640
CROPCOORDS = 2 # 0=CropExact (RESIZE ONLY), 1=CropLess, 2=CropMore, 3=CropPlus
DEBUG = False # Show output in DebugView
SIZEX = 640 # Resize X size for AVS file
SIZEY = 480 # Resize Y size for AVS file
# ----------------------------------------------------------
Function StrReplace(string s,string find,string replace){ # repeated, string replacements
# http://forum.doom9.org/showthread.php?t=147846&highlight=gscript 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))}
# ----------------------------------------------------------
GSCript("""
LISTNAME = OUTDIR + "FILELIST.TXT"
Result=RT_WriteFileList(DIR+INFILE,LISTNAME) # Write list file of video file names
Assert((Result!=0), "No files found")
FILELIST=RT_ReadTxtFromFile(LISTNAME) # Read list of names
NFILES=RT_TxtQueryLines(FILELIST) # Number of files in list
TEMT=RT_ReadTxtFromFile(TEMPLATE) # Read Template avs/batch file.
LOGTXT="" # Init output log
For(i=0,NFILES-1) { # Step through list of file names
FN=RT_TxtGetLine(FILELIST,i) # The file name of video clip including full path
DirectshowSource(FN).ConvertToYV12() # Load file, Convert to whatever your required colorspace (affects cropping)
QBC=QueryBorderCrop(relative=RELATIVE,debug=DEBUG) # Inquire all modes cropping
Eval(QBC) # Set variables in Avisynth
COORDS=RT_TxtGetLine(QBC,CROPCOORDS) # The cropping mode that is required (2=CropMore)
T=TEMT # Make a copy of the Template avs/batch
# -----------------------------------------------------
# Now we need to Edit the variables in copy of template file
T=StrReplace(T,"[VideoFile]",FN) # Replace ALL occurences of video file name incl full path.
T=StrReplace(T,"[SIZEX]",String(SIZEX)) # Resize X dim
T=StrReplace(T,"[SIZEY]",String(SIZEY)) # Resize Y dim
T=StrReplace(T,"[CROPX]",String(QBCROPXM)) # Crop X, in this case using CropMore
T=StrReplace(T,"[CROPY]",String(QBCROPYM)) # Crop Y, in this case using CropMore
T=StrReplace(T,"[CROPW]",String(QBCROPWM)) # Crop W, in this case using CropMore
T=StrReplace(T,"[CROPH]",String(QBCROPHM)) # Crop H, in this case using CropMore
# -----------------------------------------------------
OFN = StrReplace(FN,DIR,"") # Out filename excluding Input dir path
RT_TxtWriteFile(T,OUTDIR+OFN+".AVS") # Write AVS/Batch file to OUTDIR
LOGTXT=RT_TxtAddStr(
\ LOGTXT,
\ String(i)+" ) "+FN,
\ "Cropping = "+COORDS,
\ "AVS File = "+OUTDIR+OFN+".AVS",
\ "",
\ T,
\ "---------------"
\ )
}
RT_TxtWriteFile(LOGTXT,OUTDIR+OUTLOG) # Write log file
""")
Return MessageClip (String(NFILES)+" Processed OK",width=320, height=200,shrink=false,text_color=$FFFF00,halo_color=$000000,bg_color=$5555FF)
i make a folder and copy avi file in it and then create avs file above content and create TEMPLATE_AVS.TEMT with ur perameter and run avs file in media player say line
no function name "RT_Writefilelist" and line 70 error
check it out it say on """) error in avspmod
StainlessS
1st September 2012, 16:51
The only plugins you need are GScript and RT_Stats (EDIT v1.03).
RT_Stats.dll is missing and also has the QueryBorderCrop.avsi script.
import the RT_Stats plugin just like the others,
You can use:-
Import("QueryBorderCrop.avsi")
maybe after load plugin lines to import the script from CURRENT directory.
I dont use AVSPmod, and almost always just have my plugins in the Avisynth plugin directory,
also the QueryBorderCrop.avsi in plugins dir.
EDIT: Also, you load Avisynth v2.58 and Avisynth v2.6 versions of mt_tools, pick one or the other
not both, and only 26 if you are using Avisynth v2.6, suggest remove 26 as it causes problems
in scripts because it removed planar YUY2 support.
gurusinghbrar
1st September 2012, 16:58
ok,,, where i can download RT_Stats.dll
StainlessS
1st September 2012, 17:05
http://forum.doom9.org/showthread.php?t=165479
Also see previous EDIT
gurusinghbrar
1st September 2012, 17:18
ok, stainless,, my avisynth version is 2.5.8 only,,, and can i use RT_stats.avs file instead of RT_stats.dll and put it in avisynth plugin... if yes i also done that but now it show syntex error in RT_stats.avs, line 3 ,colum 5
StainlessS
1st September 2012, 17:24
I've no idea what you called the provided script, I called mine crop.avs.
You need the RT_Stats.dll, the avs uses it.
You also need the QueryborderCrop.avsi as already stated. (Comes with the dll).
StainlessS
1st September 2012, 17:27
As it says in RT_Stats thread, you can download from my mediafire account in my sig, see below in red.
gurusinghbrar
1st September 2012, 17:29
sorry for my english,,, its bad,,, ok i found the file.. RT_Stats.dll on net and replace it in same file where all files are placed.. and it say now nofile found.
gurusinghbrar
1st September 2012, 17:39
huurry,,, i have done it,,, thanks man the file log have out is created... my mistake was, i was using the old RT_stats.dll file but replace with new which download from your sig it works...
"i know i am "Pig" but i can dance very well,,, if u teach me,,,:) :) because no body is perfect from birth"
do u faver me one more tiime... can u help me more,,, how to these file value import in batch file for processing...
StainlessS
1st September 2012, 17:48
contents of directory (everything in same dir), Also put video files in same dir.
crop.avs
TEMPLATE_AVS.TEMT
QueryBorderCrop.AVSI
RT_Stats.dll
The template file
VideoFile="[VideoFile]"
DirectShowSource(VideoFile).ConvertToYV12().Trim(0,0)
Spline36Resize([SIZEX],[SIZEY],[CROPX],[CROPY],[CROPW],[CROPH])
Return Last
The crop.avs file
LoadPlugin("RT_Stats.dll")
Import("QueryBorderCrop.avsi")
# EDIT To Suit
DIR = ".\" # Video Directory
INFILE = "*.AVI|MP*G" # Input file wildcards, AVI or MPG or MPEG
OUTDIR = ".\" # Output and Temp Directory
TEMPLATE = ".\TEMPLATE_AVS.TEMT" # AVS file TEMPLATE, could be DOS, or whatever.
OUTLOG = "OUTLOG.TXT" # Crop log
RELATIVE = True # True show width as eg -8, False as eg 640
CROPCOORDS = 2 # 0=CropExact (RESIZE ONLY), 1=CropLess, 2=CropMore, 3=CropPlus
DEBUG = False # Show output in DebugView
SIZEX = 640 # Resize X size for AVS file
SIZEY = 480 # Resize Y size for AVS file
# ----------------------------------------------------------
Function StrReplace(string s,string find,string replace){ # repeated, string replacements
# http://forum.doom9.org/showthread.php?t=147846&highlight=gscript
i=s.FindStr(find) return(i==0?s:s.LeftStr(i-1)+replace+s.MidStr(Strlen(find)+i).StrReplace(find,replace))}
# ----------------------------------------------------------
GSCript("""
LISTNAME = OUTDIR + "FILELIST.TXT"
Result=RT_WriteFileList(DIR+INFILE,LISTNAME) # Write list file of video file names
Assert((Result!=0), "No files found")
FILELIST=RT_ReadTxtFromFile(LISTNAME) # Read list of names
NFILES=RT_TxtQueryLines(FILELIST) # Number of files in list
TEMT=RT_ReadTxtFromFile(TEMPLATE) # Read Template avs/batch file.
LOGTXT="" # Init output log
For(i=0,NFILES-1) { # Step through list of file names
FN=RT_TxtGetLine(FILELIST,i) # The file name of video clip including full path
DirectshowSource(FN).ConvertToYV12() # Load file, Convert to whatever your required colorspace (affects cropping)
QBC=QueryBorderCrop(relative=RELATIVE,debug=DEBUG) # Inquire all modes cropping
Eval(QBC) # Set variables in Avisynth
COORDS=RT_TxtGetLine(QBC,CROPCOORDS) # The cropping mode that is required (2=CropMore)
T=TEMT # Make a copy of the Template avs/batch
# -----------------------------------------------------
# Now we need to Edit the variables in copy of template file
T=StrReplace(T,"[VideoFile]",FN) # Replace ALL occurences of video file name incl full path.
T=StrReplace(T,"[SIZEX]",String(SIZEX)) # Resize X dim
T=StrReplace(T,"[SIZEY]",String(SIZEY)) # Resize Y dim
T=StrReplace(T,"[CROPX]",String(QBCROPXM)) # Crop X, in the case using CropMore
T=StrReplace(T,"[CROPY]",String(QBCROPYM)) # Crop Y, in the case using CropMore
T=StrReplace(T,"[CROPW]",String(QBCROPWM)) # Crop W, in the case using CropMore
T=StrReplace(T,"[CROPH]",String(QBCROPHM)) # Crop H, in the case using CropMore
# -----------------------------------------------------
OFN = StrReplace(FN,DIR,"") # Out filename excluding Input dir path
RT_TxtWriteFile(T,OUTDIR+OFN+".AVS") # Write AVS/Batch file to OUTDIR
LOGTXT=RT_TxtAddStr(
\ LOGTXT,
\ String(i)+" ) "+FN,
\ "Cropping = "+COORDS,
\ "AVS File = "+OUTDIR+OFN+".AVS",
\ "",
\ T,
\ "---------------"
\ )
}
RT_TxtWriteFile(LOGTXT,OUTDIR+OUTLOG) # Write log file
""")
Return MessageClip (String(NFILES)+" Processed OK",width=320, height=200,shrink=false,text_color=$FFFF00,halo_color=$000000,bg_color=$5555FF)
Right click crop.avs and OPEN with "Windows Media Player"
Make sure you have removed RT_Stats, QueryBorderCrop.avsi from plugins dir first.
gurusinghbrar
1st September 2012, 17:51
thanks man,,, thanks a lot its work,, check it from me... don't take it seriously... "http://forum.doom9.org/showthread.php?p=1589450#post1589450" hehehe
StainlessS
1st September 2012, 17:58
You would need to create your template batch file using [VARNAME] style variables that need replaced
Dir [DOSDIR]
and modify the supplied script to edit them, ie this part:
# -----------------------------------------------------
# Now we need to Edit the variables in copy of template file
T=StrReplace(T,"[DOSDIR]",FN) # Replace ALL occurences of [DOSDIR] file name incl full path.
# -----------------------------------------------------
That should be enough for you to figure it out, I'm no expert in dos or ffmpeg usage,, you'll need to figure that out
yourself or get someone else to assist, good luck. :)
gurusinghbrar
1st September 2012, 18:01
hey i check ur work,,, u lot of knowledge about auto it,, can't you make it in Autoit script software and make it these all thing in GUI for new bee like can work easy,.. :)
StainlessS
1st September 2012, 18:08
Expert, nah, not me. I've only written two programs in AutoIt.
Maybe you would like to have a go, looking forward to using your AutoIt program :)
I use MeGUI, its easier than command line, and using a wheel that someone else
invented is easier than re-inventing the wheel again.
EDIT: Yes, I saw your other post, we crossed over posts a few times.
gurusinghbrar
1st September 2012, 18:17
hey stainless this ur script it change the video resolution to 640 480 but if i don't want this size, i want like resize "ResizeKAR" script it resize the video according to input like (480, 272) and keep aspect ratio of video and output will be base on video aspect ratio... ie i have a video size (1280x720) with blackborder and i use ResizeKAR(480, 272, NoBorders=Ture).AutoCrop(mode=0, Samples=20) function and the out put video size is (480x198) so can this possible in ur script.. i know its possible... plz tell me how to do this...
StainlessS
1st September 2012, 18:23
What is your output format, mp4,mkv ?
Would MeGUI be OK ?
gurusinghbrar
1st September 2012, 18:24
mkv or mp4 both... but more time download movie like MKV...
StainlessS
1st September 2012, 18:26
OK, give me a little time.
gurusinghbrar
1st September 2012, 18:28
hey this little script also done that job...
function ResizeMaintainAspectWidth(clip c,int newwidth,string resizefn) {
nh=floor(eval(string(c.height)+". * "+string(newwidth)+". / "+string(c.width)+"."))
return eval(resizefn+"(c,"+string(newwidth)+","+string(nh)+")")
}
ResizeKAR script link is "http://forum.doom9.org/showthread.php?t=135229"
i think may be it help u... try this
gurusinghbrar
1st September 2012, 18:31
yes i use always use MeGUI.. but they do that job which is predefine or use predefine parameter if some one want more they have to look beyond the megui,,, i know the MeGui is great application,,,
StainlessS
2nd September 2012, 02:15
Still working, dont despare.
StainlessS
2nd September 2012, 13:35
This seems to work ok, (only implemented AVI and Mpeg in wildcard, change as desired.
# Need GScript and RT_Stats v1.03 minimum, with QueryborderCrop and QueryLumaMinMax from RT_Stats v1.03.
# EDIT To Suit
DIR = "D:\VID\" # Video Input Directory
OUTDIR = "D:\OUT\" # Output and Temp Directory
TEMPLATE = "D:\TEMT\TEMPLATE_AVS.TEMT" # AVS file TEMPLATE, could be DOS, or whatever.
INFILE = "*.AVI|MP*G" # Input file wildcards, AVI or MPG or MPEG
OUTLOG = "OUTLOG.TXT" # Crop log
CROPCOORDS = 2 # 0=CropExact (RESIZE ONLY), 1=CropLess, 2=CropMore, 3=CropPlus
RELATIVE = True # True uses eg QBCropW=-8, False == QBCropW=640
AUTOLEVEL_STRENGTH = 1.0 # AutoLevel Strength 0.0 -> 1.0, 0.0 == OFF
#
# SIZE X/Y, If both 0 then JUST CROP
# If ONLY one dimenson (X or Y) == 0, is resized to 1:1 pixel Aspect ratio, ie YouTube etc or other device needing 1:1 pixel aspect ratio.
# If both non 0, then resized to given dimensions and MeGUI sets aspect ratio, (Needs player that conforms with non 1:1 pixel aspect ratio).
SIZEX = 640
SIZEY = 0
#
# NOTE, AVI does not have an Aspect ratio flag, Mpeg Does but we dont know what it is here, all resize etc is based on FRAME aspect ratio.
# Something for you to do, ie get aspect ratio from video clip using eg MediaInfo via command line.
# ----------------------------------------------------------
GSCript("""
AUTOLEVEL=(AUTOLEVEL_STRENGTH>0.0)
LISTNAME = OUTDIR + "FILELIST.TXT"
Result=RT_WriteFileList(DIR+INFILE,LISTNAME) # Write list file of video file names
Assert((Result!=0), "No files found")
FILELIST=RT_ReadTxtFromFile(LISTNAME) # Read list of names
NFILES=RT_TxtQueryLines(FILELIST) # Number of files in list
TEMT=RT_ReadTxtFromFile(TEMPLATE) # Read Template avs/batch file.
LOGTXT="" # Init output log
For(i=0,NFILES-1) { # Step through list of file names
FN=RT_TxtGetLine(FILELIST,i) # The file name of video clip including full path
DirectshowSource(FN).ConvertToYV12() # Load file, Convert to whatever your required colorspace (affects cropping)
QBC=QueryBorderCrop(relative=RELATIVE) # Inquire all modes cropping
Eval(QBC) # Set variables in Avisynth
COORDS=RT_TxtGetLine(QBC,CROPCOORDS) # The cropping mode that is required (2=CropMore)
if(AUTOLEVEL) {
ALTMPC=Crop(QBCropXP,QBCropYP,QBCropWP,QBCropHP) # Crop TEMP with CropPlus coords (ignore black borders totally)
QLMM = ALTMPC.QueryLumaMinMax()
Eval(QLMM)
ALMin = Int(16 - ((16 - QLMMMin) * AUTOLEVEL_STRENGTH) + 0.5) # Round Up
ALMax = Int(235 - ((235 - QLMMMax) * AUTOLEVEL_STRENGTH)) # Round down
} else {
ALMin=16
ALMin=235
}
QBCTrailer=Select(CROPCOORDS,"=","L=","M=","P=")
COORDS=StrReplace(COORDS,QBCTrailer,"=") # Change all to style without a trailer eg QBCropXM to QBCropX.
COORDS=StrReplace(COORDS,"QBC","C") # Change all style QBCropX to CropX
Eval(COORDS) # Set variables in Avisynth, ie CropX etc
T=TEMT # Make a copy of the Template avs/batch
DAR = Float(Width) / Height # Here We do not know what real DAR is, DAR is purely based on FAR, frame aspect ratio. Something for you to do
# -----------------------------------------------------
# Now we need to Edit the variables in copy of template file
T=StrReplace(T,"[VIDEOFILE]",FN) # Replace ALL occurences of video file name incl full path.
T=StrReplace(T,"[SIZEX]",String(SIZEX)) # Resize X dim
T=StrReplace(T,"[SIZEY]",String(SIZEY)) # Resize Y dim
T=StrReplace(T,"[CROPX]",String(CropX)) # Crop X, using either CropLess, CropMore, or CropPlus
T=StrReplace(T,"[CROPY]",String(CropY)) # Crop Y
T=StrReplace(T,"[CROPW]",String(CropW)) # Crop W
T=StrReplace(T,"[CROPH]",String(CropH)) # Crop H
T=StrReplace(T,"[AUTOLEVEL]",String(AUTOLEVEL)) # True/False
T=StrReplace(T,"[ALMIN]",String(ALMin)) # Auto Level Min
T=StrReplace(T,"[ALMAX]",String(ALMax)) # Auto Level Max
T=StrReplace(T,"[DAR]",String(DAR)) # Full frame DAR
# -----------------------------------------------------
OFN = StrReplace(FN,DIR,"") # Out filename excluding Input dir path
RT_TxtWriteFile(T,OUTDIR+OFN+".AVS") # Write AVS/Batch file to OUTDIR
LOGTXT=RT_TxtAddStr(
\ LOGTXT,
\ String(i)+" ) "+FN,
\ "Cropping = "+COORDS,
\ "AVS File = "+OUTDIR+OFN+".AVS",
\ "",
\ T,
\ "---------------"
\ )
} # End For
RT_TxtWriteFile(LOGTXT,OUTDIR+OUTLOG) # Write log file
""")
Return MessageClip (String(NFILES)+" Processed OK",width=320, height=200,shrink=false,text_color=$FFFF00,halo_color=$000000,bg_color=$5555FF)
##############################################
Function StrReplace(string s,string find,string replace){ # repeated, string replacements
# http://forum.doom9.org/showthread.php?t=147846&highlight=gscript 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))}
And the template file TEMPLATE_AVS.TEMT
VideoFile="[VIDEOFILE]"
SizeX=[SIZEX]
SizeY=[SIZEY]
CropX=[CROPX]
CropY=[CROPY]
CropW=[CROPW]
CropH=[CROPH]
AutoLevel=[AUTOLEVEL]
ALMin=[ALMIN]
ALMax=[ALMAX]
DAR=[DAR] # Full Frame DAR
####################################
DirectShowSource(VideoFile).ConvertToYV12().Trim(0,0)
(AutoLevel) ? Levels(ALMin,1.0,ALMax,16,235,Coring=False) : NOP
DAR=GetCropDAR(DAR,CropX,CropY,CropW,CropH) # Get DAR of Image part after cropping.
Crop(CropX,CropY,CropW,CropH)
GScript("""
if(SizeX <= 0 && SizeY <= 0) { # CROPPING ONLY
# KEEPS the same SAR (Sample ie pixel Aspect Ratio)
# DAR changed because the full frame AR (FAR) changed, ie borders were chopped
# MeGUI will encode with same SAR (pixel) aspect ratio as source was.
# Nothing to do here.
} else { # Resize X,Y, ...
if(SizeX>0 && SizeY <=0) { # Resize by X dim. Y Will be resized to maintain image DAR with 1:1 SAR
SizeX = ((SizeX + 2) / 4) * 4 # Make sure X dim rounded to nearest multiple of 4
SizeY = Int(SizeX / DAR + 1.0) / 2 * 2 # Resize, make sure Y dim rounded to nearest multiple of 2
DAR = Float(SizeX)/SizeY # Sample aspect ratio ~ 1:1
} else if(SizeX<=0 && SizeY > 0) { # Resize by Y dim. X Will be resized to maintain image DAR with 1:1 SAR
SizeY = ((SizeY + 1) / 2) * 2 # Make sure Y dim rounded to nearest multiple of 2
SizeX = Int(SizeY * DAR + 2.0) / 4 * 4 # Resize, make sure X dim rounded to nearest multiple of 4
DAR = Float(SizeX)/SizeY # Sample aspect ratio ~ 1:1
} else { # Resize BOTH X and Y, MeGUI will encode according to DAR variable.
# Nothing to do here. Not for Tube sites nor devices requiring 1:1 SAR ie pixel aspect ratio.
}
Spline36Resize(SizeX,SizeY)
}
""")
SignalDAR(DAR) # Tell MeGUI the new Resized/Cropped Display Aspect Ratio
return last
####################################
Function GetCropDAR(clip c,float DAR,float "X",float "Y",float "W",float "H") {
# Call prior to Crop/Resize with (possibly fractional) cropping to calc resultant DAR, X,Y,W,H are cropping coords
# DAR = FAR * SAR ::: FAR = DAR / SAR ::: SAR = DAR / FAR
#
X=Float(Default(X,0.0)) Y=Float(Default(Y,0.0)) W=Float(Default(W,0.0)) H=Float(Default(H,0.0))
W=W<=0.0?c.width+W-X:W H=H<=0.0?c.height+H-Y:H
# Irrespective of what various resizers in various Avisynth versions silently correct, we dont allow eg -ve X
Assert(X>=0.0&&X < c.width, "GetCropDAR: Invalid X("+String(X)+")")
Assert(Y>=0.0&&Y < c.height,"GetCropDAR: Invalid Y("+String(Y)+")")
Assert(W> 0.0&&X+W<=c.width, "GetCropDAR: Invalid W("+String(W)+")")
Assert(H> 0.0&&Y+H<=c.height,"GetCropDAR: Invalid H("+String(H)+")")
Return c.GetSAR(DAR) * W / H
}
# From MeGUI Wiki:
Function GetDAR(clip c, float SAR) { return Float(c.width) * SAR / Float(c.height)} # Gets the DAR from the SAR
Function GetSAR(clip c, float DAR) { return DAR * Float(c.height) / Float(c.width) } # Gets the SAR from the DAR
Function SignalDAR(float DAR){global MeGUI_darx=Round(1000*DAR) global MeGUI_dary=1000} # Signal DAR for MEGUI (Name change from SetDar)
You can set SIZEX,SIZEY to Just Crop, resize by 1 dim and other auto resized to 1:1 pixel aspect ratio, or specific size and MeGUI sets DAR.
EDIT: Also added AutoLevels with variable 'strength'.
gurusinghbrar
2nd September 2012, 17:05
hey @stainless this is perfect script and working with some need changes in created (output) avs file just manually add these thing in created avs file via Template file...
add these lines to output avs file..
LoadPlugin("F:\gscript test\GScript.dll")
LoadPlugin("F:\gscript test\RT_Stats.dll")
Import("QueryBorderCrop.avsi")
Import("QueryLumaMinMAx.Avsi")
it not auto include in output.avs file... and without these line output.avs file not play in mediaplayer.. i just manually add in avs file and it seem working...
plz include these file in TEMPLATE_AVS.TEMT and auto create in output.avs if possible...
thankx @stainlessS
StainlessS
2nd September 2012, 17:21
You include them in template, I have them in my plugins directory and dont need them in there again.
Also, Only GScript used in template, RT_Stats and others not used.
gurusinghbrar
2nd September 2012, 17:34
ok,, its optional but i want to play output.avs file before converting... but it not play in media player without these line added. its ok,, i add maually in template file, and no need to add again..
thanks bro for help me...
hey can i add any line for avisynth like windows path for filter. just drop plugin and filter avs file in plugin folder and no need to add them in avs file for play... :)
StainlessS
2nd September 2012, 17:42
You can do what you want.
Recommend that you have GScript in your Avisynth plugins directory all of the time, it conflicts with
nothing and is always handy to have.
I only manually load awkward plugins with awkward scripts where necessary, plugins that I use all of the time
are in my plugins directory and dont need any special loading.
gurusinghbrar
2nd September 2012, 17:50
ok,,, i added them to template and working fine... greate job @StainlessS,,, appreciate ur work....
by the today i find new more thing about cropping with Mplayer script... just look @this
Code:----
for %%I in (*.mp4) do mplayer -vf cropdetect -ao null -vo null -ss 30 -frames 5 %%I | find "-vf crop=" > crop
FOR /F "tokens=3 delims=(=)" %%G IN ('type "crop"') DO SET crop=%%G
echo %crop%
make run.bat file with the coding above and put it in a folder and also put mplayer exe and your videofile in same folder and then run the run.bat and see output crop value..
StainlessS
2nd September 2012, 18:00
Which mplayer is that ?
gurusinghbrar
2nd September 2012, 18:04
mplayerhq a command line media player wich play all file with single exe via commnad line it was basically for linux but now available for window also.. site is http://oss.netfarm.it/mplayer-win32.php check it out,,,
StainlessS
2nd September 2012, 18:39
Downloading, thanks.
StainlessS
2nd September 2012, 23:06
Minor mod
# Need GScript and RT_Stats v1.03 minimum, with QueryborderCrop and QueryLumaMinMax from RT_Stats v1.03.
# EDIT To Suit
DIR = "D:\VID\" # Video Input Directory
OUTDIR = "D:\OUT\" # Output and Temp Directory
TEMPLATE = "D:\TEMT\TEMPLATE_AVS.TEMT" # AVS file TEMPLATE, could be DOS, or whatever.
INFILE = "*.AVI|MP*G" # Input file wildcards, AVI or MPG or MPEG
OUTLOG = "OUTLOG.TXT" # Crop log
MEDIAINFO = "D:\TEST\MediaInfo.Exe" #
CROPCOORDS = 2 # 0=CropExact (RESIZE ONLY), 1=CropLess, 2=CropMore, 3=CropPlus
RELATIVE = True # True uses eg QBCropW=-8, False == QBCropW=640
AUTOLEVEL_STRENGTH = 1.0 # AutoLevel Strength 0.0 -> 1.0, 0.0 == OFF
#
# SIZE X/Y, If both 0 then JUST CROP
# If ONLY one dimenson (X or Y) == 0, is resized to 1:1 pixel Aspect ratio, ie YouTube etc or other device needing 1:1 pixel aspect ratio.
# If both non 0, then resized to given dimensions and MeGUI sets aspect ratio, (Needs player that conforms with non 1:1 pixel aspect ratio).
SIZEX = 640
SIZEY = 480
#
# NOTE, AVI does not have an Aspect ratio flag, Mpeg Does but we dont know what it is here, all resize etc is based on FRAME aspect ratio.
# Something for you to do, ie get aspect ratio from video clip using eg MediaInfo via command line.
# ----------------------------------------------------------
GSCript("""
AUTOLEVEL=(AUTOLEVEL_STRENGTH>0.0)
LISTNAME = OUTDIR + "FILELIST.TXT"
Result=RT_WriteFileList(DIR+INFILE,LISTNAME) # Write list file of video file names
Assert((Result!=0), "No files found")
FILELIST=RT_ReadTxtFromFile(LISTNAME) # Read list of names
NFILES=RT_TxtQueryLines(FILELIST) # Number of files in list
TEMT=RT_ReadTxtFromFile(TEMPLATE) # Read Template avs/batch file.
LOGTXT="" # Init output log
For(i=0,NFILES-1) { # Step through list of file names
FN=RT_TxtGetLine(FILELIST,i) # The file name of video clip including full path
DirectshowSource(FN).ConvertToYV12() # Load file, Convert to whatever your required colorspace (affects cropping)
QBC=QueryBorderCrop(relative=RELATIVE) # Inquire all modes cropping
Eval(QBC) # Set variables in Avisynth
COORDS=RT_TxtGetLine(QBC,CROPCOORDS) # The cropping mode that is required (2=CropMore)
if(AUTOLEVEL) {
ALTMPC=Crop(QBCropXP,QBCropYP,QBCropWP,QBCropHP) # Crop TEMP with CropPlus coords (ignore black borders totally)
QLMM = ALTMPC.QueryLumaMinMax()
Eval(QLMM)
ALMin = Int(16 - ((16 - QLMMMin) * AUTOLEVEL_STRENGTH) + 0.5) # Round Up
ALMax = Int(235 - ((235 - QLMMMax) * AUTOLEVEL_STRENGTH)) # Round down
} else {
ALMin=16
ALMin=235
}
QBCTrailer=Select(CROPCOORDS,"=","L=","M=","P=")
COORDS=StrReplace(COORDS,QBCTrailer,"=") # Change all to style without a trailer eg QBCropXM to QBCropX.
COORDS=StrReplace(COORDS,"QBC","C") # Change all style QBCropX to CropX
Eval(COORDS) # Set variables in Avisynth, ie CropX etc
T=TEMT # Make a copy of the Template avs/batch
DAR = Float(Width) / Height # Here We do not know what real DAR is, DAR is purely based on FAR, frame aspect ratio. Something for you to do
# -----------------------------------------------------
# Now we need to Edit the variables in copy of template file
T=StrReplace(T,"[VIDEOFILE]",FN) # Replace ALL occurences of video file name incl full path.
T=StrReplace(T,"[SIZEX]",String(SIZEX)) # Resize X dim
T=StrReplace(T,"[SIZEY]",String(SIZEY)) # Resize Y dim
T=StrReplace(T,"[CROPX]",String(CropX)) # Crop X, using either CropLess, CropMore, or CropPlus
T=StrReplace(T,"[CROPY]",String(CropY)) # Crop Y
T=StrReplace(T,"[CROPW]",String(CropW)) # Crop W
T=StrReplace(T,"[CROPH]",String(CropH)) # Crop H
T=StrReplace(T,"[AUTOLEVEL]",String(AUTOLEVEL)) # True/False
T=StrReplace(T,"[ALMIN]",String(ALMin)) # Auto Level Min
T=StrReplace(T,"[ALMAX]",String(ALMax)) # Auto Level Max
T=StrReplace(T,"[DAR]",String(DAR)) # Full frame DAR
# -----------------------------------------------------
OFN = StrReplace(FN,DIR,"") # Out filename excluding Input dir path
RT_TxtWriteFile(T,OUTDIR+OFN+".AVS") # Write AVS/Batch file to OUTDIR
LOGTXT=RT_TxtAddStr(
\ LOGTXT,
\ String(i)+" ) "+FN,
\ "Cropping = "+COORDS,
\ "AVS File = "+OUTDIR+OFN+".AVS",
\ "",
\ T,
\ "---------------"
\ )
} # End For
RT_TxtWriteFile(LOGTXT,OUTDIR+OUTLOG) # Write log file
""")
Return MessageClip (String(NFILES)+" Processed OK",width=320, height=200,shrink=false,text_color=$FFFF00,halo_color=$000000,bg_color=$5555FF)
##############################################
Function StrReplace(string s,string find,string replace){ # repeated, string replacements
# http://forum.doom9.org/showthread.php?t=147846&highlight=gscript 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))}
Removed dependence on GScript() in Template
VideoFile="[VIDEOFILE]"
SizeX=[SIZEX]
SizeY=[SIZEY]
CropX=[CROPX]
CropY=[CROPY]
CropW=[CROPW]
CropH=[CROPH]
AutoLevel=[AUTOLEVEL]
ALMin=[ALMIN]
ALMax=[ALMAX]
DAR=[DAR] # Full Frame DAR
####################################
DirectShowSource(VideoFile).ConvertToYV12().Trim(0,0)
(AutoLevel) ? Levels(ALMin,1.0,ALMax,16,235,Coring=False) : NOP
DAR=GetCropDAR(DAR,CropX,CropY,CropW,CropH) # Get DAR of Image part after cropping.
Crop(CropX,CropY,CropW,CropH)
(SizeX<=0&&SizeY<=0) ? Eval("""
# KEEPS the same SAR (Sample ie pixel Aspect Ratio)
# DAR changed because the full frame AR (FAR) changed, ie borders were chopped
# MeGUI will encode with same SAR (pixel) aspect ratio as source was.
# Nothing to do here.
""") : \
(SizeX>0&&SizeY<=0) ? Eval("""
# Resize by X dim. Y Will be resized to maintain image DAR with 1:1 SAR
# Make sure X dim rounded to nearest multiple of 4
# Resize, make sure Y dim rounded to nearest multiple of 2
# Sample aspect ratio ~ 1:1
SizeX = ((SizeX + 2) / 4) * 4
SizeY = Int(SizeX / DAR + 1.0) / 2 * 2
DAR = Float(SizeX)/SizeY
Spline36Resize(SizeX,SizeY)
""") : \
(SizeX<=0&&SizeY>0) ? Eval("""
# Resize by Y dim. X Will be resized to maintain image DAR with 1:1 SAR
# Make sure Y dim rounded to nearest multiple of 2
# Resize, make sure X dim rounded to nearest multiple of 4
# Sample aspect ratio ~ 1:1
SizeY = ((SizeY + 1) / 2) * 2
SizeX = Int(SizeY * DAR + 2.0) / 4 * 4
DAR = Float(SizeX)/SizeY
Spline36Resize(SizeX,SizeY)
""") : \
Eval("""
# Not for Tube sites nor devices requiring 1:1 SAR ie pixel aspect ratio.
Spline36Resize(SizeX,SizeY)
""")
SignalDAR(DAR) # Tell MeGUI the new Resized/Cropped Display Aspect Ratio
return last
####################################
Function GetCropDAR(clip c,float DAR,float "X",float "Y",float "W",float "H") {
# Call prior to Crop/Resize with (possibly fractional) cropping to calc resultant DAR, X,Y,W,H are cropping coords
# DAR = FAR * SAR ::: FAR = DAR / SAR ::: SAR = DAR / FAR
#
X=Float(Default(X,0.0)) Y=Float(Default(Y,0.0)) W=Float(Default(W,0.0)) H=Float(Default(H,0.0))
W=W<=0.0?c.width+W-X:W H=H<=0.0?c.height+H-Y:H
# Irrespective of what various resizers in various Avisynth versions silently correct, we dont allow eg -ve X
Assert(X>=0.0&&X < c.width, "GetCropDAR: Invalid X("+String(X)+")")
Assert(Y>=0.0&&Y < c.height,"GetCropDAR: Invalid Y("+String(Y)+")")
Assert(W> 0.0&&X+W<=c.width, "GetCropDAR: Invalid W("+String(W)+")")
Assert(H> 0.0&&Y+H<=c.height,"GetCropDAR: Invalid H("+String(H)+")")
Return c.GetSAR(DAR) * W / H
}
# From MeGUI Wiki:
Function GetDAR(clip c, float SAR) { return Float(c.width) * SAR / Float(c.height)} # Gets the DAR from the SAR
Function GetSAR(clip c, float DAR) { return DAR * Float(c.height) / Float(c.width) } # Gets the SAR from the DAR
Function SignalDAR(float DAR){global MeGUI_darx=Round(1000*DAR) global MeGUI_dary=1000} # Signal DAR for MEGUI (Name change from SetDar)
StainlessS
3rd September 2012, 23:34
Update, needs new RT_Stats v1.04, Implemented RT_call() function to call MediaInfo to get Aspect Ratio of source clips.
# Need GScript and RT_Stats v1.04 minimum, with QueryborderCrop and QueryLumaMinMax from RT_Stats v1.03+.
# Requires MediaInfo CLI version.
# Script Demo to create a batch of AVS scripts, with AutoCrop and AutoLevels from a directory of video clips (AVI or Mpeg).
# Signals MeGUI with the new cropped/resize Aspect Ratio.
# Could be converted to create batch of DOS based or other batch files (with different template) .
# EDIT To Suit
DIR = "D:\VID\" # Video Input Directory
OUTDIR = "D:\OUT\" # Output and Temp Directory
TEMPLATE = "D:\TEMT\TEMPLATE_AVS.TEMT" # AVS file TEMPLATE, could be DOS, or whatever.
INFILE = "*.AVI|MP*G" # Input file wildcards, AVI or MPG or MPEG
OUTLOG = "OUTLOG.TXT" # Crop log
MEDIAINFO = "D:\TEST\MediaInfo.Exe" # Path to MediaInfo.Exe CLI version
CROPCOORDS = 2 # 0=CropExact (RESIZE ONLY), 1=CropLess, 2=CropMore, 3=CropPlus
RELATIVE = True # True uses eg QBCropW=-8, False == QBCropW=640
AUTOLEVEL_STRENGTH = 0.666 # AutoLevel Strength 0.0 -> 1.0, 0.0 == OFF
HIDE = True # Hide MediaInfo Console window
WRITELOG = 1 # Write logging file to outdir, 0=NONE, 1=Small, 2=Full
#
# SIZE X/Y, If both 0 then JUST CROP
# If ONLY one dimenson (X or Y) == 0, is resized to 1:1 pixel Aspect ratio, ie YouTube etc or other device needing 1:1 pixel aspect ratio.
# If both non 0, then resized to given dimensions and MeGUI sets aspect ratio, (Needs player that conforms with non 1:1 pixel aspect ratio).
SIZEX = 640
SIZEY = 480
#
# ----------------------------------------------------------
GSCript("""
AUTOLEVEL=(AUTOLEVEL_STRENGTH>0.0)
LISTNAME = OUTDIR + "FILELIST.TXT"
Result=RT_WriteFileList(DIR+INFILE,LISTNAME) # Write list file of video file names
Assert((Result!=0), "No files found")
FILELIST=RT_ReadTxtFromFile(LISTNAME) # Read list of names
NFILES=RT_TxtQueryLines(FILELIST) # Number of files in list
TEMT=RT_ReadTxtFromFile(TEMPLATE) # Read Template avs/batch file.
LOGTXT="" # Init output log
For(i=0,NFILES-1) { # Step through list of file names
FN=RT_TxtGetLine(FILELIST,i) # The file name of video clip including full path
DirectshowSource(FN).ConvertToYV12() # Load file, Convert to whatever your required colorspace (affects cropping)
QBC=QueryBorderCrop(relative=RELATIVE) # Inquire all modes cropping
Eval(QBC) # Set variables in Avisynth
COORDS=RT_TxtGetLine(QBC,CROPCOORDS) # The cropping mode that is required (2=CropMore)
if(AUTOLEVEL) {
ALTMPC=Crop(QBCropXP,QBCropYP,QBCropWP,QBCropHP) # Crop TEMP with CropPlus coords (ignore black borders totally)
QLMM = ALTMPC.QueryLumaMinMax()
Eval(QLMM)
ALMin = Int(16 - ((16 - QLMMMin) * AUTOLEVEL_STRENGTH) + 0.5) # Round Up
ALMax = Int(235 - ((235 - QLMMMax) * AUTOLEVEL_STRENGTH)) # Round down
} else {
ALMin=16
ALMin=235
}
QBCTrailer=Select(CROPCOORDS,"=","L=","M=","P=")
COORDS=StrReplace(COORDS,QBCTrailer,"=") # Change all to style without a trailer eg QBCropXM to QBCropX.
COORDS=StrReplace(COORDS,"QBC","C") # Change all style QBCropX to CropX
Eval(COORDS) # Set variables in Avisynth, ie CropX etc
T=TEMT # Make a copy of the Template avs/batch
MITXT = OUTDIR+"MEDINFO.TXT"
MI = QuoteStr(MEDIAINFO) + " --LogFile="+ QuoteStr(MITXT) + " --Output=Video;%AspectRatio% " + QuoteStr(FN)
Assert(RT_Call(MI,Hide=True)==0,"Problem Calling MediaInfo" + Chr(10)+MI) # MediaInfo Command line version.
MDAR=RT_ReadTxtFromFile(MITXT,0)
DAR=Value(MDAR)
# -----------------------------------------------------
# Now we need to Edit the variables in copy of template file
T=StrReplace(T,"[VIDEOFILE]",FN) # Replace ALL occurences of video file name incl full path.
T=StrReplace(T,"[SIZEX]",String(SIZEX)) # Resize X dim
T=StrReplace(T,"[SIZEY]",String(SIZEY)) # Resize Y dim
T=StrReplace(T,"[CROPX]",String(CropX)) # Crop X, using either CropLess, CropMore, or CropPlus
T=StrReplace(T,"[CROPY]",String(CropY)) # Crop Y
T=StrReplace(T,"[CROPW]",String(CropW)) # Crop W
T=StrReplace(T,"[CROPH]",String(CropH)) # Crop H
T=StrReplace(T,"[AUTOLEVEL]",String(AUTOLEVEL&&(ALMin!=16||ALMax!=235))) # True/False
T=StrReplace(T,"[ALMIN]",String(ALMin)) # Auto Level Min
T=StrReplace(T,"[ALMAX]",String(ALMax)) # Auto Level Max
T=StrReplace(T,"[DAR]",String(DAR)) # Full frame DAR
# -----------------------------------------------------
OFN = StrReplace(FN,DIR,"") # Out filename excluding Input dir path
RT_TxtWriteFile(T,OUTDIR+OFN+".AVS") # Write AVS/Batch file to OUTDIR
if(WRITELOG>0) {
if(WRITELOG==1) {
T2 = RT_ReadTxtFromFile(OUTDIR+OFN+".AVS" ,Lines=11,Start=2)
LOGTXT=RT_TxtAddStr(LOGTXT,T2,"##########################")
} else {
LOGTXT=RT_TxtAddStr(
\ LOGTXT,
\ String(i)+" ) "+FN,
\ "Cropping = "+COORDS,
\ "AVS File = "+OUTDIR+OFN+".AVS",
\ "",
\ T,
\ "##########################"
\ )
}
}
} # End For
if(WRITELOG>0){RT_TxtWriteFile(LOGTXT,OUTDIR+OUTLOG)} # Write log file
""")
Return MessageClip (String(NFILES)+" Processed OK",width=320, height=200,shrink=false,text_color=$FFFF00,halo_color=$000000,bg_color=$5555FF)
##############################################
Function StrReplace(string s,string find,string replace) # Repeated, string replacements
# Original:- http://forum.doom9.org/showthread.php?t=147846&highlight=gscript 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 QuoteStr(String s) {return Chr(34)+S+Chr(34)} # Enclose a string eg filename, in Double Quotes (").
And Template, No external plugins required
# No external plugins required.
#
VideoFile="[VIDEOFILE]"
SizeX=[SIZEX]
SizeY=[SIZEY]
CropX=[CROPX]
CropY=[CROPY]
CropW=[CROPW]
CropH=[CROPH]
AutoLevel=[AUTOLEVEL]
ALMin=[ALMIN]
ALMax=[ALMAX]
DAR=[DAR] # Full Frame DAR
# --------------
DirectShowSource(VideoFile).ConvertToYV12().Trim(0,0)
(AutoLevel) ? Levels(ALMin,1.0,ALMax,16,235,Coring=False) : NOP
DAR=GetCropDAR(DAR,CropX,CropY,CropW,CropH) # Get DAR of Image part after cropping.
Crop(CropX,CropY,CropW,CropH)
(SizeX<=0&&SizeY<=0) ? Eval("""
# KEEPS the same SAR (Sample ie pixel Aspect Ratio)
# DAR changed because the full frame AR (FAR) changed, ie borders were chopped
# MeGUI will encode with same SAR (pixel) aspect ratio as source was.
# Nothing to do here.
""") : \
(SizeX>0&&SizeY<=0) ? Eval("""
# Resize by X dim. Y Will be resized to maintain image DAR with 1:1 SAR
SizeX = ((SizeX + 2) / 4) * 4 # Make sure X dim rounded to nearest multiple of 4
SizeY = Int(SizeX / DAR + 1.0) / 2 * 2 # Resize, make sure Y dim rounded to nearest multiple of 2
DAR = Float(SizeX)/SizeY # Sample aspect ratio ~ 1:1
Spline36Resize(SizeX,SizeY)
""") : \
(SizeX<=0&&SizeY>0) ? Eval("""
# Resize by Y dim. X Will be resized to maintain image DAR with 1:1 SAR
SizeY = ((SizeY + 1) / 2) * 2 # Make sure Y dim rounded to nearest multiple of 2
SizeX = Int(SizeY * DAR + 2.0) / 4 * 4 # Resize, make sure X dim rounded to nearest multiple of 4
DAR = Float(SizeX)/SizeY # Sample aspect ratio ~ 1:1
Spline36Resize(SizeX,SizeY)
""") : \
Eval("""
# Resize to supplied X & Y. Not for Tube sites nor devices requiring 1:1 SAR sample ie pixel aspect ratio.
Spline36Resize(SizeX,SizeY)
""")
SignalDAR(DAR) # Tell MeGUI the new Resized/Cropped Display Aspect Ratio
return last
# --------------
Function GetCropDAR(clip c,float DAR,float "X",float "Y",float "W",float "H") {
# Call prior to Crop/Resize with (possibly fractional) cropping to calc resultant DAR, X,Y,W,H are cropping coords
# DAR = FAR * SAR ::: FAR = DAR / SAR ::: SAR = DAR / FAR
#
X=Float(Default(X,0.0)) Y=Float(Default(Y,0.0)) W=Float(Default(W,0.0)) H=Float(Default(H,0.0))
W=W<=0.0?c.width+W-X:W H=H<=0.0?c.height+H-Y:H
# Irrespective of what various resizers in various Avisynth versions silently correct, we dont allow eg -ve X
Assert(X>=0.0&&X < c.width, "GetCropDAR: Invalid X("+String(X)+")")
Assert(Y>=0.0&&Y < c.height,"GetCropDAR: Invalid Y("+String(Y)+")")
Assert(W> 0.0&&X+W<=c.width, "GetCropDAR: Invalid W("+String(W)+")")
Assert(H> 0.0&&Y+H<=c.height,"GetCropDAR: Invalid H("+String(H)+")")
Return c.GetSAR(DAR) * W / H
}
# From MeGUI Wiki:
Function GetDAR(clip c, float SAR) { return Float(c.width) * SAR / Float(c.height)} # Gets the DAR from the SAR
Function GetSAR(clip c, float DAR) { return DAR * Float(c.height) / Float(c.width) } # Gets the SAR from the DAR
Function SignalDAR(float DAR){global MeGUI_darx=Round(1000*DAR) global MeGUI_dary=1000} # Signal DAR for MEGUI (Name change from SetDar)
gurusinghbrar
4th September 2012, 14:38
thanks stainless..
but is last script create out log with all extra stuff which is below ######### i need only
VideoFile="[VIDEOFILE]"
SizeX=[SIZEX]
SizeY=[SIZEY]
CropX=[CROPX]
CropY=[CROPY]
CropW=[CROPW]
CropH=[CROPH]
AutoLevel=[AUTOLEVEL]
ALMin=[ALMIN]
ALMax=[ALMAX]
DAR=[DAR] # Full Frame DAR
only... not all stuff
StainlessS
4th September 2012, 15:38
Previous post updated, see WRITELOG.
gurusinghbrar
4th September 2012, 20:00
hey StainlesS can you add some more info in outlog file like, if a video with 1280x720 ratio so in outlog it show
original height x width of main video and
crop method (which use in mplayer or ffmpeg use with -vf cropdetect) like crop=1280:560:0:92 and
output video size after crop and resize 480x212
example:
VideoFile="[VIDEOFILE]"
Original SizeX=[SIZEX]
Original SizeY=[SIZEY]
SizeX=[SIZEX]
SizeY=[SIZEY]
CropX=[CROPX]
CropY=[CROPY]
CropW=[CROPW]
CropH=[CROPH]
cropdetect=[1280:560:0:92]
AutoLevel=[AUTOLEVEL]
ALMin=[ALMIN]
ALMax=[ALMAX]
DAR=[DAR]
Resize=[480x212]
it will help me lot or i can create my own batch with ffmpeg / x264 video converting.
StainlessS
4th September 2012, 20:30
Not like you suggested, eg NOT Resize=[480x212]
cropdetect=[1280:560:0:92], Not sure what that is supposed to mean.
However, will implement as "ORIGX=[ORIGX]" rather than "Original SizeX=[SIZEX]" which would not work in avs (SPACE in name).
If making a batch, instead of "Resize=[480x212]" could use eg [SIZEX]x[SIZEY].
Am currently moving most of the crop logic out of AVS Template into Main script. SIZEX and SIZEY will become the output size.
Also implementing FFDAR and FFSAR (Full frame DAR and SAR, before crop)
ALSO, SAR and DAR, after crop/resize, and also user selected batch output extension (default ".AVS"), hopefully could just
change the beginning of main script to config and use a different Template for eg ffmpeg batch file.
The log will be of configurable number of lines with start line number. The LOG is created from the output eg AVS file.
The format of the template does not have to be formatted like the AVS template, could be a single line ffmpeg command with the [SIZEX] style vars inserted.
EDIT: Post a typical ffmpeg command line that you might want implemented, with explanation of the the numbers.
StainlessS
5th September 2012, 02:38
Update, batch creates AVS files, CMD files and a LOG file, using 3 different templates.
Script
# Need GScript and RT_Stats v1.04 minimum, with QueryborderCrop and QueryLumaMinMax from RT_Stats v1.03+.
# Requires MediaInfo CLI version.
# Script Demo to create a batch of AVS scripts, with AutoCrop and AutoLevels from a directory of video clips (AVI or Mpeg).
# Signals MeGUI with the new cropped/resize Aspect Ratio.
# Also creates a CMD file and log file.
# EDIT To Suit
DIR = "D:\VID\" # Video Input Directory
OUTDIR = "D:\OUT\" # Output and Temp Directory
INFILE = "*.AVI|MP*G|WMV|MP4|MKV" # Input file wildcards, add whatever.
TEMPLATE = "D:\TEMT\TEMPLATE_AVS.TEMT" # AVS file TEMPLATE.
CMDTEMPLATE = "D:\TEMT\TEMPLATE_CMD.TEMT" # CMD file TEMPLATE
LOGTEMPLATE = "D:\TEMT\TEMPLATE_LOG.TEMT" # LOG file TEMPLATE
MEDIAINFO = "D:\TEST\MediaInfo.Exe" # Path to MediaInfo.Exe CLI version
OUTLOG = "OUTLOG.TXT" # Output log file name without path.
CROPCOORDS = 2 # 0=CropExact (DONT USE), 1=CropLess, 2=CropMore, 3=CropPlus
AUTOLEVEL_STRENGTH = 1.0 # AutoLevel Strength 0.0 -> 1.0, 0.0 == OFF
HIDE = True # Hide MediaInfo Console window
WRITELOG = True # Write logging file to outdir
WRITECMD = True # Write command file.
#
# SIZE X/Y, If both 0 then JUST CROP
# If ONLY one dimenson (X or Y) == 0, is resized to 1:1 pixel Aspect ratio, ie YouTube etc or other device needing 1:1 pixel aspect ratio.
# If both non 0, then resized to given dimensions and MeGUI sets aspect ratio, (Needs player that conforms with non 1:1 pixel aspect ratio).
SIZEX = 640
SIZEY = 480
#
# ----------------------------------------------------------
GSCript("""
Assert(CROPCOORDS!=0, "Cannot use CropExact")
AUTOLEVEL=(AUTOLEVEL_STRENGTH>0.0)
LISTNAME = OUTDIR + "FILELIST.TXT"
Result=RT_WriteFileList(DIR+INFILE,LISTNAME) # Write list file of video file names
Assert((Result!=0), "No files found")
FILELIST=RT_ReadTxtFromFile(LISTNAME) # Read list of names
NFILES=RT_TxtQueryLines(FILELIST) # Number of files in list
TEMT=RT_ReadTxtFromFile(TEMPLATE) # Read Template avs/batch file.
CMDT=RT_ReadTxtFromFile(CMDTEMPLATE) # Read Template cmd file.
LOGT=RT_ReadTxtFromFile(LOGTEMPLATE) # Read Template log file.
LOGTXT="" # Init output log
For(i=0,NFILES-1) { # Step through list of file names
FN=RT_TxtGetLine(FILELIST,i) # The file name of video clip including full path
DirectshowSource(FN).ConvertToYV12() # Load file, Convert to whatever your required colorspace (affects cropping)
QBC=QueryBorderCrop() # Inquire all modes cropping
Eval(QBC) # Set variables in Avisynth
COORDS=RT_TxtGetLine(QBC,CROPCOORDS) # The cropping mode that is required (2=CropMore)
if(AUTOLEVEL) {
ALTMPC=Crop(QBCropXP,QBCropYP,QBCropWP,QBCropHP) # Crop TEMP with CropPlus coords (ignore black borders totally)
QLMM = ALTMPC.QueryLumaMinMax()
Eval(QLMM)
ALMin = Int(16 - ((16 - QLMMMin) * AUTOLEVEL_STRENGTH) + 0.5) # Round Up
ALMax = Int(235 - ((235 - QLMMMax) * AUTOLEVEL_STRENGTH)) # Round down
} else {
ALMin=16
ALMin=235
}
QBCTrailer=Select(CROPCOORDS,"=","L=","M=","P=")
COORDS=StrReplace(COORDS,QBCTrailer,"=") # Change all to style without a trailer eg QBCropXM to QBCropX.
COORDS=StrReplace(COORDS,"QBC","C") # Change all style QBCropX to CropX
Eval(COORDS) # Set variables in Avisynth, ie CropX etc
MITXT = OUTDIR+"MEDINFO.TXT"
MI = QuoteStr(MEDIAINFO) + " --LogFile="+ QuoteStr(MITXT) + " --Output=Video;%AspectRatio% " + QuoteStr(FN)
Assert(RT_Call(MI,Hide=True)==0,"Problem Calling MediaInfo" + Chr(10)+MI) # MediaInfo Command line version.
MDAR=RT_ReadTxtFromFile(MITXT,0)
FFDAR=Value(MDAR) # Full Frame DAR
FFSAR=FFDAR * Height / Width
DAR=GetCropDAR(FFDAR,CropX,CropY,CropW,CropH) # Get DAR of Image part after cropping.
CropWRel = CropX + CropW - Width
CropHRel = CropY + CropH - Height
if(SizeX<=0&&SizeY<=0) {
# Crop Only, KEEPS the same SAR (Sample ie pixel Aspect Ratio)
# DAR will change (become Image DAR) because borders were chopped
# MeGUI will encode with same SAR (pixel) aspect ratio as source (but using the DAR variable, converted to SAR by MeGUI).
SAR = GetSAR(FFDAR) # Get SAR from Full Frame DAR
SX = (CropW<=0) ? Width + CropW - CropX : CropW
SY = (CropH<=0) ? Height + CropH - CropY : CropH
} else if(SizeX>0&&SizeY<=0){
# Resize by X dim. Y Will be resized to maintain image DAR with 1:1 SAR
SX = ((SizeX + 2) / 4) * 4 # Make sure X dim rounded to nearest multiple of 4
SY = Int(SX / DAR + 1.0) / 2 * 2 # Resize, make sure Y dim rounded to nearest multiple of 2
DAR = Float(SX)/SY # Equiv Sample aspect ratio ~ 1:1
SAR = 1.0 # 1:1
} else if(SizeX<=0&&SizeY>0) {
# Resize by Y dim. X Will be resized to maintain image DAR with 1:1 SAR
SY = ((SizeY + 1) / 2) * 2 # Make sure Y dim rounded to nearest multiple of 2
SX = Int(SY * DAR + 2.0) / 4 * 4 # Resize, make sure X dim rounded to nearest multiple of 4
DAR = Float(SX)/SY # Equiv Sample aspect ratio ~ 1:1
SAR = 1.0 # 1:1
} else {
# Resize to supplied X & Y. Not for Tube sites nor devices requiring 1:1 SAR sample ie pixel aspect ratio.
SX=SizeX SY=SizeY # Copy Size vars
SAR = DAR * SY / SX
}
OFN = StrReplace(FN,DIR,"") # Out filename excluding Input dir path
AFN=OUTDIR+AddFileExtension(OFN,"AVS") # Generated AVS file.
CFN=OUTDIR+AddFileExtension(OFN,"CMD") # Generated CMD file.
BASEFN=StrReplace(FN,DIR,"").DotExt2UsExt()
# Now we need to Edit the variables in copy of one of the template files
for(repn=0,2) {
T = (repn==0) ? TEMT : (repn==1) ? CMDT : LOGT
T=StrReplace(T,"[AVSFILE]",AFN) # Replace ALL occurences of AVS file name incl full path.
T=StrReplace(T,"[VIDEOFILE]",FN) # Replace ALL occurences of video file name incl full path.
T=StrReplace(T,"[CMDFILE]",CFN) # Replace ALL occurences of CMD file name incl full path.
T=StrReplace(T,"[BASEFILE]",BASEFN) # Base filename excluding path, ".Ext" changed to "_Ext"
T=StrReplace(T,"[ORIGX]",String(Width)) # Resize X dim
T=StrReplace(T,"[ORIGY]",String(Height)) # Resize Y dim
T=StrReplace(T,"[SIZEX]",String(SX)) # Resize X dim
T=StrReplace(T,"[SIZEY]",String(SY)) # Resize Y dim
T=StrReplace(T,"[CROPX]",String(CropX)) # Crop X, using either CropLess, CropMore, or CropPlus
T=StrReplace(T,"[CROPY]",String(CropY)) # Crop Y
T=StrReplace(T,"[CROPW]",String(CropW)) # Crop W
T=StrReplace(T,"[CROPH]",String(CropH)) # Crop H
T=StrReplace(T,"[CROPWREL]",String(CropWRel)) # Crop W Relative
T=StrReplace(T,"[CROPHREL]",String(CropHRel)) # Crop H Relative
T=StrReplace(T,"[AUTOLEVEL]",String(AUTOLEVEL&&(ALMin!=16||ALMax!=235))) # True/False
T=StrReplace(T,"[ALMIN]",String(ALMin)) # Auto Level Min
T=StrReplace(T,"[ALMAX]",String(ALMax)) # Auto Level Max
T=StrReplace(T,"[CSMIN]",String(16)) # Colorspace Min
T=StrReplace(T,"[CSMAX]",String(235)) # Colorspace Max
T=StrReplace(T,"[FFDAR]",String(FFDAR)) # Full frame DAR (Before Crop)
T=StrReplace(T,"[FFSAR]",String(FFSAR)) # Full frame SAR (Before Crop)
T=StrReplace(T,"[DAR]",String(DAR)) # Cropped/Resized DAR
T=StrReplace(T,"[SAR]",String(SAR)) # Cropped/Resized SAR, Sample ie Pixel Aspect Ratio
if(repn==0) {
RT_TxtWriteFile(T,AFN) # Write AVS file to OUTDIR
} else if(repn==1) {
if(WRITECMD) {
RT_TxtWriteFile(T,CFN) # Write AVS file to OUTDIR
}
}
}
if(WRITELOG) {
LOGTXT=RT_TxtAddStr(LOGTXT,T,"","")
}
} # End For
if(WRITELOG){RT_TxtWriteFile(LOGTXT,OUTDIR+OUTLOG)} # Write log file
""")
Return MessageClip (String(NFILES)+" Processed OK",width=320, height=200,shrink=false,text_color=$FFFF00,halo_color=$000000,bg_color=$5555FF)
##############################################
Function StrReplace(string s,string find,string replace) # Repeated, string replacements
# Original:- http://forum.doom9.org/showthread.php?t=147846&highlight=gscript 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)))}
# Get File extension from filename excluding '.', use to eg select Source filter for audio file.
Function GetFileExtension(String fn) {len=FindStr(RevStr(fn),".")-1 Return Len>=1 ? RightStr(fn,Len) : ""}
# Add File extension to filename, changing old '.' to '_'. Avoid AntiMalware false +ve on eg *.mpg.cmd
Function DotExt2UsExt(String fn){n=StrLen(fn) r=FindStr(RevStr(fn),".") return leftStr(fn,n-r)+((r>1)?"_"+MidStr(fn,n-r+2):"")}
# Add File extension to filename, changing old '.' to '_'. Avoid AntiMalware false +ve on eg *.mpg.cmd
Function AddFileExtension(String fn,String Ext){n=StrLen(fn) r=FindStr(RevStr(fn),".")return leftStr(fn,n-r)+((r>1)?"_"+MidStr(fn,n-r+2):"")+"."+Ext}
Function QuoteStr(String s) {return Chr(34)+S+Chr(34)} # Enclose a string eg filename, in Double Quotes (").
# --------------
Function GetCropDAR(clip c,float DAR,float "X",float "Y",float "W",float "H") {
# Call prior to Crop/Resize with (possibly fractional) cropping to calc resultant DAR, X,Y,W,H are cropping coords
# DAR = FAR * SAR ::: FAR = DAR / SAR ::: SAR = DAR / FAR
#
X=Float(Default(X,0.0)) Y=Float(Default(Y,0.0)) W=Float(Default(W,0.0)) H=Float(Default(H,0.0))
W=W<=0.0?c.width+W-X:W H=H<=0.0?c.height+H-Y:H
# Irrespective of what various resizers in various Avisynth versions silently correct, we dont allow eg -ve X
Assert(X>=0.0&&X < c.width, "GetCropDAR: Invalid X("+String(X)+")")
Assert(Y>=0.0&&Y < c.height,"GetCropDAR: Invalid Y("+String(Y)+")")
Assert(W> 0.0&&X+W<=c.width, "GetCropDAR: Invalid W("+String(W)+")")
Assert(H> 0.0&&Y+H<=c.height,"GetCropDAR: Invalid H("+String(H)+")")
Return c.GetSAR(DAR) * W / H
}
# From MeGUI Wiki:
Function GetDAR(clip c, float SAR) { return Float(c.width) * SAR / Float(c.height)} # Gets the DAR from the SAR
Function GetSAR(clip c, float DAR) { return DAR * Float(c.height) / Float(c.width) } # Gets the SAR from the DAR
Function SignalDAR(float DAR){global MeGUI_darx=Round(1000*DAR) global MeGUI_dary=1000} # Signal DAR for MEGUI (Name change from SetDar)
AVS TEMPLATE
AVSFile="[AVSFILE]" # Name of THIS file
VideoFile="[VIDEOFILE]" # Source file
CMDFile="[CMDFILE]" # Name of BATCH file
BaseFile="[BASEFILE]" # Base Filename, no path and ".Ext" changed to "_Ext"
OrigX=[ORIGX] # Original Width
OrigY=[ORIGY] # Original Height
SizeX=[SIZEX] # Output Width
SizeY=[SIZEY] # Output Height
CropX=[CROPX] # Crop Left
CropY=[CROPY] # Crop Top
CropW=[CROPW] # Crop Width
CropH=[CROPH] # Crop Height
CropWRel=[CROPWREL] # Crop Width relative
CropHRel=[CROPHREL] # Crop Height relative
AutoLevel=[AUTOLEVEL] # True If Auto Level
ALMin=[ALMIN] # AutoLevel Minimum
ALMax=[ALMAX] # AutoLevel Maximum
CSMin=[CSMIN] # Colorspace Minimum
CSMax=[CSMAX] # Colorspace Maximum
FFDAR=[FFDAR] # Full Frame DAR before cropping
FFSAR=[FFSAR] # Full Frame SAR before cropping
DAR=[DAR] # DAR After Crop/Resize
SAR=[SAR] # Sample ie Pixel Aspect Ratio
# --------------
DirectShowSource(VideoFile).ConvertToYV12().Trim(0,0)
Crop(CropX,CropY,CropW,CropH)
(AutoLevel) ? Levels(ALMin,1.0,ALMax,CSMin,CSMax,Coring=False) : NOP
(SizeX != Width && SizeY != Height) ? Spline36Resize(SizeX,SizeY) : NOP
SignalDAR(DAR) # Tell MeGUI the new Resized/Cropped Display Aspect Ratio
return last
# --------------
# From MeGUI Wiki:
Function SignalDAR(float DAR){global MeGUI_darx=Round(1000*DAR) global MeGUI_dary=1000} # Signal DAR for MEGUI (Name change from SetDar)
CMD Template, I Dont use ffmpeg much, so all it does is convert to AVI (no crop/resize) with huffyuv video, make your own CMD file.
"D:\TEST\ffmpeg.exe" -i "[VIDEOFILE]" -vcodec huffyuv -acodec pcm_s16le "D:\OUT\[BASEFILE].AVI"
Log Template
Example LOG File
The input Video file is :- [VIDEOFILE]
The created AVS file is :- [AVSFILE]
The created CMD file is :- [CMDFILE] Note, eg 1.mpg video file is converted 1_mpg.CMD not 1.mpg.CMD to avoid anti malware false +ve.
The Base filename is :- [BASEFILE]
Original Size :- [ORIGX]x[ORIGY] with a DAR of [FFDAR] and SAR of [FFSAR]
Resize to :- [SIZEX]x[SIZEY] with Cropping [CROPX],[CROPY],[CROPW],[CROPH], DAR = [DAR], SAR = [SAR]
Resize Relative Cropping :- CropW Relative = [CROPWREL], CropH Relative = [CROPHREL]
AutoLevel :- [AUTOLEVEL]
Levels if AUTOLEVEL True :- Levels([ALMIN],1.0,[ALMAX],[CSMIN],[CSMAX],Coring=False)
StainlessS
5th September 2012, 02:42
Too big for one post, see previous post.
One of the batched AVS Scripts generated (NOT ALL ENTRIES ARE USED, ITS AN EXAMPLE)
AVSFile="D:\OUT\1_mpg.AVS" # Name of THIS file
VideoFile="D:\VID\1.mpg" # Source file
CMDFile="D:\OUT\1_mpg.CMD" # Name of BATCH file
BaseFile="1_mpg" # Base Filename, no path and ".Ext" changed to "_Ext"
OrigX=448 # Original Width
OrigY=304 # Original Height
SizeX=640 # Output Width
SizeY=480 # Output Height
CropX=18 # Crop Left
CropY=0 # Crop Top
CropW=412 # Crop Width
CropH=304 # Crop Height
CropWRel=-18 # Crop Width relative
CropHRel=0 # Crop Height relative
AutoLevel=true # True If Auto Level
ALMin=22 # AutoLevel Minimum
ALMax=231 # AutoLevel Maximum
CSMin=16 # Colorspace Minimum
CSMax=235 # Colorspace Maximum
FFDAR=1.474000 # Full Frame DAR before cropping
FFSAR=1.000214 # Full Frame SAR before cropping
DAR=1.355554 # DAR After Crop/Resize
SAR=1.016665 # Sample ie Pixel Aspect Ratio
# --------------
DirectShowSource(VideoFile).ConvertToYV12().Trim(0,0)
Crop(CropX,CropY,CropW,CropH)
(AutoLevel) ? Levels(ALMin,1.0,ALMax,CSMin,CSMax,Coring=False) : NOP
(SizeX != Width && SizeY != Height) ? Spline36Resize(SizeX,SizeY) : NOP
SignalDAR(DAR) # Tell MeGUI the new Resized/Cropped Display Aspect Ratio
return last
# --------------
# From MeGUI Wiki:
Function SignalDAR(float DAR){global MeGUI_darx=Round(1000*DAR) global MeGUI_dary=1000} # Signal DAR for MEGUI (Name change from SetDar)
one of the Command batch files generated, Just a demo, create your own cmd template
"D:\TEST\ffmpeg.exe" -i "D:\VID\1.mpg" -vcodec huffyuv -acodec pcm_s16le "D:\OUT\1_mpg.AVI"
Log file
Example LOG File
The input Video file is :- D:\VID\1.mpg
The created AVS file is :- D:\OUT\1_mpg.AVS
The created CMD file is :- D:\OUT\1_mpg.CMD Note, eg 1.mpg video file is converted 1_mpg.CMD not 1.mpg.CMD to avoid anti malware false +ve.
The Base filename is :- 1_mpg
Original Size :- 448x304 with a DAR of 1.474000 and SAR of 1.000214
Resize to :- 640x480 with Cropping 18,0,412,304, DAR = 1.355554, SAR = 1.016665
Resize Relative Cropping :- CropW Relative = -18, CropH Relative = 0
AutoLevel :- true
Levels if AUTOLEVEL True :- Levels(22,1.0,231,16,235,Coring=False)
Example LOG File
The input Video file is :- D:\VID\Lexx-S02xE01-Mantrid.avi
The created AVS file is :- D:\OUT\Lexx-S02xE01-Mantrid_avi.AVS
The created CMD file is :- D:\OUT\Lexx-S02xE01-Mantrid_avi.CMD Note, eg 1.mpg video file is converted 1_mpg.CMD not 1.mpg.CMD to avoid anti malware false +ve.
The Base filename is :- Lexx-S02xE01-Mantrid_avi
Original Size :- 512x384 with a DAR of 1.333000 and SAR of 0.999750
Resize to :- 640x480 with Cropping 2,0,508,384, DAR = 1.322586, SAR = 0.991939
Resize Relative Cropping :- CropW Relative = -2, CropH Relative = 0
AutoLevel :- true
Levels if AUTOLEVEL True :- Levels(15,1.0,216,16,235,Coring=False)
Example LOG File
The input Video file is :- D:\VID\Lexx-S02xE02-Terminal.avi
The created AVS file is :- D:\OUT\Lexx-S02xE02-Terminal_avi.AVS
The created CMD file is :- D:\OUT\Lexx-S02xE02-Terminal_avi.CMD Note, eg 1.mpg video file is converted 1_mpg.CMD not 1.mpg.CMD to avoid anti malware false +ve.
The Base filename is :- Lexx-S02xE02-Terminal_avi
Original Size :- 512x384 with a DAR of 1.333000 and SAR of 0.999750
Resize to :- 640x480 with Cropping 2,0,508,384, DAR = 1.322586, SAR = 0.991939
Resize Relative Cropping :- CropW Relative = -2, CropH Relative = 0
AutoLevel :- true
Levels if AUTOLEVEL True :- Levels(15,1.0,233,16,235,Coring=False)
Example LOG File
The input Video file is :- D:\VID\Lexx-S02xE03-Lyekka.avi
The created AVS file is :- D:\OUT\Lexx-S02xE03-Lyekka_avi.AVS
The created CMD file is :- D:\OUT\Lexx-S02xE03-Lyekka_avi.CMD Note, eg 1.mpg video file is converted 1_mpg.CMD not 1.mpg.CMD to avoid anti malware false +ve.
The Base filename is :- Lexx-S02xE03-Lyekka_avi
Original Size :- 576x432 with a DAR of 1.333000 and SAR of 0.999750
Resize to :- 640x480 with Cropping 6,0,564,428, DAR = 1.317427, SAR = 0.988071
Resize Relative Cropping :- CropW Relative = -6, CropH Relative = -4
AutoLevel :- true
Levels if AUTOLEVEL True :- Levels(14,1.0,218,16,235,Coring=False)
To create your own templates, just use the examples given eg insert [DAR] wherever you need the crop/resized display aspect ratio, etc.
You can even use the created AVS files in the command files (or indeed the command files in the avs files).
StainlessS
5th September 2012, 21:53
AutoCropLevelsGenny.avs
Update, with GROUP CMD file.
# AutoCropLevelsGenny.avs
#
# Need GScript and RT_Stats v1.04 minimum, with QueryborderCrop and QueryLumaMinMax from RT_Stats v1.03+.
# Requires MediaInfo CLI version.
# Demo Script to create a batch of AVS scripts, with AutoCrop and AutoLevels from a directory of video clips.
# Generated AVS scripts signal MeGUI with the new cropped/resize Aspect Ratio.
# Also creates CMD files and log file.
# Option to create 1 single Group command CMD file.
# EDIT To Suit
DIR = "D:\VID\" # Video Input Directory
OUTDIR = "D:\OUT\" # Output and Temp Directory
INFILE = "*.AVI|MP*G|WMV|MP4|MKV|flv" # Input file wildcards, add whatever.
TEMPLATE = "D:\TEMT\TEMPLATE_AVS.TEMT" # AVS file TEMPLATE.
CMDTEMPLATE = "D:\TEMT\TEMPLATE_CMD.TEMT" # CMD file TEMPLATE
LOGTEMPLATE = "D:\TEMT\TEMPLATE_LOG.TEMT" # LOG file TEMPLATE
MEDIAINFO = "D:\TEST\MediaInfo.Exe" # Path to MediaInfo.Exe CLI version
OUTLOG = "OUTLOG.TXT" # Output log file name without path.
GROUPCMDFILE = "D:\GROUP.CMD" # filename incl path for single combined group CMD file.
CROPCOORDS = 2 # 0=CropExact (DONT USE), 1=CropLess, 2=CropMore, 3=CropPlus
AUTOLEVEL_STRENGTH = 1.0 # AutoLevel Strength 0.0 -> 1.0, 0.0 == OFF
HIDE = True # Hide MediaInfo Console window
WRITELOG = True # Write logging file to outdir
WRITECMD = True # Write separate command files.
WRITEGROUPCMD = True # All CMD files written to a single command file.
GROUPCMDPAUSE = True # Append Pause to end of Group CMD file.
#
# SIZE X/Y, If both 0 then JUST CROP
# If ONLY one dimenson (X or Y) == 0, it is resized to 1:1 pixel Aspect ratio, ie YouTube etc or other device needing 1:1 pixel aspect ratio.
# If both non 0, then resized to given dimensions and MeGUI sets aspect ratio, (Needs player that conforms with non 1:1 pixel aspect ratio).
SIZEX = 472
SIZEY = 0
#
# ----------------------------------------------------------
GSCript("""
Assert(CROPCOORDS!=0, "Cannot use CropExact")
AUTOLEVEL=(AUTOLEVEL_STRENGTH>0.0)
LISTNAME = OUTDIR + "FILELIST.TXT"
Result=RT_WriteFileList(DIR+INFILE,LISTNAME) # Write list file of video file names
Assert((Result!=0), "No files found")
FILELIST=RT_ReadTxtFromFile(LISTNAME) # Read list of names
NFILES=RT_TxtQueryLines(FILELIST) # Number of files in list
TEMT=RT_ReadTxtFromFile(TEMPLATE) # Read Template avs/batch file.
CMDT=RT_ReadTxtFromFile(CMDTEMPLATE) # Read Template cmd file.
LOGT=RT_ReadTxtFromFile(LOGTEMPLATE) # Read Template log file.
LOGTXT="" # Init output log
GRPTXT="" # Init Group Cmd
For(i=0,NFILES-1) { # Step through list of file names
FN=RT_TxtGetLine(FILELIST,i) # The file name of video clip including full path
DirectshowSource(FN).ConvertToYV12() # Load file, Convert to whatever your required colorspace (affects cropping)
QBC=QueryBorderCrop() # Inquire all modes cropping
Eval(QBC) # Set variables in Avisynth
COORDS=RT_TxtGetLine(QBC,CROPCOORDS) # The cropping mode that is required (2=CropMore)
if(AUTOLEVEL) {
ALTMPC=Crop(QBCropXP,QBCropYP,QBCropWP,QBCropHP) # Crop TEMP with CropPlus coords (ignore black borders totally)
QLMM = ALTMPC.QueryLumaMinMax()
Eval(QLMM)
ALMin = Int(16 - ((16 - QLMMMin) * AUTOLEVEL_STRENGTH) + 0.5) # Round Up
ALMax = Int(235 - ((235 - QLMMMax) * AUTOLEVEL_STRENGTH)) # Round down
} else {
ALMin=16
ALMin=235
}
QBCTrailer=Select(CROPCOORDS,"=","L=","M=","P=")
COORDS=StrReplace(COORDS,QBCTrailer,"=") # Change all to style without a trailer eg QBCropXM to QBCropX.
COORDS=StrReplace(COORDS,"QBC","C") # Change all style QBCropX to CropX
Eval(COORDS) # Set variables in Avisynth, ie CropX etc
MITXT = OUTDIR+"MEDINFO.TXT"
MI = QuoteStr(MEDIAINFO) + " --LogFile="+ QuoteStr(MITXT) + " --Output=Video;%AspectRatio% " + QuoteStr(FN)
Assert(RT_Call(MI,Hide=True)==0,"Problem Calling MediaInfo" + Chr(10)+MI) # MediaInfo Command line version.
MDAR=RT_ReadTxtFromFile(MITXT,0)
FFDAR=Value(MDAR) # Full Frame DAR
FFSAR=FFDAR * Height / Width
DAR=GetCropDAR(FFDAR,CropX,CropY,CropW,CropH) # Get DAR of Image part after cropping.
CropWRel = CropX + CropW - Width
CropHRel = CropY + CropH - Height
if(SizeX<=0&&SizeY<=0) {
# Crop Only, KEEPS the same SAR (Sample ie pixel Aspect Ratio)
# DAR will change (become Image DAR) because borders were chopped
# MeGUI will encode with same SAR (pixel) aspect ratio as source (but using the DAR variable, converted to SAR by MeGUI).
SAR = GetSAR(FFDAR) # Get SAR from Full Frame DAR
SX = CropW
SY = CropH
} else if(SizeX>0&&SizeY<=0){
# Resize by X dim. Y Will be resized to maintain image DAR with 1:1 SAR
SX = ((SizeX + 2) / 4) * 4 # Make sure X dim rounded to nearest multiple of 4
SY = Int(SX / DAR + 1.0) / 2 * 2 # Resize, make sure Y dim rounded to nearest multiple of 2
DAR = Float(SX)/SY # Equiv Sample aspect ratio ~ 1:1
SAR = DAR * SY/SX # ~1:1
} else if(SizeX<=0&&SizeY>0) {
# Resize by Y dim. X Will be resized to maintain image DAR with 1:1 SAR
SY = ((SizeY + 1) / 2) * 2 # Make sure Y dim rounded to nearest multiple of 2
SX = Int(SY * DAR + 2.0) / 4 * 4 # Resize, make sure X dim rounded to nearest multiple of 4
DAR = Float(SX)/SY # Equiv Sample aspect ratio ~ 1:1
SAR = DAR * SY/SX # ~1:1
} else {
# Resize to supplied X & Y. Not for Tube sites nor devices requiring 1:1 SAR sample ie pixel aspect ratio.
SX=SizeX SY=SizeY # Copy Size vars
SAR = DAR * SY/SX
}
OFN = StrReplace(FN,DIR,"") # Out filename excluding Input dir path
AFN=OUTDIR+AddFileExtension(OFN,"AVS") # Generated AVS file.
CFN=OUTDIR+AddFileExtension(OFN,"CMD") # Generated CMD file.
BASEFN=DotExt2UsExt(OFN) # Base filename
# Now we need to Edit the variables in copy of one of the template files
for(repn=0,2) {
T = (repn==0) ? TEMT : (repn==1) ? CMDT : LOGT
T=StrReplace(T,"[AVSFILE]",AFN) # Replace ALL occurences of AVS file name incl full path.
T=StrReplace(T,"[VIDEOFILE]",FN) # video
T=StrReplace(T,"[CMDFILE]",CFN) # cmd
T=StrReplace(T,"[BASEFILE]",BASEFN) # base
T=StrReplace(T,"[ORIGX]",String(Width)) # Orig X dim
T=StrReplace(T,"[ORIGY]",String(Height)) # Orig Y dim
T=StrReplace(T,"[SIZEX]",String(SX)) # Resize X dim
T=StrReplace(T,"[SIZEY]",String(SY)) # Resize Y dim
T=StrReplace(T,"[CROPX]",String(CropX)) # Crop X
T=StrReplace(T,"[CROPY]",String(CropY)) # Crop Y
T=StrReplace(T,"[CROPW]",String(CropW)) # Crop W
T=StrReplace(T,"[CROPH]",String(CropH)) # Crop H
T=StrReplace(T,"[CROPWREL]",String(CropWRel)) # Crop W Relative
T=StrReplace(T,"[CROPHREL]",String(CropHRel)) # Crop H Relative
T=StrReplace(T,"[CROPRGT]",String(-CropWRel)) # Crop Right edge
T=StrReplace(T,"[CROPBOT]",String(-CropHRel)) # Crop Bottom edge
T=StrReplace(T,"[AUTOLEVEL]",String(AUTOLEVEL&&(ALMin!=16||ALMax!=235))) # True/False
T=StrReplace(T,"[ALMIN]",String(ALMin)) # Auto Level Min
T=StrReplace(T,"[ALMAX]",String(ALMax)) # Auto Level Max
T=StrReplace(T,"[CSMIN]",String(16)) # Colorspace Min
T=StrReplace(T,"[CSMAX]",String(235)) # Colorspace Max
T=StrReplace(T,"[FFDAR]",String(FFDAR,"%.3f")) # Full frame DAR (Before Crop)
T=StrReplace(T,"[FFSAR]",String(FFSAR,"%.3f")) # Full frame SAR (Before Crop)
T=StrReplace(T,"[DAR]",String(DAR,"%.3f")) # Cropped/Resized DAR
T=StrReplace(T,"[SAR]",String(SAR,"%.3f")) # Cropped/Resized SAR, Sample ie Pixel Aspect Ratio
if(repn==0) {
RT_TxtWriteFile(T,AFN) # Write AVS file to OUTDIR
} else if(repn==1) {
if(WRITECMD) {
RT_TxtWriteFile(T,CFN) # Write CMD file to OUTDIR
}
if(WRITEGROUPCMD) {
GRPTXT=RT_TxtAddStr(GRPTXT,T)
}
} else if(repn==2&&WRITELOG) {
LOGTXT=RT_TxtAddStr(LOGTXT,T)
}
}
} # End For
if(WRITEGROUPCMD){
if(GROUPCMDPAUSE) {GRPTXT=RT_TxtAddStr(GRPTXT,"","Pause")}
RT_TxtWriteFile(GRPTXT,GROUPCMDFILE)
}
if(WRITELOG){RT_TxtWriteFile(LOGTXT,OUTDIR+OUTLOG)} # Write log file
""")
Return MessageClip (String(NFILES)+" Processed OK",width=320, height=200,shrink=false,text_color=$FFFF00,halo_color=$000000,bg_color=$5555FF)
#######################
Function StrReplace(string s,string find,string replace) # Repeated, string replacements
# Original:- http://forum.doom9.org/showthread.php?t=147846&highlight=gscript 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)))}
# Get File extension from filename excluding '.', use to eg select Source filter for audio file.
Function GetFileExtension(String fn) {len=FindStr(RevStr(fn),".")-1 Return Len>=1 ? RightStr(fn,Len) : ""}
# Change extension '.' to '_'. Avoid AntiMalware false +ve on eg *.mpg.cmd
Function DotExt2UsExt(String fn){n=StrLen(fn) r=FindStr(RevStr(fn),".") return leftStr(fn,n-r)+((r>1)?"_"+MidStr(fn,n-r+2):"")}
# Add File extension to filename, changing old '.' to '_'. Avoid AntiMalware false +ve on eg *.mpg.cmd
Function AddFileExtension(String fn,String Ext){n=StrLen(fn) r=FindStr(RevStr(fn),".")return leftStr(fn,n-r)+((r>1)?"_"+MidStr(fn,n-r+2):"")+"."+Ext}
Function QuoteStr(String s) {return RT_StrAddStr(Chr(34),S,Chr(34))} # Enclose a string eg DOS command filename, in Double Quotes (").
# --------------
Function GetCropDAR(clip c,float DAR,float "X",float "Y",float "W",float "H") {
# Call prior to Crop/Resize with (possibly fractional) cropping to calc resultant DAR, X,Y,W,H are cropping coords
# DAR = FAR * SAR ::: FAR = DAR / SAR ::: SAR = DAR / FAR
#
X=Float(Default(X,0.0)) Y=Float(Default(Y,0.0)) W=Float(Default(W,0.0)) H=Float(Default(H,0.0))
W=W<=0.0?c.width+W-X:W H=H<=0.0?c.height+H-Y:H
# Irrespective of what various resizers in various Avisynth versions silently correct, we dont allow eg -ve X
Assert(X>=0.0&&X < c.width, "GetCropDAR: Invalid X("+String(X)+")")
Assert(Y>=0.0&&Y < c.height,"GetCropDAR: Invalid Y("+String(Y)+")")
Assert(W> 0.0&&X+W<=c.width, "GetCropDAR: Invalid W("+String(W)+")")
Assert(H> 0.0&&Y+H<=c.height,"GetCropDAR: Invalid H("+String(H)+")")
Return c.GetSAR(DAR) * W / H
}
# From MeGUI Wiki:
Function GetDAR(clip c, float SAR) { return Float(c.width) * SAR / Float(c.height)} # Gets the DAR from the SAR
Function GetSAR(clip c, float DAR) { return DAR * Float(c.height) / Float(c.width) } # Gets the SAR from the DAR
Function SignalDAR(float DAR){global MeGUI_darx=Round(1000*DAR) global MeGUI_dary=1000} # Signal DAR for MEGUI (Name change from SetDar)
See next post for templates and results.
vBulletin® v3.8.11, Copyright ©2000-2026, vBulletin Solutions Inc.