ffmpeg Converting files in place - batch-file

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?

Related

Batch file to perform multiple conversions in a row

I'm trying to write a batch file that can be launched from Handbrake GUI with the "send file to" feature, that will split an MKV into chapters with MKVmerge, then convert all those new files into mp4s with ffmpeg. Handbrake passes only one argument into the batch file, the full file path of the output (surrounded by quotes). I'm very new to batch scripting and am having difficulty getting it all to work together -- all the individual parts work just fine when absolute paths are given. Here's the content of the .bat file:
START /WAIT "C:\Program Files\MKVToolNix\mkvmerge.exe -o output.mkv --split chapters:all %1"
for %%i in (*.mkv) do C:\ffmpeg\bin\ffmpeg.exe -i "%%i" -loop 1 -i "%%~dpi"\folder.jpg -map 1:v -map 0:a -c:a ac3 -b:a 640K -pix_fmt yuv420p -c:v libx264 -shortest -fflags +shortest -max_interleave_delta 100M "%%~ni.mp4"
Any glaring mistakes I'm making? I've been at this for hours reading SO threads and documentation, and can't figure it out for the life of me. Any help is appreciated, thanks in advance.

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 batch file to re-encode movies that don't contain string "h265" in file name?

I have a batch file that uses FFMPEG to re-encode movies to h265 to reduce file size. I have a few videos that I previously re-encoded over the past couple of years. I want to skip those and at the moment I just manually move them from subfolders before running my script, but this is getting tedious. I'd like to add a command to my batch file that skips those files automatically. All files that have been re-encoded in the past have h265 appended before the file extension. My batch file looks like this, but it isn't skipping the files with h265 in them:
for %%a in ("*.*") do findstr /v /c:"h265" "%%a" >nul && \
ffmpeg -i "%%a" -c:v libx265 -preset ultrafast \
-x265-params [parameters] -c:a copy "%%~na.h265.mp4"
pause
What do I need to change to get this to work?

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

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

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