How to modify a batch file to support drag and drop for processing a video file?

Viewed 50

I am wanting to modify this batch file to my needs. This is a file which allows me to use a video encoder and encode videos. I have to do following to get it working:

  1. Put the batch file called proxy generator.bat in the folder where my video is located.

  2. Put the encoder software called NVEncC64.exe which is in a folder called nvencc in the same folder as the video and the batch file.

  3. I can now run the batch file and it will do the job.

What I want to achieve if possible is to have the batch file and the nvencc folder saved to another location like D:\Encoder and be able to drag and drop a video from another drive (external HDD) on the batch file and get the job done.

I will paste the batch file here.

@echo off
set t0=%TIME%, %DATE%

set "ConversionDir=%~dp0"
set "ProxyDir=C:\Users\Mypc\Desktop\"


for %%i in (*.mp4, *.avi, *.mov, *.wmv, *.ts, *.m2ts, *.mkv) do (
  if /i not "%%~xi"==".avi" SET "nvcm=--colorprim auto --transfer auto --colormatrix auto --colorrange auto"
  if not exist "%ProxyDir%\%%~ni_proxy.mp4" nvencc\NVEncC64.exe -i "%ConversionDir%\%%i" -o "%ProxyDir%\%%~ni_proxy.mp4" --output-res 960x-2 -c h264 --preset quality --cqp 23:25:28 --aq-strength 10 --ref 1 --bframes 0 --gop-len 15 --lookahead 15 --qp-max 30 --aq --cabac --mv-precision q-pel --audio-codec aac --audio-bitrate 384 --avsync cfr %nvcm%
 )


powershell write-host -fore cyan  ====================== Processing is FINISHED =======================
echo ----------------------------
echo Batch processing start time: %t0%
echo Batch processing end time:   %TIME%, %DATE%
echo ----------------------------
pause

BTW: ProxyDir in this batch file is the folder in which the encoded file will be saved. To be more precise I am making proxy files (low resolution, small files) to be able to edit them easily in Adobe Premiere as the original files are too large.

1 Answers

The batch file should be modified to following to support running it

  • without any argument to process all video files in the current directory;
  • with a file name of a video file as first argument to process this video file;
  • with a folder name as first argument to process all video files in this folder.
@echo off
setlocal EnableExtensions DisableDelayedExpansion
set "t0=%TIME%, %DATE%"
set "RestoreFolder="
set "ProxyDir=%USERPROFILE%\Desktop"

rem Is the batch file started without any argument (or with first argument
rem being an empty string)? Yes, process all video files in current directory.
if "%~1" == "" goto AllFiles

rem Is the batch file started not with a folder name as first argument?
rem Yes, the first argument is most likely a file name and so just a
rem single file should be processed by the batch file.
if not exist "%~1\" goto SingleFile

rem The batch file is started with a folder name as first argument.
rem Make this folder the current directory using command PUSHD and
rem if that is successfull process all video files in that folder.
pushd "%~1" 2>nul
if not errorlevel 1 set "RestoreFolder=1" & goto AllFiles

echo ERROR: Failed to make "%~1" the current directory!
echo/
pause
exit /B 1

:AllFiles
for %%i in (*.mp4 *.avi *.mov *.wmv *.ts *.m2ts *.mkv) do if not exist "%ProxyDir%\%%~ni_proxy.mp4" (
    if /I not "%%~xi" == ".avi" (
        "%~dp0nvencc\NVEncC64.exe" -i "%%i" -o "%ProxyDir%\%%~ni_proxy.mp4" --output-res 960x-2 -c h264 --preset quality --cqp 23:25:28 --aq-strength 10 --ref 1 --bframes 0 --gop-len 15 --lookahead 15 --qp-max 30 --aq --cabac --mv-precision q-pel --audio-codec aac --audio-bitrate 384 --avsync cfr --colorprim auto --transfer auto --colormatrix auto --colorrange auto
    ) else (
        "%~dp0nvencc\NVEncC64.exe" -i "%%i" -o "%ProxyDir%\%%~ni_proxy.mp4" --output-res 960x-2 -c h264 --preset quality --cqp 23:25:28 --aq-strength 10 --ref 1 --bframes 0 --gop-len 15 --lookahead 15 --qp-max 30 --aq --cabac --mv-precision q-pel --audio-codec aac --audio-bitrate 384 --avsync cfr
    )
)
if defined Restorefolder popd
goto OutputInfo

