Log in

View Full Version : Any tool for Autocrop and Resize based on ffmpeg


Pages : 1 [2]

StainlessS
5th September 2012, 21:58
Continued from previous post.

AVS Template (Most entries unused)


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
CropRGT=[CROPRGT] # Crop Right Edge, abs
CropBOT=[CROPBOT] # Crop Bottom Edge, abs
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

"D:\TEST\ffmpeg.exe" -i "[AVSFILE]" -vcodec libx264 -crf 21.5 -s [SIZEX]x[SIZEY] -aspect [DAR] -acodec libvo_aacenc -ab 128k "D:\OUT\[BASEFILE].MKV"


Log Template

The input Video file is :- [VIDEOFILE]
The created AVS file is :- [AVSFILE]
The created CMD file is :- [CMDFILE] eg 1.mpg file is converted 1_mpg.CMD not 1.mpg.CMD, avoids 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]
Resize Abs Cropping :- CropRight Abs = [CROPRGT], CropBottom Abs = [CROPBOT]
AutoLevel :- [AUTOLEVEL]
Levels if AUTOLEVEL True :- Levels([ALMIN],1.0,[ALMAX],[CSMIN],[CSMAX],Coring=False)


AVS Output

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=472 # Output Width
SizeY=348 # 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
CropRGT=18 # Crop Right Edge, abs
CropBOT=0 # Crop Bottom Edge, abs
AutoLevel=true # True If Auto Level
ALMin=22 # AutoLevel Minimum
ALMax=231 # AutoLevel Maximum
CSMin=16 # Colorspace Minimum
CSMax=235 # Colorspace Maximum
FFDAR=1.474 # Full Frame DAR before cropping
FFSAR=1.000 # Full Frame SAR before cropping
DAR=1.356 # DAR After Crop/Resize
SAR=1.000 # 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)


Log Output

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 eg 1.mpg file is converted 1_mpg.CMD not 1.mpg.CMD, avoids anti malware false +ve.
The Base filename is :- 1_mpg
Original Size :- 448x304 with a DAR of 1.474 and SAR of 1.000
Resize to :- 472x348 with Cropping 18,0,412,304, DAR = 1.356, SAR = 1.000
Resize Relative Cropping :- CropW Relative = -18, CropH Relative = 0
Resize Abs Cropping :- CropRight Abs = 18, CropBottom Abs = 0
AutoLevel :- true
Levels if AUTOLEVEL True :- Levels(22,1.0,231,16,235,Coring=False)

The input Video file is :- D:\VID\2.avi
The created AVS file is :- D:\OUT\2_avi.AVS
The created CMD file is :- D:\OUT\2_avi.CMD eg 1.mpg file is converted 1_mpg.CMD not 1.mpg.CMD, avoids anti malware false +ve.
The Base filename is :- 2_avi
Original Size :- 640x360 with a DAR of 1.778 and SAR of 1.000
Resize to :- 472x266 with Cropping 0,0,640,360, DAR = 1.774, SAR = 1.000
Resize Relative Cropping :- CropW Relative = 0, CropH Relative = 0
Resize Abs Cropping :- CropRight Abs = 0, CropBottom Abs = 0
AutoLevel :- true
Levels if AUTOLEVEL True :- Levels(17,1.0,239,16,235,Coring=False)

The input Video file is :- D:\VID\3.MP4
The created AVS file is :- D:\OUT\3_MP4.AVS
The created CMD file is :- D:\OUT\3_MP4.CMD eg 1.mpg file is converted 1_mpg.CMD not 1.mpg.CMD, avoids anti malware false +ve.
The Base filename is :- 3_MP4
Original Size :- 640x480 with a DAR of 1.333 and SAR of 1.000
Resize to :- 472x354 with Cropping 0,0,640,480, DAR = 1.333, SAR = 1.000
Resize Relative Cropping :- CropW Relative = 0, CropH Relative = 0
Resize Abs Cropping :- CropRight Abs = 0, CropBottom Abs = 0
AutoLevel :- true
Levels if AUTOLEVEL True :- Levels(12,1.0,255,16,235,Coring=False)

