Dens
Dens

Reputation: 125

How can I apply custom font on watermark that created by FFmpeg

I am creating a video with FFmpeg library. I want access fonts from Android assets folder and overlay on my video as watermark.

What is the path for Android assets folder to access and apply custom font on FFmpeg? I can overlay watermark on video but when I try to apply custom font on it, it gives error related to not exist, etc.

What I found till now:

The alternative solution that I found is the create an image file and write text on it and overlay image on video. problem is text and image being stretch output. I need a solution for it.

String[] execute = {"-y",  "-i",  videoPath, "-i", image, "-filter_complex",
             "[1:v]scale=" + width + ":" + height + "[ovrl],[0:v][ovrl]overlay=(main_w-overlay_w)/2:(main_h-overlay_h)/2"
             ,"-strict" , "experimental", "-vcodec", "libx264" , "-preset",  "ultrafast" , "-crf",  "20", "-acodec",  "aac",  "-ar",
             "44100",  "-ac",  "2",  "-b:v",  "36000k",   outputPath.getPath()};

I thought itz output will be good but it stretching my text and image.

I want some perfect solution to apply assets font on FFmpeg.

Thank you in Advance.

Upvotes: 3

Views: 2103

Answers (1)

Vinesh Chauhan
Vinesh Chauhan

Reputation: 1378

Assets Directry is nt directly accessible by FFMPEG you have to copy that in internal storage then you can pass t to ffmpeg otherwise ffmpeg troughs error ffmpeg is native library for vdeo editing so it can fetch assets data

You can use drawtext attribute to draw text on Image/ Video. Drawtext has its own parameter for fontstyle here is a simple example code how to draw text on video/image with custom fontstyel and color

ffmpeg -i input.mp4 -filter_complex "drawtext=text=Vinesh Chauhan:fontcolor=#000000:fontsize=14:x=43:y=103:fontfile=FACEBOLF.OTF" -y output.mp4

here

text= what you want to draw as text on video/image

fontcolor = color for font

fontsize = fontsize for your text

x and y is used to draw your text on specified co-ordinates

fontfile= pass you fontsyle file(TTF) file path

if you wish to add image as water mark on video do not scale tahat image otherwise it alter the image aspec ratio

use below code that not alter width height of image

String[] execute = {"-y",  "-i",  videoPath, "-i", image, "-filter_complex",
             "[1:v]scale=iw:-2[ovrl],[0:v][ovrl]overlay=(main_w-overlay_w)/2:(main_h-overlay_h)/2"
             ,"-strict" , "experimental", "-vcodec", "libx264" , "-preset",  "ultrafast" , "-crf",  "20", "-acodec",  "aac",  "-ar",
             "44100",  "-ac",  "2",  "-b:v",  "36000k",   outputPath.getPath()};

or your app has custom height width functionality so use below code that will maintain your aspect ratio

String[] execute = {"-y",  "-i",  videoPath, "-i", image, "-filter_complex",
             "[1:v]scale=" + width + ":-2[ovrl],[0:v][ovrl]overlay=(main_w-overlay_w)/2:(main_h-overlay_h)/2"
             ,"-strict" , "experimental", "-vcodec", "libx264" , "-preset",  "ultrafast" , "-crf",  "20", "-acodec",  "aac",  "-ar",
             "44100",  "-ac",  "2",  "-b:v",  "36000k",   outputPath.getPath()};

Upvotes: 7

Related Questions