FFMPEG batch for loop with multiple inputs - batch-file

What we have:
test.mp3
test.mp4
We want to merge this using ffmpeg
ffmpeg -i "test.mp3" -i "test.mp4" -c copy D:\test_final.mp4
The thing is we just need this to do for tons of videos.
What we currently have:
for %%a in ("merge\*.*") do ffmpeg -i "%%a" -i "%%a" -c copy D:\%%~na
So, what's the correct form? I have no idea :/
In merge there are always mp3 and mp4 files with the same name.

What you have looks pretty darn good, and you didn't describe exactly how it does not work for you.
To match your desired line more exactly:
Get only *.mp3 files
Use %%~pa%%~na to get the path and filename-only, but no extension, then add a .mp4 on the end of it.
Add a _final to the resulting name
If this isn't working for you, please describe exactly how it fails.
for %%a in ("merge\*.mp3") do (
ffmpeg -i "%%a" -i "%%~pa%%~na.mp4" -c copy D:\%%~na_final.mp4 )

Related

Using FFMPEG to batch remove audio endings

I have over 1000 audio files, all of which end in a mouse click. I would like to remove the last half second from all of them. The audio files have different length (i.e. 15sec, 5 sec ...) But one thing in common with all of them is the last half second has a mouse click sound. How do I trim in bulk the ending of the mp3 files within a folder using windows 10 command line? I already have FFMPEG downloaded. Thank you!
This is two questions in one:
How to remove the last 0.5 seconds from inputs of arbitrary durations?
How to incorporate this into a Windows batch script?
I'll answer #1 because I'm not a Windows user. The batch scripting will be up to you.
Get duration using ffprobe:
ffprobe -v error -show_entries format=duration -of csv=p=0 input.mp3
Trim using ffmpeg:
ffmpeg -i input.mp3 -t <duration> -c copy output.mp3
Replace <duration> with the output from ffprobe minus 0.5 seconds.
How to incorporate this into a Windows batch script?
This should do:
FOR %%A IN (*.mp3) DO (
FOR /F %%B IN ('ffprobe.exe -v error -show_entries format^=duration -of csv^=p^=0 "%%~A" ^| xidel -s - -e ". - 0.5"') DO (
ffmpeg.exe -i "%%~A" -t %%B -c copy "%%~dpnA_trimmed.mp3"
)
)
First of all, doing floating point calculations in Batch is officially impossible and unofficially really hard to script. That's why I suggest to let Xidel do the math. It's first of all a command line tool to download and extract data from HTML/XML/JSON, but it can do A LOT more!
Loop over all mp3-files in the current directory.
The ffprobe command as suggested by llogan piped to Xidel to subtract the 0.5s. For example, 25.547755 now becomes 25.047755.
Don't forget to escape the necessary characters inside the for-loop! The = and | in this case.
The ffmpeg command as suggested by llogan, which opens "%%~A (the mp3-file), sets the duration to %%B and creates a new mp3-file (<filename>_trimmed.mp3).
This code assumes the mp3-files, ffprobe.exe, xidel.exe and ffmpeg.exe are all in the same directory.

Troubleshooting ffmpeg batch-file. mkv to mp4 conversion with subtitle hardcode