The input Video file is :- D:\VID\4.flv
The created AVS file is :- D:\OUT\4_flv.AVS
The created CMD file is :- D:\OUT\4_flv.CMD eg 1.mpg file is converted 1_mpg.CMD not 1.mpg.CMD, avoids anti malware false +ve.
The Base filename is :- 4_flv
Original Size :- 432x240 with a DAR of 1.800 and SAR of 1.000
Resize to :- 472x262 with Cropping 0,0,432,240, DAR = 1.802, SAR = 1.000
Resize Relative Cropping :- CropW Relative = 0, CropH Relative = 0
Resize Abs Cropping :- CropRight Abs = 0, CropBottom Abs = 0
AutoLevel :- true
Levels if AUTOLEVEL True :- Levels(16,1.0,236,16,235,Coring=False)

The input Video file is :- D:\VID\5.wmv
The created AVS file is :- D:\OUT\5_wmv.AVS
The created CMD file is :- D:\OUT\5_wmv.CMD eg 1.mpg file is converted 1_mpg.CMD not 1.mpg.CMD, avoids anti malware false +ve.
The Base filename is :- 5_wmv
Original Size :- 720x540 with a DAR of 1.333 and SAR of 1.000
Resize to :- 472x354 with Cropping 0,0,720,540, DAR = 1.333, SAR = 1.000
Resize Relative Cropping :- CropW Relative = 0, CropH Relative = 0
Resize Abs Cropping :- CropRight Abs = 0, CropBottom Abs = 0
AutoLevel :- true
Levels if AUTOLEVEL True :- Levels(1,1.0,255,16,235,Coring=False)


Group Command output (will not show them individually, they look the same).

"D:\TEST\ffmpeg.exe" -i "D:\OUT\1_mpg.AVS" -vcodec libx264 -crf 21.5 -s 472x348 -aspect 1.356 -acodec libvo_aacenc -ab 128k "D:\OUT\1_mpg.MKV"

"D:\TEST\ffmpeg.exe" -i "D:\OUT\2_avi.AVS" -vcodec libx264 -crf 21.5 -s 472x266 -aspect 1.774 -acodec libvo_aacenc -ab 128k "D:\OUT\2_avi.MKV"

"D:\TEST\ffmpeg.exe" -i "D:\OUT\3_MP4.AVS" -vcodec libx264 -crf 21.5 -s 472x354 -aspect 1.333 -acodec libvo_aacenc -ab 128k "D:\OUT\3_MP4.MKV"

"D:\TEST\ffmpeg.exe" -i "D:\OUT\4_flv.AVS" -vcodec libx264 -crf 21.5 -s 472x262 -aspect 1.802 -acodec libvo_aacenc -ab 128k "D:\OUT\4_flv.MKV"

"D:\TEST\ffmpeg.exe" -i "D:\OUT\5_wmv.AVS" -vcodec libx264 -crf 21.5 -s 472x354 -aspect 1.333 -acodec libvo_aacenc -ab 128k "D:\OUT\5_wmv.MKV"

Pause


Probably not optimal (I dont use ffmpeg much) but works pretty good.
Output MKV's all AutoCropped/Resized and AutoLeveled.
Alternatively, could feed the avs files into MeGUI and do them that way, also signals DAR to MeGUI.
(MeGUI AutoEncode Batcher, might assist if you have a lot of avs files to encode).

gurusinghbrar
6th September 2012, 14:00
hey StainlesS many many thank for the script that you created it solve most of my problem..
and in other word we near to our final point not on final...

For our script help lot to encode with lot of file which have no problem with sound in most of movies. (thanks for it)

and first of all i want to use orignal file as input not avs for ffmpeg converting. so i change in CMD template like this.
code:

"D:\TEST\ffmpeg.exe" -i "[VIDEOFILE]" (not avs file use) -vcodec libx264 -crf 21.5 -s [SIZEX]x[SIZEY] -aspect [DAR] -acodec libvo_aacenc -ab 128k "D:\OUT\[BASEFILE].MKV"
it done most of my work. but now only one thing like "-vf crop=" method with not implement.

because sometime due to fps problem with video file (some dual audio movies) when i convert avs file. the output file audio video not sync. this problem it not occur with using original file instead of avs file as input in ffmpeg / x264.

and now i give you my batch file code use with ffmpeg, and now only one thing not auto set via ur script show in color full...
code:-

