使用.BAT脚本查找文件匹配和变量分配

时间:2019-10-02 10:25:45

标签: windows batch-file ffmpeg

我试图将文件分配给中的变量,然后还将另外2个文件分配给注释变量。

但是,我遇到了一个问题。

从研究中,我发现了如何进行分配,但是没有人知道我如何进行以下工作。

从文件夹或文本文件中(都可以)找到.m2v视频文件并将其分配给Var1,然后在.wav中找到匹配的音频并将其放入{{ 1}},第三个也是带有数学名称的音频Var2,并将其分配给.wav

我遇到的问题是试图找到与视频匹配的Var3音频文件。

视频文件命名为:

2

音频1是:

PAV_PRG_13683Highc450277201906251802090353.m2v

音频2是:

PAV_PRG_13683High01c450211201906251802090376.wav

文件名匹配,直到看到单词PAV_PRG_13683High00c450211201906251802090368.wav 。不需要High之后的所有内容(它只是一个随机字符串),因此尝试匹配是一个问题。

是否可以通过比较High之前的所有内容来找到匹配项。

正如我将使用变量并将其放入进行合并一样,有没有办法做到这一点,以便在High命令完成后,它移至下一个匹配的文件,并将它们分配给变量。

文件存储在ffmpeg文件夹中,一个文件夹包含所有2个视频文件,另一个文件夹包含所有*.m2v*.wav的所有音频文件。每个视频都有准确的2音频(左右)。

在这个问题上是否有任何帮助,我已经在研究中空洞了,并且在过去的30个小时里一直在进行检查。

2 个答案:

答案 0 :(得分:2)

逻辑(“伪代码”):

for each .m2v file
  create video-variable
  split by `High` and get first part
    look for corresponding .wav files
      set right- and left-variables

代码:

@echo off
setlocal enabledelayedexpansion

for %%a in (*.m2v) do (
  set "right="
  set "left="
  set "video=%%a"
  for /f "delims=|" %%b in ("!video:High=|!") do (
    for %%c in ("%%b*.wav") do (
      if not defined right (set "right=%%c") else (set "left=%%c")
    )
  )
  echo your ffmpeg command with  !video! , !right! , !left!
)

答案 1 :(得分:1)

我确定了哪些文件是左右音频通道。

“高”后带有00的文件为左音频。  “高”后带有01的文件是正确的音频。

请编辑以下内容以获取正确的路径,然后根据自己的喜好编辑FFMPeg命令。

我对FFMPeg不熟悉,只是花了几个小时试图弄清楚如何合并视频和音频,所以可能不正确。

编辑:

@(SetLocal EnableDelayedExpansion
  ECHO OFF
  SET "_Src_Path_M2v=C:\Users\Username\Documents\VideoFiles"
  SET "_Src_Path_Wav=C:\Users\Username\Documents\AudioFiles"
  SET "_Dst_Path=C:\Users\Username\Documents\MergedFiles"
  SET "_Dst_Ext=.Mxf"
  SET "_CMD_FFMpeg=C:\Path\to\FFMPEG\ffmpeg.exe"
  SET "_CMD_FFMpeg_Options= -map 0:0 -vcodec mpeg2video -s 1920x1080 -b:v 50000k -maxrate 50000k -bufsize 3835k -minrate 50000k -r 25 -flags ilme -top 1 -acodec pcm_s24le -ar 48000 -pix_fmt yuv422p -map 1:0 -c:a pcm_s24le -ar 48000 -map 2:0 -c:a pcm_s24le -ar 48000 "
  REM -i video.m2v -i right.wav -i left.wav -map 0:0 -vcodec mpeg2video -s 1920x1080 -b:v 50000k -maxrate 50000k -bufsize 3835k -minrate 50000k -r 25 -flags ilme -top 1 -acodec pcm_s24le -ar 48000 -pix_fmt yuv422p -map 1:0 -c:a pcm_s24le -ar 48000 -map 2:0 -c:a pcm_s24le -ar 48000 !video!.mxf 
)

CALL :Main

( ENDLOCAL
  EXIT /B
)

