Home > Software engineering >  I need a way to calculate time differentials between 2 time variables in Batch
I need a way to calculate time differentials between 2 time variables in Batch

Time:07-09

first of all here's my little batch script project that I wrote really quickly for video processing convenience with ffmpeg

set start="00:01:52.000"
set finish="00:01:52.000"
set /A duration=24
set /A resolution=1080
set /A framerate=60
set /A filesizeinMB=8
set /A bitrate=(%filesizeinMB%*8192)/%duration%

"%~dp0ffmpeg.exe" -ss %start% -i "%~1" -c:v libvpx-vp9 -an -pass 1 ^
-t %duration% -b:v %bitrate%k -crf 12 -vf fps=%framerate%,scale=-1:%resolution% ^
-auto-alt-ref 1 -lag-in-frames 25 -row-mt 1 -map_metadata -1 ^
-f null NUL

"%~dp0ffmpeg.exe" -ss %start% -i "%~1" -c:v libvpx-vp9 -an -pass 2 ^
-t %duration% -b:v %bitrate%k -crf 12 -vf fps=%framerate%,scale=-1:%resolution% ^
-auto-alt-ref 1 -lag-in-frames 25 -row-mt 1 -map_metadata -1 ^
-f webm "%~dp0%~n1.webm"

As of now, I'm manually imputing start, and duration for the script to work. For convenience, I would like a way to only input start and finish variables without bothering with duration, and having it something like

set /A duration=%finish%-%start%

but before doing that I need to convert the time strings into simple integers or fractionals (seconds of course) And i'm kind of in a loss about how I would approach this Some help would be greatly appreciated, thank you

CodePudding user response:

@ECHO OFF
SETLOCAL
set "start=00:01:52.681"
set "finish=00:02:33.167"

FOR /f "tokens=1-4delims=:." %%g IN ("%start%") DO SET /a s_hr=1%%g,s_mn=1%%h,s_ss=1%%i,s_ms=1%%j
FOR /f "tokens=1-4delims=:." %%g IN ("%finish%") DO SET /a f_hr=1%%g,f_mn=1%%h,f_ss=1%%i,f_ms=1%%j

SET st
SET fi

SET /a durationms=(f_hr-s_hr)*3600000   (f_mn-s_mn)*60000   (f_ss-s_ss)*1000   f_ms - s_ms
SET    "durms=%durationms:~-3%"
SET /a durs=100 (durationms/1000) %% 60
SET    "durs=%durs:~-2%"
SET /a durm=100 (durationms/60000) %% 60
SET    "durm=%durm:~-2%"
SET /a durh=100 (durationms/3600000)
SET    "durh=%durh:~-2%"
SET "duration=%durh%:%durm%:%durs%.%durms%"
SET du

Tips : Use set "var=value" for setting string values - this avoids problems caused by trailing spaces. Don't assign a terminal backslash, Space or " - build pathnames from the elements - counterintuitively, it is likely to make the process easier.

I'm assuming that both start and finish would be supplied to this procedure in the hh:mm:ss:mmm format you have indicated.

The jiggery-pokery of adding a leading 1 or adding 100 is to overcome the odd characteristic of batch that a leading 0 indicates that the number is octal so 01:09:54.123 would yield an error on the 09.

CodePudding user response:

Thank you, @Magoo here's the final script, if anyone is curious

SETLOCAL
set "start=00:04:32.000"
set "finish=00:04:52.000"
set /A filesizeinMB=8
set /A resolution=576
set /A framerate=60


FOR /f "tokens=1-4delims=:." %%g IN ("%start%") DO SET /a s_hr=1%%g,s_mn=1%%h,s_ss=1%%i,s_ms=1%%j
FOR /f "tokens=1-4delims=:." %%g IN ("%finish%") DO SET /a f_hr=1%%g,f_mn=1%%h,f_ss=1%%i,f_ms=1%%j

SET st
SET fi

SET /a durationms=(f_hr-s_hr)*3600000   (f_mn-s_mn)*60000   (f_ss-s_ss)*1000   f_ms - s_ms
SET    "durms=%durationms:~-3%"
SET /a durs=100 (durationms/1000) %% 60
SET    "durs=%durs:~-2%"
SET /a durm=100 (durationms/60000) %% 60
SET    "durm=%durm:~-2%"
SET /a durh=100 (durationms/3600000)
SET    "durh=%durh:~-2%"
SET "duration=%durh%:%durm%:%durs%.%durms%"
SET du
SET /a durationinseconds=(%durh%*3600) (%durm%*60) %durs%.%durms%
set /A bitrate=(%filesizeinMB%*8192)/%durationinseconds%



"%~dp0ffmpeg.exe" -ss %start% -i "%~1" -c:v libvpx-vp9 -an -pass 1 ^
-t %duration% -b:v %bitrate%k -crf 12 -vf fps=%framerate%,scale=-1:%resolution% ^
-auto-alt-ref 1 -lag-in-frames 25 -row-mt 1 -map_metadata -1 ^
-f null NUL

"%~dp0ffmpeg.exe" -ss %start% -i "%~1" -c:v libvpx-vp9 -an -pass 2 ^
-t %duration% -b:v %bitrate%k -crf 12 -vf fps=%framerate%,scale=-1:%resolution% ^
-auto-alt-ref 1 -lag-in-frames 25 -row-mt 1 -map_metadata -1 ^
-f webm "%~dp0%~n1.webm"

I added a duration in seconds variable, so it could be used in the bitrate formula.

  • Related