0

I'm trying to capture the output of ffprobe to assign to two variables - currently I just run the loop twice to captue the encoder and codec_name tag. Is there a more effcient way to do this from within a single loop?

for /F "delims=" %%i in ('ffprobe -v error -show_entries format_tags^=encoder -of default^=nw^=1:nk^=1 %%a 2^>^&1') do set "encoder=%%i"
echo !encoder!

for /F "delims=" %%j in ('ffprobe -v error -show_entries stream^=codec_name -of default^=nw^=1:nk^=1 %%a 2^>^&1') do set "codec_name=%%j"
echo !codec_name!    

Here's the full code for info - it's a batch reverse function in FFMPEG.

@echo off
md "%cd%\_reversedFiles"

for %%a in (*.wav) do (
    SETLOCAL enabledelayedexpansion
    for /F "delims=" %%i in ('ffprobe -v error -show_entries format_tags^=encoder -of default^=nw^=1:nk^=1 %%a 2^>^&1') do set "encoder=%%i"
    echo !encoder!

    for /F "delims=" %%j in ('ffprobe -v error -show_entries stream^=codec_name -of default^=nw^=1:nk^=1 %%a 2^>^&1') do set "codec_name=%%j"
    echo !codec_name!

    ffmpeg -i %%a -af areverse -c:a !codec_name! "\_reversedFiles\%%~na.wav" 
    bwfmetaedit "\_reversedFiles\%%a" -a --ISFT="!encoder!"
)

For ease of sharing it needs to be in a batch file.

1 Answer 1

3

Given that both ffmpeg command lines return exactly one non-empty line each, you could execute them within the same set of a single for /F loop:

    rem // Initialise variable that is going to receive the first line:
    set "ENCODER="
    rem // Capture the output of both `ffmpeg` command lines:
    for /F delims^=^ eol^= %%F in ('
        rem/ // Execute both `ffmpeg` command lines one after another: ^
            ^& ffprobe -v error -show_entries format_tags^=encoder -of default^=nw^=1:nk^=1 "%%~a" 2^>^&1 ^
            ^& ffprobe -v error -show_entries stream^=codec_name -of default^=nw^=1:nk^=1 "%%~a" 2^>^&1 ^
    ') do (
        rem // This captures the first non-empty line of text:
        if not defined ENCODER set "ENCODER=%%F"
        rem // This captures the last non-empty line of text:
        set "CODEC_NAME=%%F"
    )
    echo Encoder: !ENCODER!
    echo Codec:   !CODEC_NAME!
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.