RocketNuts
RocketNuts

Reputation: 11160

ffmpeg: overlay a png image on a video with custom transparency?

Suppose I have a video foo.mkv and an image bar.png (which happens to contains an alpha channel). I can blend this image over the video like this:

ffmpeg 
 -i foo.mkv 
 -i bar.png 
 -filter_complex "[0:v][1:v]overlay" 
 -vcodec libx264 
 myresult.mkv

(using multiple lines here for readability, normally this is one command line).

Now, besides the png image having an alpha channel of its own, I would also apply a custom overall transparency when blending this image over the video.

In the above example, the image would be visible 100% on top of the video — or at least the parts where its alpha channel is fully opaque.

Is there a way to add a custom overall opacity or transparency blend factor, something like opacity=0.5 or whatever, which would make the image only 50% visible?

Upvotes: 18

Views: 23059

Answers (3)

Adil Mushtaq
Adil Mushtaq

Reputation: 1

use this function For overlay image with transparency in video in FFmpeg Android

fun addimagemerge(inputVideo: String, imageInput: String, output: String): Array<String> {
    val inputs: ArrayList<String> = ArrayList()
    inputs.apply {
        add("-i")
        add(inputVideo)
        add("-i")
        add(imageInput)
        add("-filter_complex")
        add("[1:v]format=argb,geq=r='r(X,Y)':a='0.5*alpha(X,Y)'[zork]; [0:v][zork]overlay")
        add("-preset")
        add("ultrafast")
        add(output)
    }
    return inputs.toArray(arrayOfNulls<String>(inputs.size))
}

Use this for call Function on Convert Button

val outputPath = getFilePath(this, VIDEO)
  
    val query = ffmpegQueryExtension.addimagemerge(
        selectedVideoPath!!,
        selectedImagePath!!,
        outputPath
    )

    CallBackOfQuery().callQuery(this, query, object : FFmpegCallBack {
        override fun process(logMessage: LogMessage) {

            runOnUiThread {
                tvOutputPath.text = logMessage.text
            }
        }

        override fun success() {
            tvOutputPath.text = String.format(getString(R.string.output_path), outputPath)
            processStop()
            runOnUiThread {
                videoPlayclass.stopVideoPlay(bottmLay, videoPlayAct)
                videoPlayclass.videoPlayStart(
                    this@MergeImageVideoActivity,
                    bottmLay,
                    videoPlayAct,
                    outputPath
                )
            }
        }

        override fun cancel() {
            processStop()
        }

        override fun failed() {
            processStop()
        }
    })

Upvotes: -3

Gyan
Gyan

Reputation: 93359

Another option besides geq is colorchannelmixer.

[1:v]format=argb,colorchannelmixer=aa=0.5[zork]

Upvotes: 12

RocketNuts
RocketNuts

Reputation: 11160

I think I got it:

ffmpeg 
 -i foo.mkv 
 -i bar.png 
 -filter_complex "[1:v]format=argb,geq=r='r(X,Y)':a='0.5*alpha(X,Y)'[zork]; 
   [0:v][zork]overlay" 
 -vcodec libx264 
 myresult.mkv

Where 0.5 is the opacity factor. I'm including format=argb so it also works with overlay images that don't have an alpha channel of themselves.

Upvotes: 22

Related Questions