之前本人写过ffmpeg movie滤镜添加图片水印,ffmpeg 非movie滤镜添加图片水印
今天用delogo滤镜去掉图片水印,ffmpeg命令行如下:

ffmpeg -i in-computer_drawmovie.mp4 -vf delogo=x=100:y=300:w=480:h=320:show=0 in-computer_drawmovie_delogo.mp4

指定去除的起始位置为100,300,水印的宽度和高度分别为480和320。

水印去除前,效果如下:

去除水印后,效果如下:

去除的原理是根据被去掉部分周围的色彩进行颜色构造,具体我暂时没兴趣研究。

下面是代码结构:

FfmpegDelogoTest.cpp的代码如下:

#include <iostream>
#include "Delogo.h"
#include <vector>#ifdef   __cplusplus
extern "C"
{#endif#pragma comment(lib, "avcodec.lib")
#pragma comment(lib, "avformat.lib")
#pragma comment(lib, "avutil.lib")
#pragma comment(lib, "avdevice.lib")
#pragma comment(lib, "avfilter.lib")
#pragma comment(lib, "postproc.lib")
#pragma comment(lib, "swresample.lib")
#pragma comment(lib, "swscale.lib")#ifdef __cplusplus
};
#endifstd::string Unicode_to_Utf8(const std::string & str)
{int nwLen = ::MultiByteToWideChar(CP_ACP, 0, str.c_str(), -1, NULL, 0);wchar_t * pwBuf = new wchar_t[nwLen + 1];//一定要加1,不然会出现尾巴 ZeroMemory(pwBuf, nwLen * 2 + 2);::MultiByteToWideChar(CP_ACP, 0, str.c_str(), str.length(), pwBuf, nwLen);int nLen = ::WideCharToMultiByte(CP_UTF8, 0, pwBuf, -1, NULL, NULL, NULL, NULL);char * pBuf = new char[nLen + 1];ZeroMemory(pBuf, nLen + 1);::WideCharToMultiByte(CP_UTF8, 0, pwBuf, nwLen, pBuf, nLen, NULL, NULL);std::string retStr(pBuf);delete[]pwBuf;delete[]pBuf;pwBuf = NULL;pBuf = NULL;return retStr;
}int main()
{CDelogo cCDelogo;const char *pFileA = "E:\\learn\\ffmpeg\\FfmpegFilterTest\\x64\\Release\\in-computer_drawmovie.mp4";const char *pFileOut = "E:\\learn\\ffmpeg\\FfmpegFilterTest\\x64\\Release\\in-computer_drawmovie_delogo.mp4";int x = 100;int y = 300;int width = 480;int height = 320;cCDelogo.StartDelogo(pFileA, pFileOut, x, y, width, height);cCDelogo.WaitFinish();return 0;
}

Delogo.h的代码如下:

#pragma once#include <Windows.h>
#include <string>#ifdef   __cplusplus
extern "C"
{#endif
#include "libavcodec/avcodec.h"
#include "libavformat/avformat.h"
#include "libswscale/swscale.h"
#include "libswresample/swresample.h"
#include "libavdevice/avdevice.h"
#include "libavutil/audio_fifo.h"
#include "libavutil/avutil.h"
#include "libavutil/fifo.h"
#include "libavutil/frame.h"
#include "libavutil/imgutils.h"#include "libavfilter/avfilter.h"
#include "libavfilter/buffersink.h"
#include "libavfilter/buffersrc.h"#ifdef __cplusplus
};
#endifclass CDelogo
{public:CDelogo();~CDelogo();
public:int StartDelogo(const char *pFileA, const char *pFileOut, int x, int y, int width, int height);int WaitFinish();
private:int OpenFileA(const char *pFileA);int OpenOutPut(const char *pFileOut);int InitFilter(const char* filter_desc);
private:static DWORD WINAPI VideoAReadProc(LPVOID lpParam);void VideoARead();static DWORD WINAPI VideoDelogoProc(LPVOID lpParam);void VideoDelogo();
private:AVFormatContext *m_pFormatCtx_FileA = NULL;AVCodecContext *m_pReadCodecCtx_VideoA = NULL;AVCodec *m_pReadCodec_VideoA = NULL;AVCodecContext  *m_pCodecEncodeCtx_Video = NULL;AVFormatContext *m_pFormatCtx_Out = NULL;AVFifoBuffer *m_pVideoAFifo = NULL;int m_iVideoWidth = 1920;int m_iVideoHeight = 1080;int m_iYuv420FrameSize = 0;
private:AVFilterGraph* m_pFilterGraph = NULL;AVFilterContext* m_pFilterCtxSrcVideoA = NULL;AVFilterContext* m_pFilterCtxSink = NULL;
private:CRITICAL_SECTION m_csVideoASection;HANDLE m_hVideoAReadThread = NULL;HANDLE m_hVideoDelogoThread = NULL;
};

