批处理脚本中的错误:`缺少操作数`和不正确的秒计算

问题描述 投票:0回答:1

编辑:获取丢失操作数错误该脚本首先将视频长度转换为秒并将其显示在output.md中的公式,它在大多数情况下都有效,但对于某些文件,它只是不显示正确,就像我有一个文件一样是 17 分 08 秒,它必须是 1028,但它显示 1020

我收到的错误:

Invalid number.  Numeric constants are either decimal (17), hexadecimal (0x11), or octal (021). Missing operand.

我不知道如何解决它。

这是我的批处理脚本的代码:

@echo off
setlocal enabledelayedexpansion

set "output_file=output.txt"

rem Delete the output file if it already exists
if exist "%output_file%" del "%output_file%"

for %%i in (*.mp4 *.avi *.mkv *.mov *.wmv *.flv *.mpeg *.mpg) do (
    set "input_file=%%i"

    for /f "tokens=*" %%a in ('ffmpeg -i "!input_file!" 2^>^&1 ^| find "Duration"') do (
        set "duration=%%a"
        set "duration=!duration:*Duration=!"
        set "duration=!duration:~0,12!"
    )

    set "duration=!duration:.=,:!"
    set "duration=!duration:~1,-1!"

    for /f "tokens=1-4 delims=,:." %%a in ("!duration!") do (
        set /a "hours=%%a, minutes=%%b, seconds=%%c, milliseconds=%%d"
        set /a "total_seconds=hours*3600 + minutes*60 + seconds"
        set "hhmmss=%%a:%%b:%%c"
    )

    echo Filename: "!input_file!" >> "%output_file%"
    echo Duration: !hhmmss! >> "%output_file%"
    echo Total Seconds: !total_seconds! >> "%output_file%"
    echo. >> "%output_file%"
)

echo Output has been written to "%output_file%"

endlocal
if-statement batch-file error-handling
1个回答
0
投票

您的第一个错误是使用 ffmpeg 从文件中获取持续时间。我建议您改用 ffprobe。

您的主要问题有据可查,事实上您正在尝试使用可以识别为八进制(base8)而不是预期的十进制(base10)的数字执行

set /a
算术。

这是我如何尝试这项任务的想法:

@Echo Off
SetLocal EnableExtensions DisableDelayedExpansion

Set "output_file=output.txt"

(
    For %%G In (*.mp4 *.avi *.mkv *.mov *.wmv *.flv *.mpeg *.mpg) Do (
        For /F "Tokens=1-4 Delims=:." %%H In ('P:\ath\To\ffprobe.exe
         -i "%%G" -show_entries format^=duration -v quiet -of csv^="p=0" 2^>NUL'
        ) Do (
            Set /A ss=%%~nH,hh=ss/3600+100,ss%%=3600,mm=ss/60+100,ss=ss%%60+100
            Echo Filename: "%%G"
            SetLocal EnableDelayedExpansion
            Echo Duration: !hh:~1!:!mm:~1!:!ss:~1!
            EndLocal
            Echo Total Seconds: %%~nH
            Echo(
        )
    )
) 1>"%output_file%"

Echo Output has been written to "%output_file%"

EndLocal
© www.soinside.com 2019 - 2024. All rights reserved.