Search code examples
videocommand-linecmdffmpeg

FFMPEG - Store FFPROBE's duration output as a variable


I wanted to use a video's duration in the "enable='between(t,0,0)" field to make sure that I overlay an image at 1/3 of the video's duration every time.

According to FFMPEG's info on their site, I need to use this

ffprobe -v error -show_entries format=duration -of default=noprint_wrappers=1:nokey=1 input.mp4

it does return a value but I can't seem to be able to set it to a usable variable, I already tried by using

set duration=ffprobe -v error -show_entries format=duration -of default=noprint_wrappers=1:nokey=1 input.mp4

And then referencing it via %duration%, but no luck.

Is there a way to do this, am I doing something wrong?

Thank you for your help.


Solution

  • The command designed for processing output of a console application is FOR.

    A batch file code like below can be used to capture output of ffprobe and assign it to an environment variable.

    @echo off
    setlocal EnableExtensions DisableDelayedExpansion
    if not exist input.mp4 goto :EOF
    for /F "delims=" %%I in ('ffprobe.exe -v error -show_entries format^=duration -of default^=noprint_wrappers^=1:nokey^=1 input.mp4 2^>^&1') do set "duration=%%I"
    echo Duration is: %duration%
    endlocal
    

    The command FOR with option /F interprets the string enclosed in ' as command to execute of which output written to STDOUT should be processed line by line. FOR respectively cmd.exe processing the batch file starts for this purpose one more command process in background with %ComSpec% /c and the command line between ' appended as additional arguments and captures everything written to handle STDOUT of the additionally started background command process.

    Windows command processor cmd.exe processing the batch file interprets among space also comma ,, semicolon ;, equal sign = and OEM encoded no-break space (with decimal code value 255) as argument separator. For that reason it is necessary to escape each = with ^ in the command line to pass to cmd.exe started in background to get the equal signs interpreted as literal characters by cmd.exe processing the batch file instead of argument separators.

    2>&1 redirects output written to STDERR (standard error) to handle STDOUT (standard output) which is processed by FOR. It is necessary here to escape the operators > and & with ^ to apply this redirection on execution of ffprobe and not interpreting the redirection already on parsing FOR command line. See the Microsoft article Using command redirection operators. This redirection of STDERR to STDOUT is perhaps not necessary if ffprobe outputs on correct usage the requested information to STDOUT. But it could be important if ffprobe outputs the data to STDERR.

    So executed in background is following with Windows installed into C:\Windows:

    C:\Windows\System32\cmd.exe /c ffprobe.exe -v error -show_entries format=duration -of default=noprint_wrappers=1:nokey=1 input.mp4 2>&1
    

    That is an absolutely valid execution of Windows command processor according to syntax as explained by the help output on running cmd /? in a Windows command prompt window.

    The option delims= in double quotes after /F disables splitting up the captured line into multiple tokens (strings) using space and horizontal tab as string separators as it defines an empty list of string delimiters. "delims=" is most likely not necessary for output of ffprobe with the used command line. But I don't have ffprobe installed and it was not posted in question what it outputs.

    To understand the commands used and how they work, open a command prompt window, execute there the following commands, and read the displayed help pages for each command, entirely and carefully.

    • echo /?
    • endlocal /?
    • for /?
    • goto /?
    • if /?
    • set /?
    • setlocal /?