Delogo.cpp的代码如下:


#include "Delogo.h"
//#include "log/log.h"CDelogo::CDelogo()
{InitializeCriticalSection(&m_csVideoASection);
}CDelogo::~CDelogo()
{DeleteCriticalSection(&m_csVideoASection);
}int CDelogo::StartDelogo(const char *pFileA, const char *pFileOut, int x, int y, int width, int height)
{int ret = -1;do{ret = OpenFileA(pFileA);if (ret != 0){break;}ret = OpenOutPut(pFileOut);if (ret != 0){break;}char szFilterDesc[512] = { 0 };_snprintf(szFilterDesc, sizeof(szFilterDesc),"[in0]delogo=x=%d:y=%d:w=%d:h=%d:show=0[out]", x, y, width, height);ret = InitFilter(szFilterDesc);if (ret != 0){break;}m_iYuv420FrameSize = av_image_get_buffer_size(AV_PIX_FMT_YUV420P, m_pReadCodecCtx_VideoA->width, m_pReadCodecCtx_VideoA->height, 1);//申请30帧缓存m_pVideoAFifo = av_fifo_alloc(30 * m_iYuv420FrameSize);m_hVideoAReadThread = CreateThread(NULL, 0, VideoAReadProc, this, 0, NULL);m_hVideoDelogoThread = CreateThread(NULL, 0, VideoDelogoProc, this, 0, NULL);} while (0);return ret;
}int CDelogo::WaitFinish()
{int ret = 0;do{if (NULL == m_hVideoAReadThread){break;}WaitForSingleObject(m_hVideoAReadThread, INFINITE);CloseHandle(m_hVideoAReadThread);m_hVideoAReadThread = NULL;WaitForSingleObject(m_hVideoDelogoThread, INFINITE);CloseHandle(m_hVideoDelogoThread);m_hVideoDelogoThread = NULL;} while (0);return ret;
}int CDelogo::OpenFileA(const char *pFileA)
{int ret = -1;do{if ((ret = avformat_open_input(&m_pFormatCtx_FileA, pFileA, 0, 0)) < 0) {printf("Could not open input file.");break;}if ((ret = avformat_find_stream_info(m_pFormatCtx_FileA, 0)) < 0) {printf("Failed to retrieve input stream information");break;}if (m_pFormatCtx_FileA->streams[0]->codecpar->codec_type != AVMEDIA_TYPE_VIDEO){break;}m_pReadCodec_VideoA = (AVCodec *)avcodec_find_decoder(m_pFormatCtx_FileA->streams[0]->codecpar->codec_id);m_pReadCodecCtx_VideoA = avcodec_alloc_context3(m_pReadCodec_VideoA);if (m_pReadCodecCtx_VideoA == NULL){break;}avcodec_parameters_to_context(m_pReadCodecCtx_VideoA, m_pFormatCtx_FileA->streams[0]->codecpar);m_iVideoWidth = m_pReadCodecCtx_VideoA->width;m_iVideoHeight = m_pReadCodecCtx_VideoA->height;m_pReadCodecCtx_VideoA->framerate = m_pFormatCtx_FileA->streams[0]->r_frame_rate;if (avcodec_open2(m_pReadCodecCtx_VideoA, m_pReadCodec_VideoA, NULL) < 0){break;}ret = 0;} while (0);return ret;
}int CDelogo::OpenOutPut(const char *pFileOut)
{int iRet = -1;AVStream *pAudioStream = NULL;AVStream *pVideoStream = NULL;do{avformat_alloc_output_context2(&m_pFormatCtx_Out, NULL, NULL, pFileOut);{AVCodec* pCodecEncode_Video = (AVCodec *)avcodec_find_encoder(m_pFormatCtx_Out->oformat->video_codec);m_pCodecEncodeCtx_Video = avcodec_alloc_context3(pCodecEncode_Video);if (!m_pCodecEncodeCtx_Video){break;}pVideoStream = avformat_new_stream(m_pFormatCtx_Out, pCodecEncode_Video);if (!pVideoStream){break;}int frameRate = 10;m_pCodecEncodeCtx_Video->flags |= AV_CODEC_FLAG_QSCALE;m_pCodecEncodeCtx_Video->bit_rate = 4000000;m_pCodecEncodeCtx_Video->rc_min_rate = 4000000;m_pCodecEncodeCtx_Video->rc_max_rate = 4000000;m_pCodecEncodeCtx_Video->bit_rate_tolerance = 4000000;m_pCodecEncodeCtx_Video->time_base.den = frameRate;m_pCodecEncodeCtx_Video->time_base.num = 1;m_pCodecEncodeCtx_Video->width = m_iVideoWidth;m_pCodecEncodeCtx_Video->height = m_iVideoHeight;//pH264Encoder->pCodecCtx->frame_number = 1;m_pCodecEncodeCtx_Video->gop_size = 12;m_pCodecEncodeCtx_Video->max_b_frames = 0;m_pCodecEncodeCtx_Video->thread_count = 4;m_pCodecEncodeCtx_Video->pix_fmt = AV_PIX_FMT_YUV420P;m_pCodecEncodeCtx_Video->codec_id = AV_CODEC_ID_H264;m_pCodecEncodeCtx_Video->codec_type = AVMEDIA_TYPE_VIDEO;av_opt_set(m_pCodecEncodeCtx_Video->priv_data, "b-pyramid", "none", 0);av_opt_set(m_pCodecEncodeCtx_Video->priv_data, "preset", "superfast", 0);av_opt_set(m_pCodecEncodeCtx_Video->priv_data, "tune", "zerolatency", 0);if (m_pFormatCtx_Out->oformat->flags & AVFMT_GLOBALHEADER)m_pCodecEncodeCtx_Video->flags |= AV_CODEC_FLAG_GLOBAL_HEADER;if (avcodec_open2(m_pCodecEncodeCtx_Video, pCodecEncode_Video, 0) < 0){//编码器打开失败,退出程序break;}}if (!(m_pFormatCtx_Out->oformat->flags & AVFMT_NOFILE)){if (avio_open(&m_pFormatCtx_Out->pb, pFileOut, AVIO_FLAG_WRITE) < 0){break;}}avcodec_parameters_from_context(pVideoStream->codecpar, m_pCodecEncodeCtx_Video);if (avformat_write_header(m_pFormatCtx_Out, NULL) < 0){break;}iRet = 0;} while (0);if (iRet != 0){if (m_pCodecEncodeCtx_Video != NULL){avcodec_free_context(&m_pCodecEncodeCtx_Video);m_pCodecEncodeCtx_Video = NULL;}if (m_pFormatCtx_Out != NULL){avformat_free_context(m_pFormatCtx_Out);m_pFormatCtx_Out = NULL;}}return iRet;
}DWORD WINAPI CDelogo::VideoAReadProc(LPVOID lpParam)
{CDelogo *pVideoMerge = (CDelogo *)lpParam;if (pVideoMerge != NULL){pVideoMerge->VideoARead();}return 0;
}void CDelogo::VideoARead()
{AVFrame *pFrame;pFrame = av_frame_alloc();int y_size = m_pReadCodecCtx_VideoA->width * m_pReadCodecCtx_VideoA->height;char *pY = new char[y_size];char *pU = new char[y_size / 4];char *pV = new char[y_size / 4];AVPacket packet = { 0 };int ret = 0;while (1){av_packet_unref(&packet);ret = av_read_frame(m_pFormatCtx_FileA, &packet);if (ret == AVERROR(EAGAIN)){continue;}else if (ret == AVERROR_EOF){break;}else if (ret < 0){break;}ret = avcodec_send_packet(m_pReadCodecCtx_VideoA, &packet);if (ret >= 0){ret = avcodec_receive_frame(m_pReadCodecCtx_VideoA, pFrame);if (ret == AVERROR(EAGAIN)){continue;}else if (ret == AVERROR_EOF){break;}else if (ret < 0) {break;}while (1){if (av_fifo_space(m_pVideoAFifo) >= m_iYuv420FrameSize){///Yint contY = 0;for (int i = 0; i < pFrame->height; i++){memcpy(pY + contY, pFrame->data[0] + i * pFrame->linesize[0], pFrame->width);contY += pFrame->width;}///Uint contU = 0;for (int i = 0; i < pFrame->height / 2; i++){memcpy(pU + contU, pFrame->data[1] + i * pFrame->linesize[1], pFrame->width / 2);contU += pFrame->width / 2;}///Vint contV = 0;for (int i = 0; i < pFrame->height / 2; i++){memcpy(pV + contV, pFrame->data[2] + i * pFrame->linesize[2], pFrame->width / 2);contV += pFrame->width / 2;}EnterCriticalSection(&m_csVideoASection);av_fifo_generic_write(m_pVideoAFifo, pY, y_size, NULL);av_fifo_generic_write(m_pVideoAFifo, pU, y_size / 4, NULL);av_fifo_generic_write(m_pVideoAFifo, pV, y_size / 4, NULL);LeaveCriticalSection(&m_csVideoASection);break;}else{Sleep(100);}}}if (ret == AVERROR(EAGAIN)){continue;}}av_frame_free(&pFrame);delete[] pY;delete[] pU;delete[] pV;
}DWORD WINAPI CDelogo::VideoDelogoProc(LPVOID lpParam)
{CDelogo *pVideoMerge = (CDelogo *)lpParam;if (pVideoMerge != NULL){pVideoMerge->VideoDelogo();}return 0;
}void CDelogo::VideoDelogo()
{int ret = 0;DWORD dwBeginTime = ::GetTickCount();AVFrame *pFrameVideoA = av_frame_alloc();uint8_t *videoA_buffer_yuv420 = (uint8_t *)av_malloc(m_iYuv420FrameSize);av_image_fill_arrays(pFrameVideoA->data, pFrameVideoA->linesize, videoA_buffer_yuv420, AV_PIX_FMT_YUV420P, m_pReadCodecCtx_VideoA->width, m_pReadCodecCtx_VideoA->height, 1);pFrameVideoA->width = m_iVideoWidth;pFrameVideoA->height = m_iVideoHeight;pFrameVideoA->format = AV_PIX_FMT_YUV420P;AVFrame* pFrame_out = av_frame_alloc();uint8_t *out_buffer_yuv420 = (uint8_t *)av_malloc(m_iYuv420FrameSize);av_image_fill_arrays(pFrame_out->data, pFrame_out->linesize, out_buffer_yuv420, AV_PIX_FMT_YUV420P, m_iVideoWidth, m_iVideoHeight, 1);AVPacket packet = { 0 };int iPicCount = 0;while (1){if (NULL == m_pVideoAFifo){break;}int iVideoASize = av_fifo_size(m_pVideoAFifo);if (iVideoASize >= m_iYuv420FrameSize){EnterCriticalSection(&m_csVideoASection);av_fifo_generic_read(m_pVideoAFifo, videoA_buffer_yuv420, m_iYuv420FrameSize, NULL);LeaveCriticalSection(&m_csVideoASection);pFrameVideoA->pkt_dts = pFrameVideoA->pts = av_rescale_q_rnd(iPicCount, m_pCodecEncodeCtx_Video->time_base, m_pFormatCtx_Out->streams[0]->time_base, (AVRounding)(AV_ROUND_NEAR_INF | AV_ROUND_PASS_MINMAX));pFrameVideoA->pkt_duration = 0;pFrameVideoA->pkt_pos = -1;ret = av_buffersrc_add_frame(m_pFilterCtxSrcVideoA, pFrameVideoA);if (ret < 0){break;}ret = av_buffersink_get_frame(m_pFilterCtxSink, pFrame_out);if (ret < 0){//printf("Mixer: failed to call av_buffersink_get_frame_flags\n");break;}pFrame_out->pkt_dts = pFrame_out->pts = av_rescale_q_rnd(iPicCount, m_pCodecEncodeCtx_Video->time_base, m_pFormatCtx_Out->streams[0]->time_base, (AVRounding)(AV_ROUND_NEAR_INF | AV_ROUND_PASS_MINMAX));pFrame_out->pkt_duration = 0;pFrame_out->pkt_pos = -1;pFrame_out->width = m_iVideoWidth;pFrame_out->height = m_iVideoHeight;pFrame_out->format = AV_PIX_FMT_YUV420P;ret = avcodec_send_frame(m_pCodecEncodeCtx_Video, pFrame_out);ret = avcodec_receive_packet(m_pCodecEncodeCtx_Video, &packet);av_write_frame(m_pFormatCtx_Out, &packet);iPicCount++;}else{if (m_hVideoAReadThread == NULL){break;}Sleep(1);}}av_write_trailer(m_pFormatCtx_Out);avio_close(m_pFormatCtx_Out->pb);av_frame_free(&pFrameVideoA);
}int CDelogo::InitFilter(const char* filter_desc)
{int ret = 0;char args_videoA[512];const char* pad_name_videoA = "in0";AVFilter* filter_src_videoA = (AVFilter *)avfilter_get_by_name("buffer");AVFilter* filter_sink = (AVFilter *)avfilter_get_by_name("buffersink");AVFilterInOut* filter_output_videoA = avfilter_inout_alloc();AVFilterInOut* filter_input = avfilter_inout_alloc();m_pFilterGraph = avfilter_graph_alloc();AVRational timeBase;timeBase.num = 1;timeBase.den = 10;AVRational timeAspect;timeAspect.num = 0;timeAspect.den = 1;_snprintf(args_videoA, sizeof(args_videoA),"video_size=%dx%d:pix_fmt=%d:time_base=%d/%d:pixel_aspect=%d/%d",m_iVideoWidth, m_iVideoHeight, AV_PIX_FMT_YUV420P,timeBase.num, timeBase.den,timeAspect.num,timeAspect.den);AVFilterInOut* filter_outputs[1];do{ret = avfilter_graph_create_filter(&m_pFilterCtxSrcVideoA, filter_src_videoA, pad_name_videoA, args_videoA, NULL, m_pFilterGraph);if (ret < 0){break;}ret = avfilter_graph_create_filter(&m_pFilterCtxSink, filter_sink, "out", NULL, NULL, m_pFilterGraph);if (ret < 0){break;}ret = av_opt_set_bin(m_pFilterCtxSink, "pix_fmts", (uint8_t*)&m_pCodecEncodeCtx_Video->pix_fmt, sizeof(m_pCodecEncodeCtx_Video->pix_fmt), AV_OPT_SEARCH_CHILDREN);filter_output_videoA->name = av_strdup(pad_name_videoA);filter_output_videoA->filter_ctx = m_pFilterCtxSrcVideoA;filter_output_videoA->pad_idx = 0;filter_output_videoA->next = NULL;filter_input->name = av_strdup("out");filter_input->filter_ctx = m_pFilterCtxSink;filter_input->pad_idx = 0;filter_input->next = NULL;//filter_outputs[0] = filter_output_videoPad;filter_outputs[0] = filter_output_videoA;ret = avfilter_graph_parse_ptr(m_pFilterGraph, filter_desc, &filter_input, filter_outputs, NULL);if (ret < 0){break;}ret = avfilter_graph_config(m_pFilterGraph, NULL);if (ret < 0){break;}ret = 0;} while (0);avfilter_inout_free(&filter_input);av_free(filter_src_videoA);avfilter_inout_free(filter_outputs);char* temp = avfilter_graph_dump(m_pFilterGraph, NULL);return ret;
}

ffmpeg delogo滤镜去除图片水印相关推荐

  1. Java使用ffmpeg去除图片水印

    /*** 去除图片水印*/@Testpublic void deleteLogo(){/*** 对delogo滤镜的说明:- x,y:水印的左上方坐标- w,h: 水印的宽高- show: 可选参数, ...

  2. 快速批量去除图片水印方法大全~~

    原文地址:http://blog.163.com/simonyao_cool/blog/static/16512555720107311858809/ 去水印是不复杂啦,可几千页下来,自己用橡皮抹会抹 ...

  3. 去除图片水印_只需一键,即可快速去除图片水印!如此简单的方法,谁不知谁吃亏...

    现在有很多小伙伴都会到网上找一些好看的图片保存下来,但是很多时候我们找到的图片都是带有水印的,虽然水印很小,但是看起来也是怪怪的.别担心,今天小编将分享几个简单又好用的图片去水印方法给大家,有需要的小 ...

  4. 如何在线无痕去除图片水印

    教你如何在线无痕去图片水印 网址:https://shuiyinyun.com/inpaint-image.htmlhttp://如何在线无痕去除图片水印 软件截图:

  5. 如何去除图片水印不留痕迹_去水印工具

    在自媒体互联网越加火热的现代,只要你上网,都难免不和图片打交道,有时候我们想要下载网上的图片,结果发现有水印,无疑是很影响心情,那么如何一键去除图片水印呢,今天为大家带来一个简单快速的去图片水印方法 ...

  6. Python OpenCV去除图片水印

    问题描述 去除百度水印 1.jpg,背景简单 2.jpg,背景复杂 解决方案 安装 pip install opencv-python pip install numpy 方法一.OpenCV提取颜色 ...

  7. 教你一招利用Python快速去除图片水印

    大家好,我是IT界搬运工. 相信大家都有在网上下载好图片但是有水印的烦恼,那么问题就来了:看到心爱的图片想要"占为己有".怎么把图片上的水印去除呢?今天我就来教你一招利用Pytho ...

  8. 如何快速去除图片上的水印?去除图片水印怎么做?

    有些时候我们想要使用某些图片,但是因为有水印我们不得不放弃,导致很多优质的图片使用不到,感到很可惜,那么有没有什么方式可以快速去水印呢? 其实是有的,而且的确是很快速,只需要几步就可以快速完成,让我们 ...

  9. java去除图片水印的解决办法

    原文地址:http://www.oicqzone.com/pc/2017062023844.html [color=red]去除水印: 其实就是把水印的颜色替换为背景色(ARGB) /** * * 分 ...

最新文章

  1. 互联网协议 — TCP — 拥塞控制(网络质量保障)
  2. Unity中如何计算带minimap的贴图资源的大小
  3. Apache URL重写的配置 及其 apache500错误
  4. hibernate自定义校验器使用(字段在in范围之内)
  5. 学习一下spring-cloud-function中官方修复的一个问题
  6. golang断言的使用(Type Assertion)
  7. android studio的 gradle 依赖同步错误解决方法
  8. 深度探索QT窗口系统——几何篇
  9. 机器视觉:系统中影响图像质量的因素有哪些?
  10. 自组织映射网络SOMnet
  11. 微信支付开发:当前URL未注册
  12. PAT 1006 解析
  13. 80c51单片机编程语言是什么,80c51单片机的ram分为几个部分各有什么特点和功能...
  14. J-Flash下载程序
  15. ubuntu1804安装mysql5.7教程
  16. jvisualvm监控java,JVM-jvisualvm运行监控工具使用
  17. python 文件去重样例
  18. 棕榈油复制2008年走势?棕榈油认沽大涨,YP开始抄底2022.3.4
  19. 机器学习笔记之指数族分布——最大熵原理与softmax激活函数的关系
  20. Adroid 触摸屏设备

热门文章

  1. 记录: 问题:/usr/bin/ld: main.o: Relocations in generic ELF (EM: 183)
  2. aws php 上传文件 限制大小_单个文件大小 上传百度云盘 微信发送 有大小限制 怎么破?...
  3. 编译原理(一)编译程序、解释程序、程序设计语言范型
  4. 年已过,新的一年如何过?
  5. 【职业人的自我修炼十项原则】
  6. 【windows虚拟机装openwrt做主路由扩wan扩lan】
  7. 《PHP挖宝》1—再论框架
  8. 计算机u盘设备无法启动不了怎么办,u盘无法启动提示错误代码10该怎么处理
  9. ffmpeg加水印、logo等
  10. R语言多层桑基图_R语言可视化(二十三):桑基图绘制