I have the following batch-file code to convert some video files with HandBrakeCLI:
for /R .\test %%F in (*.mp4,*.avi,*.flv,*.mov,*.3gp,*.wmv,*.mkv,*.ts) do (
HandBrakeCLI -e x264 --x264-preset medium -q 35 --crop 0:0:0:0 --aencoder copy -i "%%~fF" -o "%%~dpF%%~nF_conv.mp4"
if exist "%%~dpF%%~nF_conv.mp4" (
del "%%~fF"
ren "%%~dpF%%~nF_conv.mp4" "%%~nxF"
)
)
To apply this code on videos, I should copy the batch-file and Handbrake.exe (and also its relative folders) and paste them beside the folder named test (in the code above) then change the name "test" in the batch file to the name of that folder, then run the batch-file.
Could you write the batch-file in a way we run it in an arbitrary folder, so it prompt for a folder containing videos and we write the path (or just simply drag and drop the folder to the command line and press enter) without moving the files and renaming the "test"?
Use "%~1"
to read a drag-and-drop argument.
for /R "%~1" %%F in (etc...)
If the batch script is launched without drag and drop (if "%~1"==""
), you could use a folder chooser to let the user browse for a folder.
@if (@CodeSection == @Batch) @then
:: based on fchooser2.bat
:: https://stackoverflow.com/a/15906994/1683264
@echo off
setlocal
if "%~1"=="" (
call :chooser dir || goto usage
) else if exist "%~1" (
set "dir=%~1"
) else goto usage
for /R "%dir%" %%F in (*.mp4,*.avi,*.flv,*.mov,*.3gp,*.wmv,*.mkv,*.ts) do (
HandBrakeCLI -e x264 --x264-preset medium -q 35 --crop 0:0:0:0 --aencoder copy -i "%%~fF" -o "%%~dpF%%~nF_conv.mp4"
if exist "%%~dpF%%~nF_conv.mp4" (
del "%%~fF"
ren "%%~dpF%%~nF_conv.mp4" "%%~nxF"
)
)
:: end main runtime. Pause if dragged-and-dropped or double-clicked.
if /i "%cmdcmdline:~0,6%"=="cmd /c" pause
goto :EOF
:chooser <var_to_set>
setlocal
for /f "delims=" %%I in ('cscript /nologo /e:jscript "%~f0"') do (
if not "%%~I"=="" if exist "%%~I" (
endlocal & set "%~1=%%~I"
exit /b 0
)
)
exit /b 1
:usage
echo Usage: %~nx0 [directory]
goto :EOF
@end
// JScript portion
var shl = new ActiveXObject("Shell.Application");
var folder = shl.BrowseForFolder(0, "Please choose a folder.", 0, 0x00);
WSH.Echo(folder ? folder.self.path : '');