在android中的视频上添加水印位图:4.3's MediaMuxer or ffmpeg [英] Adding watermark bitmap over video in android: 4.3's MediaMuxer or ffmpeg

查看:62
本文介绍了在android中的视频上添加水印位图:4.3's MediaMuxer or ffmpeg的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的场景:

  • 从网上下载 avi 电影
  • 打开位图资源
  • 将此位图覆盖在电影底部的所有背景帧上
  • 将视频保存在外部存储设备上
  • 视频长度通常为 15 秒

这可以使用 MediaMuxer 实现吗?很高兴收到有关此事的任何信息

Is this possible to achieve using MediaMuxer ? Any info on the matter is gladly received

我一直在寻找 http://bigflake.com/mediacodec/#DecodeEditEncodeTest(谢谢@fadden),上面写着:

I've been looking to http://bigflake.com/mediacodec/#DecodeEditEncodeTest (Thanks @fadden) and it says there:

"解码帧并将其复制到 ByteBuffer 中glReadPixels() 在 Nexus 5 上大约需要 8 毫秒,足够快跟上 30fps 输入,但需要额外的步骤来保存将它作为 PNG 格式存储到磁盘上很昂贵(大约半秒)"

"Decoding the frame and copying it into a ByteBuffer with glReadPixels() takes about 8ms on the Nexus 5, easily fast enough to keep pace with 30fps input, but the additional steps required to save it to disk as a PNG are expensive (about half a second)"

因此几乎 1 秒/帧是不可接受的.根据我的想法,一种方法是将每个帧保存为 PNG,打开它,在其上添加位图叠加层,然后保存它.然而,这需要大量时间才能完成.

So having almost 1 sec/frame is not acceptable. From what I am thinking one way would be to save each frame as PNG, open it, add the bitmap overlay on it and then save it. However this would take an enormous time to accomplish.

我想知道是否有办法做这样的事情:

I wonder if there is a way to do things like this:

  1. 打开外部存储中的视频文件
  2. 开始解码
  3. 每个解码的帧都会随着内存中的位图叠加而改变
  4. 帧被发送到编码器.

在 iOS 上,我看到有一种方法可以将原始音频 + 原始视频 + 图像添加到容器中,然后对整个内容进行编码...

On iOS I saw that there a way to take the original audio + original video + an image and add them in a container and then just encode the whole thing...

我应该切换到 ffmpeg 吗?ffmpeg 的稳定性和兼容性如何?我是否冒着与 android 4.0+ 设备的兼容性问题的风险?有没有办法使用 ffmpeg 来完成这个?我是这个领域的新手,还在做研究.

Should I switch to ffmpeg ? How stable and compatible is ffmpeg ? Am I risking compatibility issues with android 4.0+ devices ? Is there a way to use ffmpeg to acomplish this ? I am new to this domain and still doing research.

多年后
自从提出问题以来已经过去了很多年,并且 ffmpeg 在许可证方面并不是很容易添加到商业软件中.这是如何演变的?使用默认 sdk 的较新版本的 android 更能做到这一点?

Years later edit:
Years have passed since the question and ffmpeg isn't really easy to add to a commercial software in terms of license. How did this evolved? Newer versions of android are more capable on this with the default sdk?

稍后再编辑

我因为发布信息作为答案得到了一些反对票,所以我将编辑原始问题.这是一个很棒的库,从我的测试来看,它确实将水印应用于视频,并通过进度回调来实现,这使得向用户显示进度变得更加容易,并且还使用了默认的 android sdks.https://github.com/MasayukiSuda/Mp4Composer-android

I got some negative votes for posting info as an answer so I'll edit the original question. Here is a great library which, from my testing does apply watermark to video and does it with progress callback making it a lot easier to show progress to the user and also uses the default android sdks. https://github.com/MasayukiSuda/Mp4Composer-android

此库使用 Android MediaCodec API 生成 Mp4 电影并应用过滤器、缩放和旋转 Mp4.

This library generate an Mp4 movie using Android MediaCodec API and apply filter, scale, and rotate Mp4.

示例代码,可能如下所示:

Sample code, could look like:

new mp4Composer(sourcePath, destinationPath)
        .filter(new GlWatermarkFilter(watermarkBitmap)
        .listener(){
              @Override 
              private void onProgress(double value){}

              @Override 
              private void onCompleted(double value){
                  runOnUiThread( () ->{
                     showSneakbar
                  }
              }

              @Override 
              private void onCancelled(double value){}

              @Override 
              private void onFailed(Exception e){}

        }).start();

在模拟器上测试,似乎可以在 android 8+ 上正常工作,而在较旧的版本上会生成黑色视频文件.但是,在真实设备上测试时似乎可以正常工作.

Testing on emulator, seems to work fine on android 8+ while on older generates a black video file.However, when testing on real device seems to work.

推荐答案

我对 MediaMuxer 了解不多,但 ffmpeg 确实支持叠加功能.FFMPEG 有各种过滤器,其中之一是叠加过滤器.我的理解是你想在视频上叠加一个图像(即 png),ffmpeg 肯定是一个有用的框架来完成这项工作.您可以设置输出格式,您可以设置要重放的图像的坐标.

I don't know much about the MediaMuxer but ffmpeg does support overlaying functionality. FFMPEG has various filters one of them is overlay filter. What I understand is you want to overlay an image (i.e. png) on the video, ffmpeg surely is a useful framework to do this job. You can set the output format you can set the co-ordinates of the image which is to be overplayed.

例如

ffmpeg -i input.avi -i logo.png -filter_complex 'overlay=10:main_h-overlay_h-10' output.avi

以上命令在左下角的 input.avi 视频文件上添加覆盖 logo.png.

Above command adds overlays logo.png on the input.avi video file in bottom left corner.

有关过滤器的更多信息,请访问以下网站,

More information about the filters is available at following website,

https://www.ffmpeg.org/ffmpeg-filters.html#overlay-1

如果这是您问题的解决方案,您需要与上述命令等效的 C 代码.你还需要看看ffmpeg的性能,因为它是一个纯软件框架.

If this is a solution to your problem you need the C code equivalent to the above command. You also need to see the performance of the ffmpeg because it a pure software framework.

希望我正确理解了您的问题,这会有所帮助.

Hope I have understood your question correctly and this helps.

这篇关于在android中的视频上添加水印位图:4.3's MediaMuxer or ffmpeg的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