2

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.

coden00b
  • 55
  • 1
  • 7

2 Answers2

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 :)

coden00b
  • 55
  • 1
  • 7
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
L. Scott Johnson
  • 4,213
  • 2
  • 17
  • 28
  • Thanks so much for your help. It automatically extracts album covers from .mp3 files to the folder. Unfortunately, it doesn't work after that.. I doesn't go on to convert the music to video files after that... Just shows this error.. [png @ 000001cb67b4ce00] Invalid PNG signature 0x89504E470D0A1A. [png @ 000001cb67b49e80] Invalid PNG signature 0x89504E470D0A1A. Error while decoding stream #0:0: Invalid data found when processing input Last message repeated 1 times https://i.imgur.com/lQ2WPrq.png – coden00b Jan 25 '20 at 07:11
  • I had a disparity in the jpg file names. I've edited my answer to correct that. Again, not tested -- so you may have to adjust the second ffmpeg command to match the name/format or whatever produced by the first. If it fails, just try various constructions for the second command (using one of the loop's output jpg files as the example) until you find out what works, then use that in the loop. – L. Scott Johnson Jan 25 '20 at 15:19
  • Thanks again for your help. Unfortunately it doesn't work. Shows the same error.. https://i.imgur.com/uN90IKh.png I'm an absolute noob when it comes to coding. So, there's no chance I'll be able to solve this. If you can, when you have time, please look into this. Should be fairly simple as most of the work is done. I'll be forever grateful. Thanks for your help again. – coden00b Jan 25 '20 at 17:05
  • What command (not in a loop) works correctly with one of the jpg files output by the loop? Once you find that, you should be able to use that as the template for the second command in the loop. – L. Scott Johnson Jan 25 '20 at 21:16
  • Unfortunately I can't code. So, I have no idea how to fix it. If you can, please look into this. Thanks again. – coden00b Feb 04 '20 at 07:55