FFmpeg: Batch convert all audio (mp3) in folder to video (mp4) with album artwork

泪湿孤枕 提交于 2021-02-18 18:58:36

问题


I'm looking to batch convert all audio (mp3) in folder to video (mp4) with album artwork. This for uploading audios to youtube. I have pretty much a working code but I want to automate the whole thing.

Here's the code from .bat file I'm using.

(source:FFMpeg Batch Image + Multiple Audio to video)

echo off
for %%a in ("*.mp3") do "C:\ffmpeg\bin\ffmpeg" -loop 1 -i  "C:\ffmpeg\bin\input.jpg.jpg" -i "%%a" -c:v libx264 -preset veryslow -tune stillimage -crf 18 -pix_fmt yuv420p -c:a aac -shortest -strict experimental -b:a 192k -shortest "C:\mp4\%%~na.mp4"
pause
  • "C:\ffmpeg\bin\ffmpeg" the folder of codec
  • "C:\ffmpeg\bin\input.jpg" image path
  • "C:\mp4\%%~na.mp4" output folder
  • -vf scale=854:480 you can specify the resolution of your video 16:9 for youtube video
  • -c:a aac -shortest use aac codec, by specify -shortest the video length will match the audio length

This works great except I have to manually put the album art each time. I want ffmpeg to automatically extract the album art from each audio file and then convert it to video in batch.

This is the code for extracting album art from .mp3 files

ffmpeg -i input.mp3 -an -vcodec copy cover.jpg

Thanks.


回答1:


Just move the extract command into the loop. Something like (untested) this:

echo off
for %%a in ("*.mp3") do (
  ffmpeg -i "%%a" -an -vcodec copy "C:\mp4\%%~na.jpg"
  "C:\ffmpeg\bin\ffmpeg" -loop 1 -i  "C:\mp4\%%~na.jpg" -i "%%a" -c:v libx264 -preset veryslow -tune stillimage -crf 18 -pix_fmt yuv420p -c:a aac -shortest -strict experimental -b:a 192k -shortest "C:\mp4\%%~na.mp4" )
pause



回答2:


I've figured it out. Thanks to @L. Scott Johnson for the help.

Here's the code

echo off
for %%a in ("*.mp3") do (
  "C:\ffmpeg\bin\ffmpeg.exe" -i "%%a" -an -y -vcodec copy "E:\songgs\%%~na.jpg"
  "C:\ffmpeg\bin\ffmpeg.exe" -loop 1 -i  "E:\songs\%%~na.jpg" -i "%%a" -y -c:v libx264 -preset veryslow -tune stillimage -crf 18 -pix_fmt yuv420p -c:a aac -shortest -strict experimental -b:a 192k -shortest "E:\songs\%%~na.mp4" )
pause

This is the basic version of the code.

You can edit many things from the code.

For example you can add subtitles for synchronized lyrics with this code

-vf "subtitles=%%~na.srt"

Resize the cover with this code

-vf "scale=1500:1500"

And if you want to both of them together just add them with comma (,) Like this:

-vf "pad=ceil(iw/2)*2:ceil(ih/2)*2,subtitles=%%~na.srt"

you can also change the encoder to Nvidia NVENC for faster encoding...

And many more.. you get the idea.

Again, thanks to @L. Scott Johnson for helping me with this code. Feel free to ask any question and I'll try to help. I'm just learning this, as my user name says, I'm a noob :)



来源:https://stackoverflow.com/questions/59902282/ffmpeg-batch-convert-all-audio-mp3-in-folder-to-video-mp4-with-album-artwor

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!