@echo off
rem ffmpeg -i test.ts 2>&1 | find "Duration" > duration.txt
rem set /p duration=<duration.txt
set duration= Duration: 01:09:52.59, start: 0.136811, bitrate: 10740 kb/s
set /a Hours=%duration:~12,2%
set /a Mins=%duration:~15,2%
set /a Secs=%duration:~18,2%
set /a Duration_In_Seconds=(hours*3600)+(mins*60)+secs
echo %duration_In_seconds%
It is failing when I try to set “09” as a integer variable called mins.
Invalid number. Numeric constants are either decimal (17), hexadecimal (0x11), or octal (021).
I need to convert the strings to integers so I can use some basic math to work out duration in seconds
read the SET help (type
HELP SETfrom a command prompt), and you will see that SET always interprets zero prefixed numbers as octal. There is no setting that will force base 10. Instead you must manipulate the string such that it does not start with zero.There is no need to write to a temporary file. You can use FOR /F to parse the output, and it can also break the output into hour, minute, and second tokens.
I know of 2 simple methods to circumvent the leading 0.
One method is to prefix the value with a 1 and then use the mod (remainder) operator to extract the desired number. In your case, the hours will be restricted to a max value of 99. If you need to support up to 999 then you could prefix with 10 and take mod of 1000. Note that it is possible to perform multiple assignments with a single SET /A statement using the
,to delimit each assignment. SET /A obeys the correct order of precedence, so parentheses are not needed.The other method is to use a FOR /F loop to strip off the leading zeros. I append a 1 and then divide by 10 so that a string of only zeros still gets processed.
If you want, it is easy to extend the code to round the seconds up if the fractional second is greater than or equal to 0.5. Simply add the
,as a delimiter to parse the extra token, and then a few math tricks to do the rounding.