Batch overlay logo to directory of video files - batch-file

I am using this command in windows 8 to overlay a logo (mywatermark.png , 400x200) to a video file:
ffmpeg -i E:\source\video01.mp4 -i E:\logo\mywatermark.png -filter_complex "[1:v]scale=100:50 [ovrl], [0:v][ovrl
]overlay=W-w-10:H-h-10[watermark]" -map "[watermark]" -map 0:a -codec:v libx264 -preset veryfast -crf 18 -codec:a copy E:\output\video01.mp4
as you can see I am first rescaling my logo to 100:50 pixels, and then overlaying it to the bottom right corner.
This is working for me.
My question is:
How can I repeat this to all of the videos in the same directory, adding the same logo to all?
I want to save the output video files same as the original, of course in different folder. However, not to forget that my source video files are of different quality and resolution.
How can I add a parameter to the command line to proportionnally scale the logo in regard to the video file resolution?
I am afraid that if I don't do this, the logo will be small in one file and bigger in another.
thanks a lot

How can I repeat this to all of the videos in the same directory, adding the same logo to all?
Windows command line processor - cmd.exe - offers the command FOR for executing a command on all files in a directory.
#echo off
for %%I in ("E:\source\*.mp4") do ffmpeg.exe -i "%%I" -i E:\logo\mywatermark.png -filter_complex "[1:v]scale=100:50 [ovrl], [0:v][ovrl]overlay=W-w-10:H-h-10[watermark]" -map "[watermark]" -map 0:a -codec:v libx264 -preset veryfast -crf 18 -codec:a copy "E:\output\%%~nI.mp4"
Running in a command prompt window for /? or help for outputs help for this command over several display pages. As help of command FOR is long, it is advisable to redirect the output help into a text file and read the file with Notepad or any other text editor.
for /? >"%USERPROFILE%\Desktop\Help_FOR.txt"
Notepad "%USERPROFILE%\Desktop\Help_FOR.txt"
In above batch code %%I is replaced by name of current *.mp4 file with full path and %%~nI with just name of current *.mp4 file without path and without file extension.

As #LordNeckbeard commented, you can use the scale2ref filter to proportionally scale the watermark, like so
ffmpeg -i E:\source\video01.mp4 -loop 1 -i E:\logo\mywatermark.png \
-filter_complex "[1:v][0:v]scale2ref=iw/8:-1[ovrl][0v]; \
[0v][ovrl]overlay=W-w-10:H-h-10[watermark]" \
-map "[watermark]" -map 0:a \
-codec:v libx264 -preset veryfast -crf 18 -c:a copy E:\output\video01.mp4
The iw/8 specifies that the watermark's width and height be scaled to 1/8th of the base video's width and height. The scale2ref filter has the same options available as the scale filter.

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.

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
)

Workaround to .ts files in video merging?

