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.

2

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

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.

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.