:Main
  IF NOT EXIST "%_Dst_Path%\" MD "%_Dst_Path%\"
  REM Loop the M2v Folder Matching the Number after PAV_PRG_
  FOR %%_ IN (
    "%_Src_Path_M2v%\*.m2v"
  ) DO (
    ECHO.
    ECHO.===================================
    FOR /F "Tokens=1 Delims=c" %%A IN (
      "%%~n_"
    ) DO (
      SET "_Temp_Output=%%A"
      ECHO.Unique Sequence = "%%A", so Audio must start with "%%A"
      REM Find Left Audio (File 1)
      FOR %%a IN ("%_Src_Path_Wav%\%%A00*.wav"
        ) DO ( SET "_Temp_Audio_00=%%~fa" )
     REM Find Right Audio (File 2)
      FOR %%a IN ("%_Src_Path_Wav%\%%A01*.wav"
        ) DO ( SET "_Temp_Audio_01=%%~fa" )
    )
    REM We now have all of the values we need.
    REM "%%_" holds the path of the M2v file.
    REM _Temp_Audio_ "00" and "01" hold the full paths to the audio files.
    REM Now we Output what we have found and run FFMPEG
    ECHO.
    ECHO.* *** M2v File: - %%~f_
    ECHO. *  Left Audio: - !_Temp_Audio_00!
    ECHO. * Right Audio: - !_Temp_Audio_01!

    REM Set the command to a temp variable so we can echo it then execute it.
    REM                                 -i video.m2v   -i right.wav            -i left.wav           -map 0:0 -vcodec mpeg2video -s 1920x1080 -b:v 50000k -maxrate 50000k -bufsize 3835k -minrate 50000k -r 25 -flags ilme -top 1 -acodec pcm_s24le -ar 48000 -pix_fmt yuv422p -map 1:0 -c:a pcm_s24le -ar 48000 -map 2:0 -c:a pcm_s24le -ar 48000 !video!.mxf 
    SET "_Temp_Full_Cmd="%_CMD_FFMpeg%" -i "%%~f_"     -i "!_Temp_Audio_01!"   -i "!_Temp_Audio_00!" %_CMD_FFMpeg_Options% "%_Dst_Path%\!_Temp_Output!_Result%_Dst_Ext%""

    ECHO. *  Running: !_Temp_Full_Cmd!
    !_Temp_Full_Cmd!

  )
GOTO :EOF

上一个

@(SetLocal EnableDelayedExpansion
  ECHO OFF
  SET "_Src_Path_M2v=C:\Users\Username\Documents\VideoFiles"
  SET "_Src_Path_Wav=C:\Users\Username\Documents\AudioFiles"
  SET "_Dst_Path=C:\Users\Username\Documents\MergedFiles"
  SET "_Dst_Ext=.Mxf"
  SET "_CMD_FFMpeg=C:\Path\to\FFMPEG\ffmpeg.exe"
  SET "_CMD_FFMpeg_Options= -map 0:0 -vcodec mpeg2video -s 1920x1080 -b:v 50000k -maxrate 50000k -bufsize 3835k -minrate 50000k -r 25 -flags ilme -top 1 -acodec pcm_s24le -ar 48000 -pix_fmt yuv422p -map 1:0 -c:a pcm_s24le -ar 48000 -map 2:0 -c:a pcm_s24le -ar 48000 "
  REM -i video.m2v -i right.wav -i left.wav -map 0:0 -vcodec mpeg2video -s 1920x1080 -b:v 50000k -maxrate 50000k -bufsize 3835k -minrate 50000k -r 25 -flags ilme -top 1 -acodec pcm_s24le -ar 48000 -pix_fmt yuv422p -map 1:0 -c:a pcm_s24le -ar 48000 -map 2:0 -c:a pcm_s24le -ar 48000 !video!.mxf 
)

CALL :Main

( ENDLOCAL
  EXIT /B
)