I am trying to batch convert a large number of .mkv video files to .mp4 while hard coding the subtitles included in the .mkv files. I would like to retain as much quality as possible.
I am not an adapt at using ffmpeg or writing batch files, so I have been searching online for batch files which will do what I need. I found some which successfully convert without the subtitles, and I found one that included the command for hard coding the subtitles, but that one also had excessive commands that I didn't want to delve into, so I am attempting to combined the relevant parts of the two scripts. That is where the problem is obviously coming from.
for %%a in (*.mkv) do ffmpeg -i "%%~a" -vf subtitles=%%~na.mkv copy -c:a aac -q:a 100 "%%~na.mp4"
pause
It is worth noting that the script was originally:
for %%a in (*.mkv) do ffmpeg -i "%%~a" -vcodec copy -c:a aac -q:a 100 "%%~na.mp4"
pause
But I changed it as best I could to include a section of script I found which should apparently hard code the subtitles. My lack of knowledge about how this scripting language actually functions probably just made me insert the command in the wrong place or in the wrong way. I hope that background information makes this problem easier to solve.
Expected results: convert each .mkv file in the current folder and leave the .mp4 file in the same folder.
Actual results:
[NULL # 0000026a399f1440] Unable to find a suitable output format for 'copy': Invalid argument.
Not a batch file user, but I can comment on the ffmpeg command. Use:
for %%a in (*.mkv) do ffmpeg -i "%%~a" -vf subtitles=%%~na.mkv "%%~na.mp4"
pause
You can't use -vcodec copy/-c:v copy with -vf because filtering requires encoding.
If the input audio is already AAC then add -c:a copy to stream copy (re-mux) the audio instead of re-encoding it.
This will rely on the default stream selection behavior. See the -map option if you want to include/exclude certain streams such as if your input has multiple video and/or audio streams.
I figured this out, thanks to comments from people here which helped me search out relevant info.
One of the main, and most embarrassing problems is that the original batch file was in Linux format and I am running on Windows.
This is the working batch file that I pieced together by editing someone else's batch file. I will leave a link to the original batch file as well.
for %%A IN (*.mkv) DO ffmpeg -i "%%A" -vf subtitles="%%A" "%%A.mp4"
pause
This is where I got the original code from: https://superuser.com/questions/470500/batch-convert-avi-files-using-ffmpeg#470510
And this is where I got information about hard coding the subtitles: https://superuser.com/questions/932730/ffmpeg-mkv-to-mp4-conversion-loses-subtitles
Thanks again :)

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
)

ffmpeg: Output to the same folder as source with recursive input?

I have a lot of gifs I want converted to webms in many sub directories, and I have this script which will do it, but it will output to the directory where the script is located:
for /r %%a in ("*.gif") do ffmpeg -i "%%a" -c:v libvpx -crf 12 -b:v 4000k "%%~na.webm"
pause
I've tried a bunch of things, but I can't figure out how to get the output to land in the same sub directory as the input file so I can maintain the folder structure.
Edit: it's a Windows batch file I'm using.
You are missing two command modifiers for your output filename.
"%%~dpna.webm"

Bulk converting MKV files to MP4 with AAC using a Windows batch file

So I have been trying for days to work out how to do this and this site has helped me but I am still stuck.
I basically want to run a batch file and it goes through a folder including all subdirectories and changes the container of all mkv files to mp4 converting the sound to AAC if necessary. Preferably it will create the new mp4 file in the same place with the same name and then delete the mkv.
All I have so far is this which if I place it in a certain folder will create the mp4's in a specified folder called newfiles. I am not sure if I have used libfaac correctly either. Would greatly appreciate any advice. I would have liked to use libfdk_aac but I have tried everything to get it and I cant find it.
for %%a in ("*.mkv") do C:\ffmpeg\bin\ffmpeg -i "%%a" -vcodec copy -acodec libfaac "Z:\newfiles\%%~na.mp4"
pause
Try this for variable bit rate (VBR):
for %%a in (*.mkv) do C:\ffmpeg\bin\ffmpeg -i "%%~a" -vcodec copy -c:a libfaac -q:a 100 "%%~na.mp4"
pause
Range for -q:a is 10-500 and is similar to using the -q option in standalone faac. 100 is a good value to try.
or try this for average bit rate (ABR):
for %%a in (*.mkv) do C:\ffmpeg\bin\ffmpeg -i "%%~a" -vcodec copy -c:a libfaac -b:a 192k "%%~na.mp4"
pause
FFmpeg and AAC Encoding Guide: libfaac
Note that you will not get as good results as with libfdk_aac.
The license of libfdk_aac is not compatible with the GPL, so the GPL does not permit distribution of binaries containing code licensed under these licenses when GPL-licensed code is also included. Therefore this encoder has been designated as "non-free", and you cannot download a pre-built ffmpeg that supports this. This can be resolved by compiling ffmpeg yourself. You can do this for example with the help of this project.

Resources