2

So I created a windows batch script to convert videos using the HandbrakeCLI

Batch script:

@echo off
::SET TO CURRENT CODE PAGE:::::::::::::::::::::::::::
FOR /F "tokens=4 delims= " %%G in ('chcp') DO (
chcp %%G >nul
) 
:::::::::::::::::::::::::::::::::::::::::::::::::::::
setlocal EnableExtensions
title AniCoder v2.5 by Nightsanity
color 0a
cd "%~d0%~p0"

::PREVENT MULTIPLE HANDBRAKE PROCESSES::::::::::::::::
set ignore=INFO:
for /f "usebackq" %%A in (`tasklist /nh /fi "imagename eq HandBrakeCLI.exe"`) do if not %%A==%ignore% (
exit
)
::::::::::::::::::::::::::::::::::::::::::::::::::::::

::PREREQUISITES CHECK:::::::::::::::::::::::::::::::::
if not exist "HandBrakeCLI.exe" (
echo HandBrakeCLI is missing!
pause 
exit
)
if exist "Jobs.txt" del "Jobs.txt"
if not exist "ToConvert" mkdir "ToConvert"
if not exist "Converted" mkdir "Converted"
:::::::::::::::::::::::::::::::::::::::::::::::::::::::

::CREATE A LIST OF VIDEOS TO CONVERT:::::::::::::::::::
for /F %%i in ('dir /s /b "ToConvert\*.*"') do (
dir/s/b "ToConvert\*" >> "Jobs.txt"
goto MAIN
)
goto NOFILES
:::::::::::::::::::::::::::::::::::::::::::::::::::::::

::LOOP THROUGH JOBS LIST AND CONVERT FILES:::::::::::::
:MAIN
for /f "tokens=* delims= " %%a in (Jobs.txt) do (
HandBrakeCLI -i "./ToConvert/%%~nxa" -f mp4 -o "./Converted/%%~na.mp4" -q 22 -e x264 -x cabac=0:ref=2:me=hex:bframes=0:weightp=0:subme=6:8x8dct=0:trellis=0 -E faac --mixdown mono -B 64 -X 480 -l 272 -s 1 --subtitle-burn -a 1
)
:::::::::::::::::::::::::::::::::::::::::::::::::::::::

::CLEAN UP FILES:::::::::::::::::::::::::::::::::::::::
if exist "Jobs.txt" del "Jobs.txt"
:Question
cls
echo Encode Done!
echo.
set DELFILES=
set /p DELFILES="Do you want to delete original files (Y/N)?:"
if "%DELFILES%" == "y" goto DELNOW
if "%DELFILES%" == "Y" goto DELNOW
if "%DELFILES%" == "n" goto ENDNOW
if "%DELFILES%" == "N" goto ENDNOW
goto Question
:DELNOW
if exist "ToConvert\*.*" del /Q "ToConvert\*.*"
:ENDNOW
endlocal
exit
:::::::::::::::::::::::::::::::::::::::::::::::::::::::

::WHEN NO FILES ARE INSIDE TOCONVERT FOLDER::::::::::::
:NOFILES
echo No video files found in:
echo ToConvert folder
echo.
pause
:::::::::::::::::::::::::::::::::::::::::::::::::::::::

HandbrakeCLI reference: trac.handbrake.fr/wiki/CLIGuide

I have ran into a problem where if there is a special character in the path for example a German Umlaut it will not interpret it or something as it wont convert and close the window.

How can I workaround this issue?

Running Windows 7 Home Premium Sp1

  • Use the code page of the region perhaps. You are using forward slashes also which often works in Windows but will fail in different circumstances. – foxidrive Apr 30 '14 at 15:21
  • I have to set to the current code page from the system that runs the bat or do i have to manually add it? –  Apr 30 '14 at 20:27
  • 1
    ISTM that your chcp routine does nothing - it sets the code page to the code page that it was already set to. Try using `chcp NUM` where NUM is the number of the code page for that region. Like `chcp 1252` and see if it helps. – foxidrive May 01 '14 at 08:04

1 Answers1

1

I'd suggest you change

for /F %%i in ('dir /s /b "ToConvert\*.*"') do (
 dir/s/b "ToConvert\*" >> "Jobs.txt"

to

for /F "delims=" %%i in ('dir /s /b "ToConvert\*.*"') do (
 echo(%%~dspnxi >> "Jobs.txt"

to output the shortname to jobs.tst.

or perhaps

 echo(%%~dspi%%~nxi >> "Jobs.txt"

may be better.

(can't test - no test directories available to me)

Magoo
  • 77,302
  • 8
  • 62
  • 84