ffmpeg batch script that combines multiple audio clips with one image > mp4 - batch-file

I found this script in another thread and edited it to make this:
#echo off
for %%F in (*.jpg) do (
if exist "%%~nF.mp3" (
ffmpeg -loop 1 -i "%%~nF.jpg" -i "%%~nF.mp3" -acodec libvo_aacenc -vcodec mpeg4 -shortest "%%~nF.mp4"
)
)
It combines all mp3 files with all jpeg files that share the same filename into a mp4 video with the static image running.
But the problem is - if I want to convert more than one mp3 file using the same image (let's say three mp3s), I have to duplicate the same jpeg image three times!
For example, if I have three Michael Jackson mp3 files:
MJ1.mp3
MJ2.mp3
MJ3.mp3
I would need to create:
MJ1.jpg
MJ2.jpg
MJ3.jpg
But really I only need one image for all three mp3s.
What edits do I need to make to the script combine all mp3s with one jpeg file? Let's say I wanted every mp3 file in the folder to convert with default.jpg. How would I do this?

Invert the loop. Instead of iterating over the images, iterate over the sound files.
#echo off
for %%F in (*.mp3) do (
ffmpeg -loop 1 -i "default.jpg" -i "%%~nxF" -acodec libvo_aacenc -vcodec mpeg4 -shortest "%%~nF.mp4"
)

Related

How to add cover art to MP4?

I'm trying to find an effective way to update the metadata for my MP4 files that I plan to put on a DLNA server. First thing, I want to have the video files have a cover art.
I'm specifically using 640x360 JPG files to be the cover art.
I might also want to add some other tags, like media type or update the Title. Please let me know what can be done.
If code suggestions are available, please know I have the file name details in a variable %file% to handle things. The JPG has the same name as the source MP4 file, so it's easy enough to get the file type and remove the extension, which is what I've done so far.
My goal is to be able to simply drop the MP4 file on the following batch file and know its filename and full path, the JPG file, and attach it to the orgininal MP4 file. Apparently ffmpeg.exe won't write to the file it pulled from, so I have it go to a temp file and then use MOVE to replace the old file with the fixed file.
#ECHO OFF
set arg=%1
set file=%arg:~1,-5%
ffmpeg -i "%file%.mp4" -i "%file%.jpg" -acodec copy -vcodec copy -map 0 -map 1:0 "%file%WIP.mp4"
move /Y "%file%WIP.mp4" "%file".mp4"
This code did not seem to work. It doesn't show up in Windows as the cover art, so I'd say it failed.
Can would do a try, by to change the argument and the position of -map 0 -map 1:0 to immediately after name file input -map 1 -map 0 along command line, so, this work to me...
Sorry my limited English.
#echo off & cd "%~dp0" && setlocal enableextensions enabledelayedexpansion
for %%i in (%*) do (
set "_arg=%%i"
set "_file=!_arg:~1,-5!"
if exist "!_file!.mp4" ffmpeg -i "!_file!.mp4" -i "!_file!.jpg"--map 1 -map 0 -acodec copy -vcodec copy "!_file!WIP.mp4" && move /Y "!_file!WIP.mp4" "!_file!".mp4"
shift
)

merge multiple videos and audios with ffmpeg

I have used the program youtube-dl to download a Youtube playlist, i have chosen to download videos and audios separately, i have now a folder full of videos with their corresponding audios that i wish to merge together with ffmpeg.
I need to do this using a batch script but the problem is that youtube-dl added aleatory letters after the original file's title so the videos doesn't have the same name as their corresponding audio, file names looks like this:
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
How to merge these multiple video/audio files using windows batch script and ffmpeg ?
Edit: I forgot to mention that the .webm files are the audio files and that i have multiple files i can't rename them one by one.
#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
The script will 1st make the output directory to save mp4
files so that the output will not become input.
The main for loop will get each mp4 filename. The :mux
label is called with the mp4 filename as an argument.
The variable videofile stores the mp4 filename and
variable name stores just the name without extension.
The last token of name will be the YTID pattern while a
for loop will set the last token to pattern.
The call set will substitute the pattern with "" from the
name. This will give a name that can used with a wildcard to
find the webm filename. A for loop will get the webm
filename.
If the destination does exist and not the output file, then
ffmpeg will mux the 2 files into an output file.
Output file container format is mkv.
Since I was needing to do something similar, I am posting my shorter code to help others out.
This is for in a subfolder
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"
This is for if you're doing it in the same directory
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"
This is if ffmpeg is not in the same folder
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"
Just example codes, not specifically formatted for the OP. Use with caution.

ffmpeg Converting files in place

I use ffmpeg on a daily basis to convert multiple video files to various other formats. Because of this I can't be moving ffmpeg and multiple batch files all over the place.
I have the folder C:\ffmpeg which contains ffmpeg.exe and all my different batch file converters for the different formats. I then have a shortcut on the desktop to the batch file which I can drag and drop video files onto to convert them.
As it stands, I use this to convert the video files in place.
FOR %%A IN (%*) DO (
"%~dp0\ffmpeg.exe" -i %%A -c:v libx264 -crf 22 -c:a aac %%A_Converted.mp4
)
Dragging and dropping the video file example.avi from the folder C:\Users\User\Documents\Folder results in the file example.avi_Converted.mp4 being created in the same location (perfect!).
I am wanting to remove the original file type, .avi from the new file name - so the output file is example_Converted.mp4. I can do this using ~n, shown in the following code sample, although the new file then gets created in C:\ffmpeg instead of in C:\Users\User\Documents\Folder.
FOR %%A IN (%*) DO (
"%~dp0\ffmpeg.exe" -i %%A -c:v libx264 -crf 22 -c:a aac "%%~nA_Converted.mp4"
)
Is there a way to do this so that the output file is created is the same place as the input file?

Batch Copy Cover Art from One Song to Another (Identical File Names)

I'm looking for a way to copy the album art from one set of songs to another set of the same songs that don't have the album art. FFMPEG copies the text tags (artist, album, title...) perfectly, but will not copy the embedded album artwork.
I've been trying to batch convert my folder of MP3s into 128k AAC (using FFMPEG and the libfdk-aac codec) files to save space on my phone, but it hasn't copied my album art over to the new songs.
I used the following batch command:
FOR /F "tokens=*" %%G IN ('dir /b *.mp3') DO ffmpeg -i "%%G" -c:a libfdk_aac -b:a 128k "%%~nG.m4a"
EDIT: I tried using the following command to test it out, because stream 0:0 is the audio, and stream 0:1 is the JPEG, however it did not work:
ffmpeg -i Estranged.mp3 -map 0:0 -map 0:1 -c:a libfdk_aac -b:a 128k -c:v copy Estranged.m4a
Here's a paste of the log: http://pastebin.com/dZFsvR7F (I know, it's not the latest version. I had trouble compiling it myself but I'm currently working on it.)
Is there a way to copy the album art (or entire tag, if need be) from songs with an identical name in one folder to the songs in the new folder?
I.E. C:\Folder1\song.mp3 → C:\Folder2\song.m4a
Thanks.
ffmpeg -y -i input1.mp3 -i input2.mp3
-map 1 -codec copy
-map 0:1 -map_metadata 0 -id3v2_version 3
o.mp3
takes audio from input 2
takes metadata and album cover from input 1 in the highly-compatible id3v2.3 format
this is to be modified as needed

Extract WAV from an MP4, process and then remux it back

I have a large number of x264+AAC MP4 files. I have written an audio filter that operates on PCM wav files that I want to apply to the audio on all my videos, replacing the existing audio with the filtered version.
I eventually accomplished this with a DOS batch file and FFMPEG:
ffmpeg -i "%~f1" "%~f1.wav"
audiofilter "%~f1.wav"
ffmpeg -i "%~f1" -i "%~f1.wav" -vcodec copy -acodec libvo_aacenc "%~f1.complete.mp4"
move /Y "%~f1.complete.mp4" "%~f1"

Resources