VideoHelp Forum
+ Reply to Thread
Results 1 to 18 of 18
Thread
  1. Hello, i tried both Handbrake and Staxrip and these two has a auto crop option for black borders, now i am using x264 itself with a cmd batch script for encoding but i couldnt crop black borders, i dont know how to get crop values for using them in avs script, pleae help me, is there any way to auto crop video or getting crop values.

    thanks a lot
    Quote Quote  
  2. Open your script in VirtualDub, Add Null Transform filter, Select Crop tool. Get values.

    Or export a frame as an image. Open it in an image editor, get coordinates.

    Or open your video in Handbrake, use the values its autocrop gives you.
    Last edited by jagabo; 14th May 2019 at 13:10.
    Quote Quote  
  3. You can use ffmpeg:
    Code:
    @ffmpeg -skip_frame nokey -y -hide_banner -loglevel 32 -stats -i "%1" -vf cropdetect -an -f null -
    Quote Quote  
  4. Originally Posted by jagabo View Post
    Open your script in VirtualDub, Add Null Transform filter, Select Crop tool. Get values.

    Or export a frame as an image. Open it in an image editor, get coordinates.

    Or open your video in Handbrake, use the values its autocrop gives you.
    Thanks, but i wrote a script in cmd to encode movies with my settings automatically so i need to get just crop values, i need a tool like mediainfo that i use this to get bitrate or width or ... for getting just crop values and then storing them into variables then importing them in avs script
    Quote Quote  
  5. Member Budman1's Avatar
    Join Date
    Jul 2012
    Location
    NORTHWEST ILLINOIS, USA
    Search Comp PM
    If you have avisynth installed you can create a AVSfile (video.avs) and play it in many on the players that support avisynth script (Potplayer, WMP, MPC-BE, etc)

    Code:
    LoadPlugin("C:\Users\Bud\Desktop\MEGA_Trimmed_Tabbed_2_2\bin\Debug\AutoCrop.dll")
    DirectShowSource(("C:\Users\Bud\Desktop\250_red.mp4"), Pixel_Type="yuy2").AutoCrop(mode=1, threshold=30)
    Threshold may be adjusted greater or less depending on results

    ;Result:

    Image
    [Attachment 49052 - Click to enlarge]
    Quote Quote  
  6. Originally Posted by pandy View Post
    You can use ffmpeg:
    Code:
    @ffmpeg -skip_frame nokey -y -hide_banner -loglevel 32 -stats -i "%1" -vf cropdetect -an -f null -
    is there any way to get the ffmpeg output and save it in txt then extract the crop value from it and use it?
    Quote Quote  
  7. Member Budman1's Avatar
    Join Date
    Jul 2012
    Location
    NORTHWEST ILLINOIS, USA
    Search Comp PM
    Avisynth autocrop will show, automatically crop. Save in autocrop.log or combination of these.
    https://avisynth.org.ru/docs/english/externalfilters/autocrop.htm
    Quote Quote  
  8. Originally Posted by m00511 View Post
    Originally Posted by pandy View Post
    You can use ffmpeg:
    Code:
    @ffmpeg -skip_frame nokey -y -hide_banner -loglevel 32 -stats -i "%1" -vf cropdetect -an -f null -
    is there any way to get the ffmpeg output and save it in txt then extract the crop value from it and use it?
    Like any command line program, you can redirect the output to a text file. stderr is the output stream:

    Code:
    ffmpeg -skip_frame nokey -y -hide_banner -loglevel 32 -stats -i "%~dpnx1" -vf cropdetect -an -f null - 2>autocrop.txt
    Quote Quote  
  9. Originally Posted by Budman1 View Post
    Avisynth autocrop will show, automatically crop. Save in autocrop.log or combination of these.
    https://avisynth.org.ru/docs/english/externalfilters/autocrop.htm
    Thanks, can u give the code, i dont how to do that
    Quote Quote  
  10. Originally Posted by m00511 View Post
    Originally Posted by Budman1 View Post
    Avisynth autocrop will show, automatically crop. Save in autocrop.log or combination of these.
    https://avisynth.org.ru/docs/english/externalfilters/autocrop.htm
    Thanks, can u give the code, i dont how to do that
    He gave you a link to the description of the filter and sample AviSynth code already.
    Quote Quote  
  11. Originally Posted by jagabo View Post
    Originally Posted by m00511 View Post
    Originally Posted by Budman1 View Post
    Avisynth autocrop will show, automatically crop. Save in autocrop.log or combination of these.
    https://avisynth.org.ru/docs/english/externalfilters/autocrop.htm
    Thanks, can u give the code, i dont how to do that
    He gave you a link to the description of the filter and sample AviSynth code already.
    Code:
    LoadPlugin(".\ffms2\ffms2.dll") 
    LoadPlugin(".\autocrop\autocrop.dll") 
    LoadPlugin(".\VSFilterMod\VSFilterMod.dll") 
    FFVideoSource("video.mkv", colorspace = "YV12", \ 
                  cachefile = "video.ffindex") 
    AutoCrop(mode=1, threshold=30)
    This my avs script but it doesnt work, i can get the value from an third app and then put the values in script myself but i need to do it automatically so i want to save sth like this to a text file (Crop(0,94,-0,-96)) and then import it to avs script or saving it directly into avs script without my hand
    Quote Quote  
  12. Originally Posted by jagabo View Post
    See mode=2.
    Thanks a lot, now i have a new question how can i resize and crop the video for example i have a video with 1920*1080 and with auto crop it will be 1920*804 but i need it to be 1280*536
    Quote Quote  
  13. Why don't you crop and resize in AviSynth and encode the script with x264? After cropping you can resize with:

    Code:
    Spline36Resize(1280, ((height*1280/width+2)/4)*4)
    That will round to the nearest mod4 height.
    Quote Quote  
  14. Originally Posted by m00511 View Post
    Originally Posted by jagabo View Post
    See mode=2.
    Thanks a lot, now i have a new question how can i resize and crop the video for example i have a video with 1920*1080 and with auto crop it will be 1920*804 but i need it to be 1280*536
    You can resize first and then autocrop, because having nice legit source most likely. Using legit DVD or BD I use automatic batch script like that and never had a problem. You can use something like :
    Code:
    LoadPlugin(".\ffms2\ffms2.dll") 
    LoadPlugin(".\autocrop\autocrop.dll") 
    LoadPlugin(".\VSFilterMod\VSFilterMod.dll") 
    FFVideoSource("video.mkv", colorspace = "YV12", \ 
                  cachefile = "video.ffindex") 
    Spline36Resize(1280,720)
    AutoCrop(2, 4, 4,) #this is mode 4
    this gives you that log with that crop line, then make a script again with that line and take away that autocrop
    I have a script that does it all at once, it is quite long because it takes care of other things...

    EDIT: to run a script and do nothing, just to get that log with crop line, you can load it into VirtualDub2 or if using command line only, you just load it into avsmeter for example:
    "AVSMeter.exe" "your.avs" -log -range=0,1 2>nul
    Last edited by _Al_; 15th May 2019 at 15:09.
    Quote Quote  
  15. Originally Posted by _Al_ View Post
    Originally Posted by m00511 View Post
    Originally Posted by jagabo View Post
    See mode=2.
    Thanks a lot, now i have a new question how can i resize and crop the video for example i have a video with 1920*1080 and with auto crop it will be 1920*804 but i need it to be 1280*536
    You can resize first and then autocrop, because having nice legit source most likely. Using legit DVD or BD I use automatic batch script like that and never had a problem. You can use something like :
    Code:
    LoadPlugin(".\ffms2\ffms2.dll") 
    LoadPlugin(".\autocrop\autocrop.dll") 
    LoadPlugin(".\VSFilterMod\VSFilterMod.dll") 
    FFVideoSource("video.mkv", colorspace = "YV12", \ 
                  cachefile = "video.ffindex") 
    Spline36Resize(1280,720)
    AutoCrop(2, 4, 4,) #this is mode 4
    this gives you that log with that crop line, then make a script again with that line and take away that autocrop
    I have a script that does it all at once, it is quite long because it takes care of other things...

    EDIT: to run a script and do nothing, just to get that log with crop line, you can load it into VirtualDub2 or if using command line only, you just load it into avsmeter for example:
    "AVSMeter.exe" "your.avs" -log -range=0,1 2>nul
    I'm going to try it, yes i have a script that do everything even resizeing but not cropping and after i did it couldnt resize it, can i use mod 3 and do noting else?
    Quote Quote  
  16. Sure, just try it, I never used it, but it might just work.

    With mode 2 I checked what it would do, to not crop couple of pixels for example if DVD, or some BD animation could have some two, four pixels bands, that I'd certainly left alone. This is for example autocrop where it would leave 10 pixels alone, not cropping it, only blacks more than 10 pixels would be cropped, and some other stuff, like checking width, height, etc, don't do it, it is too complicated:
    Code:
    :autocrop <avisynth script>  <video input>  <%title%_%stream%>
    rem getting horizontal and vertical resolution coming out of avisynth script
    copy "%~1" "write_image.avs" >nul
    echo converttorgb() >> "write_image.avs"
    echo ImageWriter(file = "%temp_folder%\", start = 0, end = 0, type = "jpeg") >> "write_image.avs"
    echo return(last) >> "write_image.avs"
    %virtualdub% /s %load_script_vcf% /p "write_image.avs",NUL /r >NUL
    for %%i in ("*.jpeg") do set frame=%%~ni.jpeg
    rem run mediainfo to return w and h (width and height) coming out of avisynth script before autocrop is applied
    call :run_mediainfo "%frame%" "noprint"
    echo avisynth output resolution before autocrop is %w%x%h%, getting cropping values ...
    REM AutoCrop.dll identifies black borders in video and then generates avisynth crop line in AutoCrop.log
    REM The way it is done, Avisynth script has to load AutoCrop.dll followed by Autocrop(2,width_integer,height_integer) line and that generates crop line into AutoCrop.log
    copy "%~1" "autocrop.avs" > nul
    echo LoadPlugin("%autocrop_dll%") >> "autocrop.avs"
    echo autocrop(2,%w_integer%,%h_integer%)    >> "autocrop.avs"
    if /i "%indexing%"=="ffvideosource" if not exist "%~dpn2.ffindex" echo ffvideosource is indexing video, it might take a little while ...
    %virtualdub% /s %load_script_vcf% /p "autocrop.avs",NUL /r >NUL
    set /p autocrop_avisynth_line= < "AutoCrop.log"
    if not exist AutoCrop.log ( 
       echo    WARNING,   Autocrop failed, autocrop dll did not create AutoCrop.log, so no cropping was processed
       goto :eof
       )
    echo AutoCrop.dll generated this avisynth crop line: %autocrop_avisynth_line%
    rem parsing values from that line
    for /f "tokens=2,3,4,5 delims=,()" %%m in ("%autocrop_avisynth_line%") do (set cropleft=%%m& set croptop=%%n& set w_autocrop=%%o& set h_autocrop=%%p)
    if %w% EQU %w_autocrop% if %h% EQU %h_autocrop%  (
       echo No letterbox or pillarbox detected, no cropping was applied
       echo #No letterbox or pillarbox detected from autocrop.avs, no cropping was applied >> "%~1"
       goto :eof
       )
    echo loaded integers for cropping were:  wMultOf=%w_integer%, hMultOf=%h_integer%
    REM do not crop if just tiny borders are present (basically if it is not letterbox or pillarbox) to not screw up resolutions
    set /a cropright=%w%-%w_autocrop%-%cropleft%
    if not %cropleft% EQU 0 if %cropleft% LEQ 10 ( 
      set /a w_autocrop=%cropleft%+%w_autocrop%
      set cropleft=0
      echo Negligent padding on the left, %cropleft% pixels, will be ignored
      echo # Negligent padding on the left, %cropleft% pixels, will be ignored >> "%~1"
      )
    if not %cropright% EQU 0 if %cropright% LEQ 10 (
      set /a w_autocrop=%cropright%+%w_autocrop%
      echo Negligent padding on the right, %cropright% pixels, will be ignored
      echo # Negligent padding on the right, %cropright% pixels, will be ignored  >> "%~1"
      )
    set /a cropbottom=%h%-%h_autocrop%-%croptop%
    if not %croptop% EQU 0 if %croptop% LEQ 10 (
      set /a h_autocrop=%croptop%+%h_autocrop%
      set croptop=0
      echo Negligent padding on the top, %croptop% pixels, will be ignored
      echo # Negligent padding on the top, %croptop% pixels, will be ignored >> "%~1"
      )
    if not %cropbottom% EQU 0 if %cropbottom% LEQ 10 (
      set /a h_autocrop=%cropbottom%+%h_autocrop%
      echo Negligent padding on the bottom, %cropbottom% pixels, will be ignored
      echo # Negligent padding on the bottom, %cropbottom% pixels, will be ignored >> "%~1"
      )
    echo Crop(%cropleft%,%croptop%,%w_autocrop%,%h_autocrop%)  >> "%~1"
    set w=%w_autocrop%
    set h=%h_autocrop%
    echo generated avisynth crop line: Crop^(%cropleft%,%croptop%,%w_autocrop%,%h_autocrop%^),   video resolution changes to: %w%x%h%
    goto :eof
    Quote Quote  
  17. @echo off
    set ffmpeg64="C:\Progra~1\converti_bat\ffmpegN1025.exe "
    set sett=-c:v libx264 -crf 23 -c:a aac -b:a 160k
    rem set sett=-c:v libx265 -crf 25 -c:a aac -b:a 160k
    %ffmpeg64% -ss 90 -i "%~1" -vframes 10 -vf cropdetect -f null -
    %ffmpeg64% -ss 90 -i "%~1" -vframes 10 -vf cropdetect -f null - 2>"%temp%\null.txt"
    findstr /ic:Parsed_cropdetect "%temp%\null.txt">"%temp%\nu.txt"
    for /f "usebackq tokens=*" %%A in ("%temp%\nu.txt") do (
    for /f "tokens=1-2 delims==" %%a in ("%%~A") do (
    set "cr=%%b"
    ))
    del "%temp%\nu.txt"
    del "%temp%\null.txt"
    set cro=-vf crop=%cr%
    choice /C "sn" /t 15 /D "n" /M "setting manuale ?"
    SET ort=%ERRORLEVEL%
    if "%ort%" equ "1" set /p "cr=crop=w:h:y:"
    if "%ort%" equ "1" set cro=-vf crop=%cr%
    IF "%ort%" EQU "2" echo no
    if "%ort%" equ "2" goto fin
    :fin
    %ffmpeg64% -i "%~1" %cro% -y %sett% "%~n1_.mp4"
    exit
    Quote Quote  



Similar Threads

Visit our sponsor! Try DVDFab and backup Blu-rays!