:Main
  IF NOT EXIST "%_Dst_Path%\" MD "%_Dst_Path%\"
  REM Loop the M2v Folder Matching the Number after PAV_PRG_
  FOR %%_ IN (
    "%_Src_Path_M2v%\*.m2v"
  ) DO (
    ECHO.
    ECHO.===================================
    FOR /F "Tokens=1-4 Delims=_Hc" %%A IN (
      "%%~n_"
    ) DO (
      SET "_Temp_Output=%%A_%%B_%%CH%%D"
      ECHO.Unique Sequence = "%%C", so Audio must start with "%%A_%%B_%%CH%%D"
      REM Find Left Audio (File 1)
      FOR %%a IN ("%_Src_Path_Wav%\%%A_%%B_%%CH%%D00*.wav"
        ) DO ( SET "_Temp_Audio_00=%%~fa" )
     REM Find Right Audio (File 2)
      FOR %%a IN ("%_Src_Path_Wav%\%%A_%%B_%%CH%%D01*.wav"
        ) DO ( SET "_Temp_Audio_01=%%~fa" )
    )
    REM We now have all of the values we need.
    REM "%%_" holds the path of the M2v file.
    REM _Temp_Audio_ "00" and "01" hold the full paths to the audio files.
    REM Now we Output what we have found and run FFMPEG
    ECHO.
    ECHO.* *** M2v File: - %%~f_
    ECHO. *  Left Audio: - !_Temp_Audio_00!
    ECHO. * Right Audio: - !_Temp_Audio_01!

    REM Set the command to a temp variable so we can echo it then execute it.
    REM                                 -i video.m2v   -i right.wav            -i left.wav           -map 0:0 -vcodec mpeg2video -s 1920x1080 -b:v 50000k -maxrate 50000k -bufsize 3835k -minrate 50000k -r 25 -flags ilme -top 1 -acodec pcm_s24le -ar 48000 -pix_fmt yuv422p -map 1:0 -c:a pcm_s24le -ar 48000 -map 2:0 -c:a pcm_s24le -ar 48000 !video!.mxf 
    SET "_Temp_Full_Cmd="%_CMD_FFMpeg%" -i "%%~f_"     -i "!_Temp_Audio_01!"   -i "!_Temp_Audio_00!" %_CMD_FFMpeg_Options% "%_Dst_Path%\!_Temp_Output!_Result%_Dst_Ext%""

    ECHO. *  Running: !_Temp_Full_Cmd!
    !_Temp_Full_Cmd!

  )
GOTO :EOF

示例输出:

C:\Admin>C:\Admin\SO_FFMPEG_v3.1.cmd

===================================
Unique Sequence = "13683", so Audio must start with "PAV_PRG_13683High"

* *** M2v File: - C:\Users\Username\Documents\VideoFiles\PAV_PRG_13683Highc450277201906251802090353.m2v
 *  Left Audio: - C:\Users\Username\Documents\AudioFiles\PAV_PRG_13683High00c450277201906251802090777.wav
 * Right Audio: - C:\Users\Username\Documents\AudioFiles\PAV_PRG_13683High01c450277201906251802090368.wav
 *  Running: "C:\Path\to\FFMPEG\ffmpeg.exe" -i "C:\Users\Username\Documents\VideoFiles\PAV_PRG_13683Highc450277201906251802090353.m2v"     -i "C:\Users\Username\Documents\AudioFiles\PAV_PRG_13683High01c450277201906251802090368.wav"   -i "C:\Users\Username\Documents\AudioFiles\PAV_PRG_13683High00c450277201906251802090777.wav"  -map 0:0 -vcodec mpeg2video -s 1920x1080 -b:v 50000k -maxrate 50000k -bufsize 3835k -minrate 50000k -r 25 -flags ilme -top 1 -acodec pcm_s24le -ar 48000 -pix_fmt yuv422p -map 1:0 -c:a pcm_s24le -ar 48000 -map 2:0 -c:a pcm_s24le -ar 48000  "C:\Users\Username\Documents\MergedFiles\PAV_PRG_13683High_Result.Mxf"

===================================
Unique Sequence = "13686", so Audio must start with "PAV_PRG_13686High"

