FFMPEG, convert video same as input size with watermark in Batch - batch-file

I would like to watermark some videos in a directory. I use a png watermark of 3840x2160 in size. The watermark should go in fullsize on the video. Because my videos have different resolutions I use this batch script
set /p add_watermark=Wasserzeichen Addieren? (Standard: ja/nein):
if /i "%add_watermark%" == "" set add_watermark=ja
if /i "%add_watermark%" == "ja" (
for %%f in (%cd%\output\tmp\*.mp4) do (
set "filename=%%~nf"
ffmpeg -i "%%f" -i %cd%\watermark\watermark.png -filter_complex "[0:v][1:v]scale2ref=oh*mdar:ih[v0][v1];[v0][v1]overlay=main_w-overlay_w-0:0" -c:a copy -map 0 -y "%cd%\output\%%~nf.mp4"
)
)
That works well with videos in 4k but if my input is a HD video it will scale the output up to 4k in the size of the watermark.
If I try to add
-c:v copy FFMPEG is giving out the error
Streamcopy requested for output stream fed from a complex filtergraph. Filtering and streamcopy cannot be used together.
My question is, how can I set the output as same size as the input video with the scaled watermark without doing this in two steps.

Because it seems there is no other idea how to archive this in on step without re-encoding i did a batch script that extracts the resolution from the video files and use it to change the link to a watermark.png in different resolutions.
As example:
watermark_1920x1080.png
watermark_2560x1440.png
watermark_3840x2160.png
and so on. This is only a impractical workarround (but well it works) i let this question open.

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?

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
)

How to convert rmvb to mp4 with same quality by using ffmpeg?

I found a batch file that can convert rmvb to mp4 using ffmpeg command, but the filesize become very big after converted(700mb become 3gb), so can I change something in the script below to make the filesize smaller and same quality?
FOR /F "tokens=*" %%G IN ('dir /b *.rmvb') DO ffmpeg -i "%%G" -vcodec
mpeg4 -qscale:v 2 -acodec aac "%%~nG.mp4"
And also why Im keep getting this message? Is it something wrong with the script?
You can simplify the for loop to:
for %%G in (*.rmvb) do ffmpeg -i "%%~G" -c:v h264 -c:a aac "%%~nG.mp4"
This converts to h264 video and aac audio. These codecs quite common at present.
Video defaults to CRF 23 while audio should default to 128k.
This should give reasonable quality at perhaps a tenth of the bitrate shown in your image of 9300k down to perhaps under 1000k.
The duration errors happen sometimes and may seem a lot though the end video may still appear as good as the original.

How to convert all files in a specific directory using batch script

I have videos in a folder of .h264 format and i want to convert them to mp4. now by default i can use ffmpeg to do this:
Here is the command:
ffmpeg -i youtube.flv -c:v libx264 filename.mp4
But, new h264 files are being added and I want to keep converting the videos so as to use it for my python script.
I know i can use this to initialize the variable
SET /A num=1
But how do i write a batch script to take all the videos from the directory one by one even though there new videos being added?
I wrote this but this isn't working:
#echo off
SET /A num=1
for file in E:\Records\1\*.h264
do
ffmpeg -i "$file" -c:v libx264 E:\Recods\1\converted\%num%.mp4
move E:\Records\1\"$file" E:\Records\1\done\
set /A num=%num%+1
done
PAUSE
I am making a done folder and moving the videos that have been converted there and in converted folder i am putting converted videos.. Now i just have to run a task scheduler each hour so that if there is a new entry it should convert it and move it to appropriate folder.
You can loop over all files (even with applying a filter) in a folder using the for loop in batch like this:
#echo off
setlocal EnableDelayedExpansion
set /a vid=0
cd /d "your_Folder\Goes here"
for %%f in (*.h264) do (
ffmpeg -i "%%~f" -c:v libx264 "!vid!.mp4"
set /a vid=!vid!+1
)
So what happens here?
The first line is used to have the program running in the correct directory.
After that all files ending with .h264 (might be a different ending in your case; just took it from the question) in that directory are processed using your command. For that the filepath is placed in the spot for the input file and the counter together with .mp4 is placed as the outputfile.
The counter is a bit more tricky because of how sets of parenthesis are evaluated in batch, which is as one whole block. As solution for this there are a lot of questions and answers on SO. Look for "Delayed Expansion batch" and you will find something like this answer.
Placing this as a Task-Scheduler-Task running on a regular basis should keep your folder updated. For actually monitoring the folder I found this impressive code that can be used in combination with the Task-Scheduler to run that on startup. If the monitoring triggers, you can execute the batch-file above and it should be run each time a file is added. You can adjust the powershell-file with file-filters as well to make it fit your needs.

Resources