0

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.

Crankrune
  • 15
  • 4
  • 2
    Possible duplicate of [windows batch command for loop set environment variable](http://stackoverflow.com/questions/17598638/windows-batch-command-for-loop-set-environment-variable) – Jean-François Fabre Dec 05 '16 at 22:54
  • Possible duplicate of [Batch - Assign Command output to Variable](http://stackoverflow.com/questions/16203629/batch-assign-command-output-to-variable); also related: [Batch - Assign Command output with variables to Variable](http://stackoverflow.com/questions/28663803/batch-assign-command-output-with-variables-to-variable) – aschipfl Dec 05 '16 at 23:17

1 Answers1

1
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.

Magoo
  • 77,302
  • 8
  • 62
  • 84
  • Thanks a lot, this worked with a slight modification. I had to add a caret before the equals sign, since it wasn't passing that as part of the command. – Crankrune Dec 06 '16 at 03:09