使用 ffmpeg 合并多个视频和音频
merge multiple videos and audios with ffmpeg
我已经使用程序 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 合并这些多个 video/audio 文件?
编辑:我忘了说 .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
使用 mp4 文件名作为参数调用标签。
变量videofile
存储mp4文件名和
变量 name
仅存储没有扩展名的名称。
name
的最后一个标记将是 YTID 模式,而
for
循环会将最后一个标记设置为 pattern
。
call set
会将模式替换为来自
姓名。这将给出一个可以与通配符一起使用的名称
找到 webm 文件名。 for
循环将得到 webm
文件名。
如果目标确实存在而不是输出文件,那么
ffmpeg 会将这 2 个文件混合到一个输出文件中。
输出文件容器格式为mkv。
由于我需要做类似的事情,所以我发布了我的较短代码以帮助其他人。
这是在子文件夹中的
FOR /F "tokens=*" %%f IN ('dir /b /s F2M\*.mp4') DO ffmpeg -i "%%~dpnxf" -i "%%~dpnf.m4a" -c:v copy -c:a aac -map 0:v:0 -map 1:a:0 "F2M\%%~nf-clean.mp4"
这适用于您在同一目录中执行此操作的情况
for %%f in (*.mkv) do ffmpeg -i "%%~dpnxf" -i "%%~dpnf.m4a" -c:v copy -c:a copy -map 0:0 -map 0:1 -shortest "%%~nf-remux.mp4"
这是如果 ffmpeg 不在同一个文件夹中
set ffmpeg=C:\ffmpeg.exe
for %%a in (*.mkv) do "%ffmpeg%" -i "%%~dpnxf" -i "%%~dpnf.m4a" -c:v copy -c:a copy -map 0:0 -map 1:0 -shortest "%%~nf-remux.mp4"
只是示例代码,没有专门为 OP 格式化。谨慎使用。
我已经使用程序 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 合并这些多个 video/audio 文件?
编辑:我忘了说 .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
使用 mp4 文件名作为参数调用标签。
变量videofile
存储mp4文件名和
变量 name
仅存储没有扩展名的名称。
name
的最后一个标记将是 YTID 模式,而
for
循环会将最后一个标记设置为 pattern
。
call set
会将模式替换为来自
姓名。这将给出一个可以与通配符一起使用的名称
找到 webm 文件名。 for
循环将得到 webm
文件名。
如果目标确实存在而不是输出文件,那么 ffmpeg 会将这 2 个文件混合到一个输出文件中。
输出文件容器格式为mkv。
由于我需要做类似的事情,所以我发布了我的较短代码以帮助其他人。
这是在子文件夹中的
FOR /F "tokens=*" %%f IN ('dir /b /s F2M\*.mp4') DO ffmpeg -i "%%~dpnxf" -i "%%~dpnf.m4a" -c:v copy -c:a aac -map 0:v:0 -map 1:a:0 "F2M\%%~nf-clean.mp4"
这适用于您在同一目录中执行此操作的情况
for %%f in (*.mkv) do ffmpeg -i "%%~dpnxf" -i "%%~dpnf.m4a" -c:v copy -c:a copy -map 0:0 -map 0:1 -shortest "%%~nf-remux.mp4"
这是如果 ffmpeg 不在同一个文件夹中
set ffmpeg=C:\ffmpeg.exe
for %%a in (*.mkv) do "%ffmpeg%" -i "%%~dpnxf" -i "%%~dpnf.m4a" -c:v copy -c:a copy -map 0:0 -map 1:0 -shortest "%%~nf-remux.mp4"
只是示例代码,没有专门为 OP 格式化。谨慎使用。