我用过程序 youtube-dl 要下载 Youtube 播放列表,我选择单独下载视频和音频,我现在有一个文件夹,里面装满了包含相应音频的视频,我希望将它们与 ffmpeg 合并在一起。
我需要使用批处理脚本来执行此操作,但问题是 youtube-dl 在原始文件的标题后添加了随意的字母,因此视频与其相应的音频名称不同,文件名如下所示:
First title in the playlist 5J34JG.mp4
First title in the playlist H3826D.webm
Second title in the playlist 3748JD.mp4
Second title in the playlist 6SHJFZ.webm
如何使用 Windows 批处理脚本和 ffmpeg 合并这些多个视频/音频文件?
编辑:我忘了提到 .webm 文件是音频文件,我有多个文件,我不能一个一个地重命名它们。
最佳答案
@echo off
setlocal
set "outdir=muxed"
if not exist "%outdir%" md "%outdir%" || exit /b 1
:: Get each mp4 file and call the label to mux with webm file.
for %%A in (*.mp4) do call :mux "%%~A"
exit /b
:mux
setlocal
set "videofile=%~1"
set "name=%~n1"
:: Last token of the name is the pattern to remove.
for %%A in (%name:-=- %) do set "pattern=-%%~A"
:: Remove the pattern from the name.
call set "name=%%name:%pattern%=%%"
:: Get the webm filename.
for %%A in ("%name%-*.webm") do set "audiofile=%%~A"
:: Mux if webm file exist and output file does not exist.
if exist "%audiofile%" if not exist "%outdir%\%name%.mp4" (
ffmpeg -i "%videofile%" -i "%audiofile%" -c copy "%outdir%\%name%.mkv"
)
exit /b
该脚本将首先制作输出目录以保存 mp4
文件,以便输出不会成为输入。
主
for
循环将获取每个 mp4 文件名。 :mux
label 以 mp4 文件名作为参数调用。变量
videofile
存储 mp4 文件名和变量
name
只存储没有扩展名的名称。name
的最后一个 token 将是 YTID 模式,而for
循环会将最后一个标记设置为 pattern
.call set
将用“”替换模式姓名。这将给出一个可以与通配符一起使用的名称
找到 webm 文件名。一个
for
循环将获取 webm文件名。
如果目标确实存在而不是输出文件,则
ffmpeg 会将 2 个文件混合到一个输出文件中。
输出文件容器格式为 mkv。
关于batch-file - 使用 ffmpeg 合并多个视频和音频,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51332030/