Crankrune
Crankrune

Reputation: 15

How to set command output as variable in batch

First, I've googled and googled how to fix this problem, with no luck. I'm working on making a batch file to re-encode audio in a file to aac if it's ac-3 or ogg using ffmpeg. I know how to do the conversion with ffmpeg, just not how to automate it only if the audio matches a certain codec. To get the audio codec for the file, I have two different methods, one with ffprobe, and the other with MediaInfo.

MediaInfo --Inform="Audio;%%Format%%" "input.mkv"

or

ffprobe -v error -select_streams a:0 -show_entries stream=codec_name -of default=noprint_wrappers=1:nokey=1 "input.mkv"

However, my issue is with setting a variable from the command output. This is the code I've been trying, to no avail.

SETLOCAL

for %%a in (*.mp4) do (

MediaInfo --Inform="Audio;%%Format%%" "%%a">codec.txt

SET /p codec=<codec.txt

DEL codec.txt

ECHO %codec%

)

pause

Obviously, this is only a section of my goal, but this is the part I can't seem to get past. I have checked, by removing the "DEL codec.txt" part, that is in fact storing the correct output in the text file, but it's not importing it into the variable. Anyway I can set the output to a variable is fine, this is just the way I often found in my search.

Any help is much appreciated.

Upvotes: 0

Views: 1424

Answers (1)

Magoo
Magoo

Reputation: 79982

for %%a in (*.mp4) do (
 for /f "delims=" %%c in (
  'MediaInfo --Inform="Audio;%%Format%%" "%%a" ') do (

  echo codec is %%c
  rem now use "%%c" as codec
 )
 rem "%%c" is now out-of-scope
)

You'd need to search SO for delayed expansion for an explanation of why codec remained unchanged in your code.

Upvotes: 1

Related Questions