Search code examples
batch-filecmd

How to use the output of a command in another


I'm trying to write a batch script to extract both audio and video from some mp4's in a same folder using ffmpeg, then remerging them in an mkv, but trying to conserve the audio language.

I got a script working perfectly to do it without considering the language:

chcp 65001
for %%a in (*.mp4) do (
    "ffmpeg" -i %%a -c:v copy -bsf hevc_mp4toannexb "%%~na.h265" -c:a copy "%%~na.aac"
    del %%a
    "mkvmerge" -o "%%~na.mkv" "%%~na.h265" "%%~na.aac"
)

That one works smoothly.

I've got another script which outputs the audio language to feed the mkvmerge code and I got this:

chcp 65001
for %%a in (*.mp4) do (
    "mediainfo" --Output=Audio;%%Language/String2%%  "%%a"
)

I get the code (en, ja, es, etc.).

The problem is that I don't know how to set the output of the second script to use it in the first one.

I should store the output from the second one and the mkvmerge command would be something like that:

"mkvmerge" -o "%%~na.mkv" "%%~na.h265" --language TID:(output from the other script) "%%~na.aac"

That way every new mkv keeps the original audio language from the mp4.

I've been searching today for it but I couldn't find the answer to my need.

PS: Don't ask why I need that that way instead of just remuxing directly the mp4 to an mkv, I just need it to be that way.

As an aside, ffmpeg, mediainfo and mkvmerge are double quoted because they are not env variables in my laptop, so I just set the paths to the exe's and there are spaces in them.


Solution

  • The FOR /F command is used to capture the output of a command. This is untested.

    chcp 65001
    for %%a in (*.mp4) do (
        FOR /F "delims=" %%G IN ('^""C:\MediaInfo\MediaInfo.exe" "--Output=Audio;[%%Language/String2%%]" "%%a"^"') DO (
            "mkvmerge" -o "%%~na.mkv" "%%~na.h265" --language TID:%%G "%%~na.aac"
        )
    )