java ffmpeg 视频水印,Java/JSP:如何在视频上添加WaterMark

I'm trying to add water mark on image and video. For image i got the solution as below

Image water marking code

Method

static void addWatermarkOnImage(String text, File sourceImageFile, File destImageFile) {

try {

BufferedImage sourceImage = ImageIO.read(sourceImageFile);

Graphics2D g2d = (Graphics2D) sourceImage.getGraphics();

// initializes necessary graphic properties

AlphaComposite alphaChannel = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.1f);

g2d.setComposite(alphaChannel);

g2d.setColor(Color.BLUE);

g2d.setFont(new Font("Arial", Font.BOLD, 64));

FontMetrics fontMetrics = g2d.getFontMetrics();

Rectangle2D rect = fontMetrics.getStringBounds(text, g2d);

// calculates the coordinate where the String is painted

int centerX = (sourceImage.getWidth() - (int) rect.getWidth()) / 2;

int centerY = sourceImage.getHeight() / 2;

// paints the textual watermark

g2d.drawString(text, centerX, centerY);

ImageIO.write(sourceImage, "png", destImageFile);

g2d.dispose();

//System.out.println("The tex watermark is added to the image.");

} catch (IOException ex) {

System.err.println(ex);

}

}

Method calling

File sourceImageFile = new File("e:/Test/Watermark/SwingEmailSender.png");

File destImageFile = new File("e:/Test/Watermark/text_watermarked.png");

addTextWatermark("CodeJava", sourceImageFile, destImageFile);

By fallowing above code image WaterMarking is working perfectly for me. In case of video water marking i tried lot of examples in online but nothing was helpful for me. So, kindly any one help me for video water marking in java/jsp

解决方案

The most common and popular open source tool to put a watermark in a video (or text as in another question that just came up) is probably ffmpeg:

ffmpeg can be used in the command line or programatically as part of an application (look at the licensing carefully to make sure it meets your needs if you want to distribute etc).

To use it programatically you can either use the command line tool in a wrapper or use the libraries it is built on directly.

Using the libraries directly (libavcodec, libavutil, libavformat, libavfilter, libavdevice, libswscale and libswresample - see the about page at the link above) should be more efficient and allow greater control etc, but the wrapper approach is still popular as it can be simpler to implement at first and there is a wealth of online info on command line syntax to achieve pretty much anything you might want to do, which you get to leverage with this approach.

The libraries are also c based so you will need some interface (e.g. JNI) to them anyway.

An example of a Java wrapper around ffmpeg is:

There are many online examples of using the ffmpeg tool to add a watermarking to a video - one example is here but it is worth googling to find the latest ad the syntax does change over time:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值