:SingleFile
if exist "%~1" goto ProcessFile
echo ERROR: Could not find "%~1"!
echo/
pause
exit /B 1

:ProcessFile
if not exist "%ProxyDir%\%~n1_proxy.mp4" (
    if /I not "%~x1" == ".avi" (
        "%~dp0nvencc\NVEncC64.exe" -i "%~1" -o "%ProxyDir%\%~n1_proxy.mp4" --output-res 960x-2 -c h264 --preset quality --cqp 23:25:28 --aq-strength 10 --ref 1 --bframes 0 --gop-len 15 --lookahead 15 --qp-max 30 --aq --cabac --mv-precision q-pel --audio-codec aac --audio-bitrate 384 --avsync cfr --colorprim auto --transfer auto --colormatrix auto --colorrange auto
    ) else (
        "%~dp0nvencc\NVEncC64.exe" -i "%~1" -o "%ProxyDir%\%~n1_proxy.mp4" --output-res 960x-2 -c h264 --preset quality --cqp 23:25:28 --aq-strength 10 --ref 1 --bframes 0 --gop-len 15 --lookahead 15 --qp-max 30 --aq --cabac --mv-precision q-pel --audio-codec aac --audio-bitrate 384 --avsync cfr
    )
)

:OutputInfo
%SystemRoot%\System32\WindowsPowerShell\v1.0\powershell.exe write-host -fore cyan  ====================== Processing is FINISHED =======================
echo ----------------------------
echo Batch processing start time: %t0%
echo Batch processing end time:   %TIME%, %DATE%
echo ----------------------------
pause
endlocal

Next browse in Windows File Explorer to the batch file (in D:\Encoder), right click the file and click in context submenu Send to on the menu item Desktop (create shortcut).

Switch to the desktop which has now a shortcut file with name of the batch file with file extension .lnk whereby the file extension is not displayed. Right click on this shortcut file and use the menu item Rename to change the name of the shortcut file. The name should be something meaningful for you.

Then right click once again on the shortcut file and click on last context menu item Properties. Modify the property Target and insert left to the batch file name %SystemRoot%\System32\cmd.exe /D /C separated with an additional space from the batch file name.

The property Comment should be also modified to give a useful information what the shortcut respectively the batch file is used for. There can be also other properties modified on the tabs Font, Layout or Colors.

Finally click on button OK to save the modified properties of the shortcut file.

Cut the shortcut file from desktop by pressing Ctrl+X, switch back to Windows File Explorer, browse to the folder %APPDATA%\Microsoft\Windows\SendTo and paste the shortcut file into this directory by pressing Ctrl+V.

Now it is possible to right click on any folder containing video files or on any video file in Windows File Explorer to open the context menu and click in submenu Send to on the menu item with name of the shortcut file to process all video files in the right clicked folder or just the right clicked video file.

Note: The environment variable ProxyDir is perhaps defined better with %USERPROFILE%\Videos.

To understand the commands used and how they work, open a command prompt window, execute there the following commands, and read the displayed help pages for each command, entirely and carefully.

  • call /? ... explains referencing batch file arguments as used by the batch file.
  • echo /?
  • endlocal /?
  • exit /?
  • for /?
  • goto /?
  • if /?
  • pause /?
  • popd /?
  • pushd /?
  • rem /?
  • set /?
  • setlocal /?
Related