通过监听相机预览流的 imageArrival
事件,获取预览帧:
https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V5/js-apis-image-V5#on9
获取预览帧后,对每一帧图片添加水印,图片添加水印案例,
https://developer.huawei.com/consumer/cn/forum/topic/0207151268119912225?fid=0109140870620153026
将添加水印的后的图片 buffer,喂给编码器合成视频,
https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V5/video-encoding-V5
以上做法的问题:仅能处理视频流,音频流需要使用 OHAudio Kit 录制
https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V5/using-audiocapturer-for-recording-V5
添加水印可以使用三方 ffmpeg 命令实现
videoPath
:沙箱视频源
waterPath
:沙箱中的水印图片(也可以自行通过 Canvas 绘制)
outMP4
:输出视频
依赖三方库,直接调用 ffmepg 命令,但三方库不支持 drawtext
等直接绘制水印接口,若需要可自行编译或找三四方库同事咨询
@ohos/mp4parser: 2.0.3-rc.0
public static addWaterMark(videoPath: string) {
let basePath = GlobalContext.getContext().cacheDir;
let sourceMP4 = basePath + "/" + videoPath;
let outMP4 = basePath + "/out.mp4";
fileIo.openSync(outMP4, fileIo.OpenMode.CREATE | fileIo.OpenMode.READ_WRITE);
let callBack: ICallBack = {
callBackResult(code: number) {
console.info("error " + code);
}
}
let waterPath = basePath + "/result.png"
// ffmpeg -hide_banner -i /data/storage/el2/base/haps/entry/cache/video1.mp4 -i /data/storage/el2/base/haps/entry/cache/water.jpg -filter_complex overlay=x=0:y=0 /data/storage/el2/base/haps/entry/cache/out.mp4 -y
let cmd = 'ffmpeg -hide_banner -i ' + sourceMP4 + " -i " + waterPath + " -filter_complex overlay=x=0:y=0 " + outMP4+ " -y"
MP4Parser.ffmpegCmd(cmd, callBack);
}