ffmpeg -i "[VIDEOFILE]" -f mp4 -r 25 -ss 30 -t 10 -vcodec libx264 -keyint_min 25 -profile:v main -x264opts weightp=2:b-pyramid=none:scenecut=40:rc-lookahead=40:b-adapt=2:level=30:keyint=250:min-keyint=25:no-dct-decimate=1:aq-mode=1:aq-strength=1.0:subme=7:me=umh:merange=16:no-fast-pskip=1:bframes=2:direct=auto:mixed-refs=1:trellis=2:weightb=1:ref=2: psy-rd=0/0:bitrate=300:vbv-maxrate=1000:vbv-bufsize=150:qcomp=0:sar=1/1:threads=3 -s [SIZEX]x[SIZEY] -vf crop=%crop%,mp=eq=10.0:5.0,unsharp=7:7:2.5 -bufsize 150000 -minrate 10000 -maxrate 10000 -threads 3 -vol 512 -acodec libvo_aacenc -b:a 48k -ar 32000 -ac 2 -metadata title="Encode By GuruSinghBrar" -y M4V00001.mp4


for cropping method in ffmpeg i give u small explanation for it... or u can use google.

Explain.....
--------------------

-vf crop=width:height:x:y

Width and height is the dimesions of the cropped image after cropping. X and y is the coordinate of the upper left corner of the cropping rectangle where x=0 corresponds to left edge of original frame and y=0 the top edge of the original frame.

Example:
Original frame is 1280x720. You want to crop 10 pixels from top and bottom but leave the width uncropped:
-vf crop=1280:700:0:10
Here the new height after cropping is 700 pixels and the upper left corner of the cropped image is placed at x=0 and y=10 on the original frame. Since the new height is 20 pixels less than the original it will crop 10 pixels at top and bottom. See it like a crop tool where you first set the dimensions of the cropping rectangle and then place the rectangle somewhere in the original picture where the coordinates x and y is where to place the cropping rectangle based on upper left corner of the rectangle.

I think your example is only cropping 10 pixels from bottom because you place the cropping rectangle at top left of the original image and then reduce the height of the cropped image to 10 pixels less than the original. So the post in your link is probably wrong, because I don't see how that should work. To crop only 20 pixels from bottom (as the poster asked for) he should use:
-vf crop=in_w:in_h-20:0:0

To crop 20 pixels from bottom and 20 pixels from top:
-vf crop=in_w:in_h-40:0:20

thank you StainlesS

gurusinghbrar
6th September 2012, 14:30
hey wait i am changing some [var] in your template according to me, and it should be meet my config.. :)
done done done:::: thanks for you lot of help and making the script for me....

so all file's created with your script and some changes in template it finally work for me.....

changes in: Template_cmd.temt

"D:\TEST\ffmpeg.exe" -i "[VIDEOFILE]" -vcodec libx264 -crf 21.5 -s [SIZEX]x[SIZEY] -vf crop=[CROPW]:[CROPH]:[CROPX]:[CROPY] -aspect [DAR] -acodec libvo_aacenc -ab 128k "D:\OUT\[BASEFILE].MKV"


its works... :) :D

StainlessS
6th September 2012, 15:19
Glad that works for you.
The eg, CROPW, CROPWREL and CROPRGT, are different ways of specifying the same thing, I implemented CROPRGT
to go with eg ffmpeg -cropright, but apparently it's been removed from current versions in favour of your used '-vf crop =' filter mode,
it may however be useful to generate command files for some other comand line encoder.

You can then switch off AutoLevels, as it serves no purpose in cmd generation, I think ffmpeg can do its own autolevel if needed.

EDIT: Out of interest, which command files are of more use to you, the separate ones or the group command ?

EDIT: You might like to change the name of the cmd template to eg FFMPEG_CMD.TEMT or whatever,
if there is likelihood of your wanting to generate for other cli encoders.

gurusinghbrar
6th September 2012, 16:44
yaa,,,, in new ffmpeg they shift the crop section in video filter and use can be like this "-vf crop=width:height:x:y". so i change in the cmd template as i want. and now i want to share my edit cmd template with on doom9. i think it will help the people lot...

(working for all you must install latest k-lite codec pack with ffdshow codec decoder and avisynth2.5.8 )

steps:-

1. make a folder name "Convert".