* *** M2v File: - C:\Users\Username\Documents\VideoFiles\PAV_PRG_13686Highc450277201906251803430188.m2v
 *  Left Audio: - C:\Users\Username\Documents\AudioFiles\PAV_PRG_13683High00c450277201906251802090777.wav
 * Right Audio: - C:\Users\Username\Documents\AudioFiles\PAV_PRG_13683High01c450277201906251802090368.wav
 *  Running: "C:\Path\to\FFMPEG\ffmpeg.exe" -i "C:\Users\Username\Documents\VideoFiles\PAV_PRG_13686Highc450277201906251803430188.m2v"     -i "C:\Users\Username\Documents\AudioFiles\PAV_PRG_13683High01c450277201906251802090368.wav"   -i "C:\Users\Username\Documents\AudioFiles\PAV_PRG_13683High00c450277201906251802090777.wav"  -map 0:0 -vcodec mpeg2video -s 1920x1080 -b:v 50000k -maxrate 50000k -bufsize 3835k -minrate 50000k -r 25 -flags ilme -top 1 -acodec pcm_s24le -ar 48000 -pix_fmt yuv422p -map 1:0 -c:a pcm_s24le -ar 48000 -map 2:0 -c:a pcm_s24le -ar 48000  "C:\Users\Username\Documents\MergedFiles\PAV_PRG_13686High_Result.Mxf"

===================================
Unique Sequence = "13717", so Audio must start with "PAV_PRG_13717High"

* *** M2v File: - C:\Users\Username\Documents\VideoFiles\PAV_PRG_13717Highc450277201906251813110160.m2v
 *  Left Audio: - C:\Users\Username\Documents\AudioFiles\PAV_PRG_13717High00c450277201906251813110333.wav
 * Right Audio: - C:\Users\Username\Documents\AudioFiles\PAV_PRG_13717High01c450277201906251813110194.wav
 *  Running: "C:\Path\to\FFMPEG\ffmpeg.exe" -i "C:\Users\Username\Documents\VideoFiles\PAV_PRG_13717Highc450277201906251813110160.m2v"     -i "C:\Users\Username\Documents\AudioFiles\PAV_PRG_13717High01c450277201906251813110194.wav"   -i "C:\Users\Username\Documents\AudioFiles\PAV_PRG_13717High00c450277201906251813110333.wav"  -map 0:0 -vcodec mpeg2video -s 1920x1080 -b:v 50000k -maxrate 50000k -bufsize 3835k -minrate 50000k -r 25 -flags ilme -top 1 -acodec pcm_s24le -ar 48000 -pix_fmt yuv422p -map 1:0 -c:a pcm_s24le -ar 48000 -map 2:0 -c:a pcm_s24le -ar 48000  "C:\Users\Username\Documents\MergedFiles\PAV_PRG_13717High_Result.Mxf"

C:\Admin>

我认为这是合并两个单声道音频流并将所有三个单声道音频流合并到一个输出文件中的正确语法。

在FFMPeg中,我花的时间比我预期的要长得多,而且您很可能希望做一些更复杂的事情,因此,可以随意替换为自己的东西。 :)

我认为与原始发布内容不再相关的注释:

  

文件名匹配,直到看到单词High。不需要High之后的所有内容(它只是一个随机字符串),因此尝试匹配是一个问题。

我发现一点不清楚:

每个文件的所有文件都是“ Highc”还是“唯一”?

或者“高”对于每个视频和“ c”都是唯一的,它之后的所有内容都将被忽略吗?

IE:您只需要查找PAV_PRG_13683还是PAV_PRG_13683High还是PAV_PRG_13683Highc

也总是下划线总是存在吗? PAVPRG呢?

我怀疑每个文件为每个移动/音频对唯一定义的唯一值是唯一值是1368313683High13683Highc

IE。我相信每个文件都将以PAV_PRG_作为前缀,从而使该值不再需要匹配,并且可以使过程更简单地工作。目前尚不清楚“ High”还是“ Highc”在所有文件上都相同,但是如果确实不需要前缀,则这两种方法都很容易。

仍然,现在我将假定High是非唯一的,因此现在所有文件都具有“ High”

也就是说,我能够确定正确的左右音频通道。

我注意到在音频文件中High和c之间的空格显然有一个00和01,音频通道通常会出现在Channel 0 = Left Channel,Channel 1 = Right Channel上。 (来源是立体声,2.1还是5.1。)

在处理音轨时,软件将遵循此默认方法来引用通道,并且应输出到带有00的文件,表示第一个音频通道(左),01表示第二个音频通道(右),除非采取了某些措施来更改这种行为。

在上述假设下,这样做很有必要。

请注意,您需要编辑脚本以在提供的变量中设置正确的路径。