Urik_Kane

ffmpeg_convert_to_gif_custom

Apr 8th, 2021 (edited)
635
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Batch 3.38 KB | None | 0 0
  1. @echo off
  2. SetLocal EnableDelayedExpansion
  3. set title1=Convert to GIF
  4. title %title1%
  5.  
  6. if "%~1"=="" ( echo ERROR: NO INPUT & echo  & pause & goto :eof ) else ( title %title1%^: "%~nx1")
  7.  
  8. :: you can override exe paths here. for example, set "ffmpeg=C:\ffmpeg\bin\ffmpeg.exe"
  9. set "ffmpeg=ffmpeg.exe"
  10.  
  11. :: only these file extensions will be processed by the script
  12. set supported_formats=avi,bik,flv,m2ts,m4p,m4v,mkv,mov,mp2,mp4,mpg,mpeg,mpe,mpv,mts,qt,swf,wmv,xmf,webm
  13.  
  14. :: optional default subfolder to output to. for example, set "subfolder=converted"
  15. set subfolder=
  16.  
  17. :CheckFFMPEG
  18. if not exist "%ffmpeg%" (
  19.     if exist "%~dp0ffmpeg.exe" set "ffmpeg=%~dp0ffmpeg.exe" & goto :CheckFFPROBE
  20.     ffmpeg.exe>nul 2>&1
  21.     if !errorlevel!==9009 CALL :PrintMessage "ERROR: FFMPEG NOT FOUND / INSTALLED. EXITING." & echo  & pause & goto :eof
  22. )
  23.  
  24. :CountFiles
  25.  
  26. :: count files
  27. set total=0
  28. for %%F in (%*) do (
  29.     if not "%%~xF"=="" (
  30.         for %%A in (%supported_formats%) do (
  31.             if /i %%~xF==.%%A set /a total=!total!+1
  32.         )
  33.     )
  34. )
  35. if %total%==0 echo  & CALL :PrintMessage "NO FILES TO PROCESS" "EXITING" & pause>nul & goto :eof
  36. if %total% GTR 1 title %title1%^: %total% files
  37.  
  38. :Resize
  39. if defined width goto :FrameRate
  40. set width=
  41. echo.
  42. set /p width="resize (width) = "
  43. if not "!width!"=="" (
  44.     set "width=!width: =!"
  45.     for /f "delims=1234567890" %%A in ('echo !width!') do ( echo invalid number & goto :Resize )
  46.     if !width! LSS 5 echo invalid number & goto :Resize
  47. )
  48.  
  49. :FrameRate
  50. if defined framerate goto :ConformFilters
  51. set frate=
  52. echo.
  53. set /p frate="framerate = "
  54. if not "!frate!"=="" (
  55.     set "frate=!frate: =!"
  56.     for /f "delims=1234567890" %%A in ('echo !frate!') do ( echo invalid number & goto :FrameRate)
  57.     if !frate! LSS 1 echo invalid number & goto :FrameRate
  58.     set "newframerate=!frate!"
  59. )
  60.  
  61. :ConformFilters
  62. if not "!width!"=="" set filters=scale='min(!width!,iw)':-1:flags=lanczos
  63. if not "!newframerate!"=="" ( if "!filters!"=="" ( set filters=fps=!newframerate!) else ( set filters=!filters!,fps=!newframerate!))
  64. if not "!filters!"=="" ( set filters=-vf "!filters!,split[s0][s1];[s0]palettegen[p];[s1][p]paletteuse") else ( set filters=-vf "split[s0][s1];[s0]palettegen[p];[s1][p]paletteuse")
  65.  
  66. :BatchProcess
  67. set current=
  68. for %%F in (%*) do (
  69.     if not "%%~xF"=="" (
  70.         for %%A in (%supported_formats%) do ( if /i %%~xF==.%%A CALL :ProcessFile "%~1")
  71.     )
  72. )
  73. title Finished
  74. goto :eof
  75.  
  76. :: MAIN ROUTINE
  77. :ProcessFile
  78. if "!current!"=="" ( set current=1) else ( set /a current=!current!+1)
  79. if %total% GTR 1 set "ratio= !current!/!total!"
  80. title %title1%^:!ratio! "%~nx1"
  81. if not "%cd%\"=="%~dp1" cd /d "%~dp1"
  82.  
  83. :Add_Postfix
  84. if exist "%~n1.gif" (
  85.     for /L %%N in (1,1,1000) do (
  86.         if not exist "%~n1_%%N.gif" ( set "postfix=_%%N" & goto :FFmpeg)
  87.     )
  88. )
  89.  
  90. :FFmpeg
  91. "%ffmpeg%" -i "%~1" -an !filters! "%~n1!postfix!.gif"
  92. if not !errorlevel!==0 CALL :ErrorSubroutine "!outpath!%~n1%suffix%!postfix!.%ext%"
  93. goto :eof
  94.  
  95. :: if output file is empty, deletes it. prints some messages. stores goterror var.
  96. :ErrorSubroutine
  97. if "%~z1"=="0" del %1 & ( echo  & echo ERROR: FILE OUTPUT FAILED & echo.)
  98. set goterror=true
  99. pause
  100. goto :eof
  101.  
  102. :: UTILITIES
  103.  
  104. :PrintMessage
  105. echo.
  106. echo ---------------------------------------------------------------
  107. echo  %~1
  108. if not "%~2"=="" echo  %~2
  109. if not "%~3"=="" echo  %~3
  110. if not "%~4"=="" echo  %~4
  111. echo ---------------------------------------------------------------
  112. goto :eof
  113.  
Add Comment
Please, Sign In to add comment