I've got these sentences of code that I use for video processing, the first one adds a watermark, the second creates a .ts file of the watermarked video and then adds an intro and outro to it but I found that it quickly fills disk space because of the .ts files being used, is there a way to achieve the same results without them? Either by deleting them right after using them or by using a different process altogether.
for %%I in ("C:\Users\Administrator\Desktop\work\*.mp4") do ffmpeg.exe
-y -i "%%I" -i white.png -filter_complex "[0:v]scale=iw:ih[v0];[1:v][v0]scale2ref=iw/6:ih/18[logo][0v];[0v][logo]overlay=W-w-3:H-h-1[v]"
-map "[v]" -map 0:a -codec:v libx264 -preset ultrafast -crf 23 -codec:a copy "C:\Users\Administrator\Desktop\Complete-videos\%%~nI.mp4"
for %%I in ("C:\Users\Administrator\Desktop\Complete-videos\*.mp4") do ffmpeg -y
-i %%I -c copy -vbsf h264_mp4toannexb -f mpegts -s 1280*720 %%I.ts && ffmpeg -y -i "concat:out1.ts|%%I.ts|out1.ts" -c:v libx264 -strict experimental -bsf:a aac_adtstoasc -ar 48000 -r 20 "C:\Users\Administrator\Desktop\Complete-videos\%%~nI.mp4
pause
This does it in one command, with no intermediate files.
for %%I in ("C:\Users\Administrator\Desktop\work\*.mp4") do ffmpeg.exe
-y -i "%%I" -i white.png -i out1.ts -filter_complex "[1:v][0:v]scale2ref=iw/6:ih/18[logo][0v];[0v][logo]overlay=W-w-3:H-h-1,scale=hd720,setsar=1[vl];[2:v][2:a][vl][0:a][2:v][2:a]concat=n=3:v=1:a=1[v][a]"
-map "[v]" -map "[a]" -r 20 -c:v libx264 -ar 48000 "C:\Users\Administrator\Desktop\Complete-videos\%%~nI.mp4"
pause
Notes: Depending on where out1.ts is, you may have to specify its full path during input. In the first command, in your 2nd step, the size option has no effect as you're copying streams. If resize is needed, insert scale=hd720 after the concat filter in my command. Add -strict experimental if your ffmpeg is older than Dec 2015 and you don't want to upgrade.
[totally untried suggestion since I have no idea what the ffmpeg line is doing]
I suggest you try this on a small file subset on a device with a restricted space like a ramdrive or a usb drive. USB drive may take a while. Coffee is good.
for %%I in ("C:\Users\Administrator\Desktop\work\*.mp4") do (
ffmpeg.exe -y -i "%%I" -i white.png -filter_complex "[0:v]scale=iw:ih[v0];[1:v][v0]scale2ref=iw/6:ih/18[logo][0v];[0v][logo]overlay=W-w-3:H-h-1[v]"
-map "[v]" -map 0:a -codec:v libx264 -preset ultrafast -crf 23 -codec:a copy "C:\Users\Administrator\Desktop\Complete-videos\%%~nI.mp4"
for %%Q in ("C:\Users\Administrator\Desktop\Complete-videos\%%~nI.mp4") do ffmpeg -y
-i %%Q -c copy -vbsf h264_mp4toannexb -f mpegts -s 1280*720 %%Q.ts && ffmpeg -y -i "concat:out1.ts|%%I.ts|out1.ts" -c:v libx264 -strict experimental -bsf:a aac_adtstoasc -ar 48000 -r 20 "C:\Users\Administrator\Desktop\Complete-videos\%%~nQ.mp4
del *.ts
)
pause
This way, the first loop using %%I selects the file to be processed and apparently puts the processed file in another directory then immediately performs the second process on that file, controlled by %%Q using the name in %%~nI to select the single file created by the previous process.
Consequently, instead of processing 10 files each with its own .ts set, one is processed at a time and only one .ts set is created. I don't know whether the second process deletes the .ts files, but I've added a del to get rid of them on each cycle. I've also no idea whether the first process creates the .ts files, but I've made that assumption.
As the educationalists said about "new maths" - it's the idea that's important.

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

ffmpeg batch retains old filetype in name

I'm using the following ffmpeg script to convert all .mp4 files in the folder to .webm.
for %%A IN (*.mp4) DO ffmpeg -i "%%A" -c:v libvpx -crf 4 -b:v 3M -c:a libvorbis "%%A.webm"
I am trying to make it go from "xxx.mp4" to "xxx.webm"
However when I run the script the file name goes from "xxx.mp4" to "xxx.mp4.webm"
Any suggestions are highly appreciated.
Use %%~nA.webm to select the name-part of the filename.
see for /? from the prompt or documentation.
To recursively convert in webm all the mp4 video files in nested folders, you can try this command:
find '~/Video/' -iname '*.mp4' -exec bash -c 'D=$(dirname "{}"); B=$(basename "{}"); mkdir "$D/webm/"; ffmpeg -i "{}" -c:v libvpx -crf 4 -b:v 3M -c:a libvorbis "$D/webm/${B%.*}.webm"' \;
It will create a folder named "webm" inside the one with mp4 video files and, inside the "webm" folder, it will save relative webm files, without keeping the old file extension in the name.

Resources