使用 ffmpeg 将 youtube-dl webm 文件转换为 mp3 的批处理脚本

Batch script to convert, youtube-dl webm file to mp3 with ffmpeg

我在 reddit 上使用 youtube-dl to extract the best possible audio quality from youtube videos. However the best quality audio usually turns out to be the webm format, which isn't useful. I would like to write a batch script that converts the webm file to an mp3 with ffmpeg. I've already tried using this guide 来做这件事,但它似乎不起作用。它似乎创建了一个空的 mp3 文件,当我尝试播放它时显示错误,元数据也完全空白。

这是批处理脚本:

for %%a in ("Downloaded\*.*") do %CD%\ffmpeg\bin\ffmpeg.exe -i "%%a" -vn -ar 44100 -ac 2 -ab 192k -f mp3 "Converted\%%~na.mp3" pause

我还将解释整个事情应该如何运作。

想法是,您使用 youtube-dl 提取尽可能好的音频,然后将该文件放入下载文件夹(见下图),然后 运行 mp3 脚本(使用来自ffmpeg的命令)将Downloaded文件夹中的webm文件转换为mp3文件,并将其放置在Converted文件夹中。 mp3脚本就是上面的代码。但它似乎不能正常工作。

我不是很熟悉批处理脚本,也不熟悉 ffmpeg,所以任何帮助都会得到帮助。

这是补充说明部分的图片。

youtube-dl 已经包含此功能 - 只需指定您想要 mp3:

youtube-dl -x --audio-format mp3 https://www.youtube.com/watch?v=BaW_jenozKc

https://www.youtube.com/watch?v=BaW_jenozKc 替换为您实际的 URL。

webm 使用vorbis 或opus 音频编解码器,两者在质量上都远优于mp3。 aac 音频在 youtube 上可用,它的质量介于 opus 和 vorbis 之间,并且受到媒体播放器和小工具的大力支持。 质量方面,不建议将有损音频重新编码为另一种有损音频,尤其是当其中一种音频格式为 mp3 时。

如果我是你,我会抓住那个 aac。

根据this answer:

youtube-dl -f bestaudio[ext=m4a] --embed-thumbnail --add-metadata <Video-URL>

您可以通过这个脚本进行转换:

for FILE in *.webm; do
    echo -e "Processing video '\e[32m$FILE\e[0m'";
    ffmpeg -i "${FILE}" -vn -ab 128k -ar 44100 -y "${FILE%.webm}.mp3";
done;

将其保存到.sh文件中并执行。它会自动将所有的 .webm 转换为 .mp3

在我的用例中,支持 .ogg

ffmpeg -i "input.webm" -vn -c:a copy "output.ogg"

没有重新编码:-c:a copy

.webm files indeed can contain vorbis audio, but it can also contain opus audio. Also an ogg file can contain both audio formats. One can transfer the audio without conversion to an .ogg file:

https://askubuntu.com/questions/1258842/how-to-save-the-audio-from-webm-to-ogg-without-transcoding-using-ffmpeg#1258910