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

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

Related

ffmpeg .bat script to change frame rate for multiple clips in a folder?

How do I write a .bat file in Windows 10 that changes the frame rate for multiple mp4 video clips in a folder? For example; change the frame rate from 50fps to 25fps (without re-encoding or dropping frames, so that footage is essentially slowed down.)
At the moment these are the commands I've tried using in two separate .bat text files. (I'd like to combine them but don't know how yet).
for %%A IN (*.mp4) DO ffmpeg -y -i "%%A" -c copy -f h264 "%%A.h264"
for %%A IN (*.h264) DO ffmpeg -y -r 25 -i "%%A" -c copy
"%%A_25.mp4"
Problem is these commands don't replace the file extension type, they append to the existing one, ie. '.mp4' becomes '.mp4.h264' then '.mp4.h264_25fps.mp4', and I can't get the second one to work for some reason.
Any advice appreciated. How do I replace the existing file extensions for a group of clips and combine commands into a single .bat?

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 batch for loop with multiple inputs

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 )

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