Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- @echo off
- SetLocal EnableDelayedExpansion
- set title1=FFmpeg batch rewrap
- title %title1%
- REM this script can accept multiple input files. input can be folder(s) too. only %supported_formats% will be processed.
- REM if input is a folder, and has subfolders, output should preserve the subdirectory tree in destination
- REM all destination folders will get created automatically
- REM for output path, it accepts either complete path like E:\converted files or a subfolder. If no drive letter like E:, it will treat it as subfolder of input
- if "%~1"=="" ( echo ERROR: NO INPUT & echo & pause & goto :eof ) else ( title %title1%^: "%~nx1")
- :: you can override exe paths here. for example, set "ffmpeg=C:\ffmpeg\bin\ffmpeg.exe"
- set "ffmpeg=ffmpeg"
- set "ffprobe=ffprobe"
- :: note: if you put ffmpeg.exe/ffprobe.exe and this script in the same folder, that would also work
- :: only these file extensions will be processed by the script
- set supported_formats=avi,mp4,mkv,mov,mts,m2ts,xmf,wmv
- :: whether to play a chime sound after completion
- set playsound=true
- :: the sound to play. you can change
- set "chime=C:\Windows\Media\tada.wav"
- :: whether to open the output folder after completion
- set open_folder=true
- :: whether to add suffix to output files. for example, set "suffix=_fixed"
- set suffix=
- :: optional default subfolder to output to. for example, set "subfolder=converted"
- set subfolder=
- :: override output extension, then it will not ask
- set ext=
- :CheckSubfolderVar
- if not "%subfolder%"=="" (
- set subfolder=%subfolder: =%
- for %%A in ("!subfolder!") do ( set "subfolder=%%~A" & goto :CheckFFMPEG)
- )
- set subfolder=
- :CheckFFMPEG
- if not exist "%ffmpeg%" (
- if exist "%~dp0ffmpeg.exe" set "ffmpeg=%~dp0ffmpeg.exe" & goto :CheckFFPROBE
- ffmpeg>nul 2>&1
- if !errorlevel!==9009 CALL :PrintMessage "ERROR: FFMPEG NOT FOUND / INSTALLED. EXITING." & echo & pause & goto :eof
- )
- :CheckFFPROBE
- if not exist "%ffprobe%" (
- if exist "%~dp0ffprobe.exe" set "ffprobe=%~dp0ffprobe.exe" & goto :CountFiles
- ffprobe>nul 2>&1
- if !errorlevel!==9009 CALL :PrintMessage "ERROR: FFPROBE NOT FOUND / INSTALLED. EXITING." & echo & pause & goto :eof
- )
- :CountFiles
- :: replace bad characters for batch
- set files=%*
- set files=%files:,=\comma\%
- set files=%files:(=\obr\%
- set files=%files:)=\cbr\%
- :: count files
- set total=0
- for %%F in (%files%) do (
- if "%%~xF"=="" (
- CALL :CountFolder "%%~F"
- ) else (
- for %%A in (%supported_formats%) do ( if /i %%~xF==.%%A set /a total=!total!+1)
- )
- )
- if %total%==0 echo & CALL :PrintMessage "NO FILES TO PROCESS" "EXITING" & pause & goto :eof
- if %total% GTR 1 title %title1%^: %total% files
- if not "%ext%"=="" goto :EnterPath
- :EnterExtension
- set ext=
- echo.
- set /p ext="output extension "
- for %%A in (!ext!) do ( if not ""%%~xA=="" set "ext=%%~xA")
- if not "!ext!"=="" (
- set "ext=!ext: =!"
- set "ext=!ext:.=!"
- for /f "delims=abcdefghijklmnopqrstuvwxyz1234567890" %%A in ('echo !ext!') do (
- echo ERROR: input incorrect
- goto :EnterExtension
- )
- ) else goto :EnterExtension
- for %%X in (mov,mp4) do ( if /i "%ext%"=="%%X" ( set "format_options=-movflags +faststart" & goto :EnterPath))
- :EnterPath
- if "%subfolder%"=="" ( set "msg=output path = ") else ( set "msg=output path (default %subfolder%) ")
- set p=
- echo.
- set /p p="%msg%"
- if not "%p%"=="" (
- for %%F in ("!p!") do (
- set "outfolder=%%~F"
- goto :ConformPath
- )
- )
- :ConformPath
- echo.
- if not "!outfolder!"=="" (
- if not "!outfolder:~-1!"=="\" (
- if "!outfolder:~-1!"=="/" set "outfolder=!outfolder:~0,-1!"
- set "outfolder=!outfolder!\"
- for %%A in ("%outfolder%") do (
- echo "%%~dA"| find /i "%~d1" > nul 2>&1
- if not !ErrorLevel!==0 ( set openfolder=true & goto :BatchProcess)
- )
- for %%A in ("%outfolder%") do (
- echo "%%~dpA"| find /i "%~dp1" > nul 2>&1
- if not !ErrorLevel!==0 ( set openfolder=true & goto :BatchProcess)
- )
- )
- )
- :BatchProcess
- set current=
- for %%F in (%files%) do (
- set "file=%%~F"
- set "file=!file:\comma\=,!"
- set "file=!file:\cbr\=)!"
- set "file=!file:\obr\=(!"
- if "%%~xF"=="" (
- CALL :ProcessFolder "!file!"
- ) else (
- for %%A in (%supported_formats%) do ( if /i %%~xF==.%%A CALL :ProcessFile "!file!")
- )
- )
- title Finished
- if defined openfolder if %open_folder%==true explorer "%outfolder%"
- if %playsound%==true powershell -c (New-Object Media.SoundPlayer "%chime%").PlaySync();
- goto :eof
- :: SUBROUTINES
- :CountFolder
- set "f=%~1"
- set "f=!f:\comma\=,!"
- set "f=!f:\cbr\=)!"
- set "f=!f:\obr\=(!"
- for /f "tokens=*" %%F in ('dir "!f!" /s /b /a:-d') do ( for %%A in (%supported_formats%) do ( if /i %%~xF==.%%A set /a total=!total!+1))
- goto :eof
- :ProcessFolder
- for /f "tokens=*" %%F in ('dir "%~1" /s /b /a:-d') do (
- for %%A in (%supported_formats%) do (
- if /i %%~xF==.%%A (
- if not "%outfolder%"=="" ( set "foldertree=%%~dpF" & set "foldertree=!foldertree:%~dp1=!")
- CALL :ProcessFile "%%~F"
- )
- )
- )
- set foldertree=
- goto :eof
- :: MAIN ROUTINE
- :ProcessFile
- if "!current!"=="" ( set current=1) else ( set /a current=!current!+1)
- if %total% GTR 1 set "ratio= !current!/!total!"
- title %title1%^:!ratio! "%~nx1"
- if not "%cd%\"=="%~dp1" cd /d "%~dp1"
- :Checkoutfolder
- if not "%outfolder%"=="" (
- set "outpath=%outfolder%!foldertree!"
- ) else (
- if "%subfolder%"=="" ( set "outpath=%~dp1") else ( set "outpath=%~dp1%subfolder%\")
- )
- if not exist "!outpath!" mkdir "!outpath!"
- :Add_Postfix
- if exist "!outpath!%~n1%suffix%.mp4" (
- for /L %%N in (1,1,1000) do (
- if not exist "!outpath!%~n1%suffix%_%%N.mp4" ( set "postfix=_%%N" & goto :FFmpeg)
- )
- )
- :FFmpeg
- "%ffmpeg%" -hide_banner -i "%~1" -map 0:v -map 0:a:? -c:v copy -c:a copy %format_options% "!outpath!%~n1%suffix%!postfix!.%ext%"
- goto :eof
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement