0

I'm making a batch script where I download videos using ffmpeg.

First it needs to read download codes from a text file (codes.txt)

Then it checks the current directory if the same file exists, if it exists it increments a new filename.

Then it starts to download, however, I wasn't able to make these bunch of codes to work together. (The incrementing method works but without the "read from txt file" code, if I combine the two it results to a blank filename. I think it's because the looping code is not compatible.)

Read (loop) from txt file code:

@echo off
set prefix=title-
set resolution=720p
set extension=mkv    
for /F "tokens=*" %%A in (code.txt) do (
title Downloading "%prefix%.%extension%" @ %resolution%
"%~dp0ffmpeg.exe" -loglevel info -stats -i "https://somesite.com/%%A&quality=%resolution%" -i "https://somesite.com" -headers "Origin: https://somesite.come" -c copy -disposition:s:0 default -bsf:a aac_adtstoasc "%prefix%.%extension%" 
pause

and using the incrementing method I found from link:

@echo off
setlocal EnableDelayedExpansion

SET "ffmpeg=your_path_to\ffmpeg.exe"
SET "output_folder=D:\rendered_files"
SET "extension=mov"
SET "prefix=Scene_"

rem do not set this, just define it empty:
SET "output_path="

call :get_output_path "%output_folder%" %prefix% %extension%

rem here goes your ffmpeg command line where %1 is input file and "%output_path%" is your destination

"%ffmpeg%" -i %1 ..... your filters end such ..... "%output_path%"

echo Done
endlocal & echo press any key to exit ... & pause>nul & goto :eof



:get_output_path <output folder> <prefix> <extension>
SET "i=0"
SET "formated_value=000"
:loop
SET "output_path=%~1\%~2%formated_value%.%3"
if exist "%output_path%" call :increase& goto :loop 
goto :eof

:increase
SET /a "i=%i%+1"
SET "value=000000%i%"
SET "formated_value=%value:~-3%"
goto :eof

Combined batch script:

@echo on
echo.
setlocal EnableDelayedExpansion
set prefix=showabc-
set resolution=720p
set extension=mkv
SET "output_folder=%~dp0Downloads"
for /F "tokens=*" %%A in (code.txt) do (
SET "output_path="
call :get_output_path "%output_folder%" %prefix% %extension%
echo File "%prefix%%formated_value%.%extension%" will be saved at "%output_folder%".
title Downloading "%prefix%.%extension%" @ %resolution%
"%~dp0ffmpeg.exe" -loglevel info -stats -i "https://somesite.com/%%A&quality=%resolution%" -i "https://somesite.com" -headers "Origin: https://somesite.com" -c copy -disposition:s:0 default -bsf:a aac_adtstoasc "%output_path%"
)
pause

:get_output_path <output folder> <prefix> <extension>
SET "i=1"
SET "formated_value=001"
:loop
SET "output_path=%~1\%~2%formated_value%.%3"
if exist "%output_path%" call :increase& goto :loop 
goto :eof
:increase
SET /a "i=%i%+1"
SET "value=000000%i%"
SET "formated_value=%value:~-3%"
goto :eof

This is the result I always get: Screenshot

It's either ".mkv" or filename without incrementing numbers. :((

Sorry for the very long post. I hope someone can help me, thanks y'all! Stay safe!

1
  • You have delayed expansion enabled, but the counter appears to not use it. If I recall correctly, you will want to use ! (the BANG char) instead of % (percent char) See for example: superuser.com/questions/1569594
    – Yorik
    Sep 30 '20 at 20:48
0
@echo off

cd /d "%~dp0" && setlocal enabledelayedexpansion
for /f tokens^=* %%a in ('type .\code.txt"')do call %:^) i "%%~a"

<con: rem./ & %__AppDir__%timeout.exe /t -1 & endlocal & goto=:EOF

%:^)
<con: set /a "_%~1+=1+1000"
if exist "%~dp0downloads\showabc-!_%~1:~-3!.mkv" && goto %:^)

<con: title <nul && title downloading "showabc-!_i:~-3!.mkv" @ 720p  
echo\file "showabc-!_%~1:~-3!.mkv" will be saved at "%~dp0downloads".

"%~dp0ffmpeg.exe" -i "https://somesite.com/%~2&quality=720p" -i "https://somesite.com" ^
     -headers "origin: https://somesite.com" -c copy -disposition:s:0 default -bsf:a ^
     aac_adtstoasc -loglevel info -stats "%~dp0downloads\showabc-!_%~1:~-3!.mkv"

I'm sorry, but I removed your variables to try to understand your code and not make mistakes in applying them.

If I don't understand what you want to do, I hope you will signal me where I lost the north (plz) ...

The code below I will leave here as an attempt to help you, where in your future comments, with bigger and more accurate information, (me or other operators here), we can understand and propose a better solution for your case....

Obs.: 1 We don't know the contents of your code.txt file

Obs.: 2 I'm assuming your commands are correct

Obs.: 3 Goto :EOF goes straight to the End of the File, and so, immediately ending the execution, inside your :label

Obs.: 4 Use exit /b if you don't want to end the execution immediately, thus, your increment should work without premature interruptions ...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.