2. make AutoCropLevelsGenny.avs or (MainScript.avs as you want) ( for coding check this http://forum.doom9.org/showthread.php?p=1590078#post1590078) in "Convert" Folder.

little change in this file ("AutoCropLevelsGenny.avs") according to me to these steps working smooth. and you can change "import" or "loadplugin" value in script as your configuration:

# Option to create 1 single Group command CMD file.

import(".\TOOLS\QueryBorderCrop.AVSI")
import(".\TOOLS\QueryLumaMinMAx.Avsi")
Loadplugin(".\TOOLS\RT_Stats.dll")
Loadplugin(".\TOOLS\GScript.dll")

# EDIT To Suit

DIR = ".\" # Video Input Directory
OUTDIR = ".\" # Output and Temp Directory
INFILE = "*.AVI|MP*G|WMV|MP4|MKV|flv" # Input file wildcards, add whatever.
TEMPLATE = ".\TOOLS\TEMPLATE_AVS.TEMT" # AVS file TEMPLATE.
CMDTEMPLATE = ".\TOOLS\TEMPLATE_CMD.TEMT" # CMD file TEMPLATE
LOGTEMPLATE = ".\TOOLS\TEMPLATE_LOG.TEMT" # LOG file TEMPLATE
MEDIAINFO = ".\TOOLS\MediaInfo.Exe" # Path to MediaInfo.Exe CLI version
OUTLOG = ".\OUTLOG.TXT" # Output log file name without path.
GROUPCMDFILE = ".\GROUP.CMD" # filename incl path for single combined group CMD file.



2. make a folder name "TOOLS" in "CONVERT" folder.

3. copy the all file in "TOOLS" folder:-
MediaInfo.dll,
MediaInfo.exe,
ffmpeg.exe,
GScript.dll,
RT_Stats.dll (v1.04 minimum),
QueryBorderCrop.AVSI,
QueryLumaMinMAx.Avsi
TEMPLATE_AVS.TEMT ( for coding check this http://forum.doom9.org/showthread.php?p=1590079#post1590079 )
TEMPLATE_CMD.TEMT ( for coding check this http://forum.doom9.org/showthread.php?p=1590079#post1590079 )
TEMPLATE_LOG.TEMT ( for coding check this http://forum.doom9.org/showthread.php?p=1590079#post1590079 )

4. now here is my "TEMPLATE_CMD.TEMT" just copy the matter from Code: and save in "TEMPLATE_CMD.TEMT" file without quote in "TOOLS" folder.

@Echo Off
pause
echo ===========================================================================
echo Enter The Values For Converting Video File for SONY - PSP
echo ===========================================================================
echo.

set/p "video= Video Bitrate (300 default) > " # Ask you bitrate for video in runtime
set/p "audio= Audio Bitrate (48/64 default) > " # Ask you bitrate for audio in runtime
set/p "vol= Volume (256 default) > " # Ask you audio loud (256 default and 512 can be double) in runtime for make audio with gain volume


for %%I in ("*.mp4" "*.mkv" "*.avi") do set out=%%I
set out=%out:~0,-4%

"TOOLS\ffmpeg.exe" -i "[VIDEOFILE]" -f mp4 -r 25 -vcodec libx264 -keyint_min 25 -profile:v main -x264opts weightp=2:b-pyramid=none:scenecut=40:rc-lookahead=40:b-adapt=2:level=30:keyint=250:min-keyint=25:no-dct-decimate=1:aq-mode=1:aq-strength=1.0:subme=7:me=umh:merange=16:no-fast-pskip=1:bframes=2:direct=auto:mixed-refs=1:trellis=2:weightb=1:ref=2:psy-rd=0/0:bitrate=%video%:vbv-maxrate=1000:vbv-bufsize=150:qcomp=0:sar=1/1:threads=3 -s [SIZEX]x[SIZEY] -vf crop=[CROPW]:[CROPH]:[CROPX]:[CROPY] -aspect [DAR] -bufsize 150000 -minrate 10000 -maxrate 10000 -threads 3 -vol %vol% -acodec libvo_aacenc -b:a %audio%k -ar 32000 -ac 2 -metadata title="Video Encode By GuruSinghBrar" -y "%out%_OUT.MP4"

for %%I in ("*.mp4" "*.mkv") do set in=%%I
tools\ffmpeg -i "%in%" -f image2 -ss 15 -vframes 1 -s 160x120 -an -y MOVIE.THM




5. Now copy the video like "video.mp4" (any name) in "CONVERT" folder.

6. Run the avs file with windows media player.

7. if it will show you "Processed OK" done and close media player.

8. and now run the CMD file with the video file name.



try this and post here what is the problem.

EDIT: or try to download file in attachment and download ffmpeg from here: http://ffmpeg.zeranoe.com/builds/ in "32-bit Builds (Static)"
and download mediainfo CLI here: http://mediainfo.sourceforge.net/en/Download/Windows
and Extracted files put in "TOOL" Folder.

EnJoy...

And Specially thanks To StainlesS and Doom9 Site, for Helping Me. :)