Multiplexing video and audio with FFMPEG from sub-directories using a Windows Batch Script

1

I'm trying to figure out a batch script (windows based) which will allow me to multiplex some .m2v and .wav files that exist in a load of sub-directories (within one single main directory). They need to be restriped and transcoded as well, the output files need to be MXF and end up in another folder. I'm new to batch scripts, not so new to FFMPEG commands. I've not been able to find any batch scripts so far that allow for multiple files to be used, only ones that work to convert a single file.

After much searching the closest thing I've seen is this, which I've tried with no success. Oddly, one time it did produce a .mxf file, albeit 0KB, but every time since I get nothing.

for %%a in (".m2v") do ffmpeg -i "%%~na.wav" -i "%%~na.m2v" -acodec copy -vcodec copy "newfiles\%%~na.mxf". 

I'm not sure what is meant to go in the parenthesis where I have (".m2v").

what does that represent?

DMtd

Posted 2016-07-05T15:52:28.367

Reputation: 21

Please note that [SU] is not a script writing service. If you tell us what you have tried so far (including any scripts you are using) and where you are stuck then we can try to help with specific problems. You should also read How do I ask a good question?.

– DavidPostill – 2016-07-05T19:53:31.800

After much searching the closest thing I've seen is this, which I've tried with no success. Oddly, one time it did produce a .mxf file, albeit 0KB, but every time since I get nothing. for %%a in (".m2v") do ffmpeg -i "%%~na.wav" -i "%%~na.m2v" -acodec copy -vcodec copy "newfiles%%~na.mxf". I'm not sure what is meant to go in the parenthesis where I have (".m2v"). what does that represent? – DMtd – 2016-07-06T08:57:31.593

Answers

1

I'm not sure what is meant to go in the parenthesis where I have (".m2v").

what does that represent?

for %%a in (".m2v") do ffmpeg -i "%%~na.wav" -i "%%~na.m2v" -acodec copy -vcodec copy "newfiles\%%~na.mxf"

The value (".m2v") represents a set of files to process.

In your command there are no files to process since:

  • .m2v is only an extension and we can assume that it should be something like filename.m2v
  • You really need a wildcard expression here (see below for how to do this).
  • Your files are in sub-directories.

Use the following batch file (in the single main directory):

@echo off
setlocal
for /f "usebackq" %%a in (`dir /b /s *.m2v`) do (
  echo ffmpeg -i "%%~dpna.wav" -i "%%~dpna.m2v" -acodec copy -vcodec copy "newfiles\%%~na.mxf
  )
endlocal

Notes:

  • Remove the echo from the ffmpeg command when you are happy with what the batch file is going to do.

  • dir /b /s *.m2v is used to produce a list of files to process.

    The /b option gives the filenames only (we don't want the headers, summary or other details)

    The /s option allows dir to process sub-directories.

  • for /f is used to process the list of files.

  • %%~dpna is used to get the fully qualified filename without the extension.


Further Reading

DavidPostill

Posted 2016-07-05T15:52:28.367

Reputation: 118 938