/*add watermark into move by operate pic Data. audio has been ignored
*缪国凯 MK
*821486004@qq.com
*/
#include "Common.h"
#include "ConvertImg.h"
#include "MixYUV.h"
#define ERRLOG printf
#define ERR_OK 0 //成功
#define ERR_OPEN_INPUT -1 //打开输入错误
#define ERR_NO_VIDEO_INPUT -2 //打开视频输入错误
#define ERR_NO_AUDIO_INPUT -3 //打开音频输入错误
#define ERR_OPEN_VIDEO_OUTPUT -4 //打开视频输出错误
#define ERR_OPEN_FILE_HANLDE -5 //打开输出文件错误
#define ERR_WRITE_HEAD -6 //输出文件写头错误
#define ERR_OCTX_ALLOC -7 //无法初始化输出格式错误
#define ERR_MARK_NOPIC -8 //水印图片错误
#define ERR_ADD_SUBTITLE -9 //添加字幕出错
DWORD WINAPI DecodeWork(LPVOID lpParam );
void CodecWork();
int flush_video_encoder(AVFormatContext *fmt_ctx,unsigned int stream_index);
void free_output_fmtCtx(AVFormatContext **fmtCtx);
int NewVideoStream(AVFormatContext *pOutContext, AVCodecContext *pVideoCodecContext, int &outVedioStramIndex, int w, int h, AVCodecID codecID, float frameRate, AVStream* inPutStream)
{
int ret = 0;
if (pVideoCodecContext && (!(w && h && 0 != codecID)))
{
outVedioStramIndex = pOutContext->nb_streams;
AVStream* out_video_stream = avformat_new_stream(pOutContext, NULL);
if (!out_video_stream)
{
ERRLOG("Fail to allocating output video stream!");
return -1;
}
if ((ret = avcodec_copy_context(out_video_stream->codec, pVideoCodecContext)) < 0)
{
ERRLOG("can not copy the video codec context! ret=%d", ret);
return ret;
}
if (inPutStream)
{
AVDictionaryEntry *tag = NULL;
tag = av_dict_get(inPutStream->metadata, "rotate", tag, 0);
if (tag != NULL)
{
av_dict_set(&out_video_stream->metadata, "rotate", tag->value, 0);
}
}
AVRational time_base={1, frameRate};
out_video_stream->time_base = time_base;
out_video_stream->codec->codec_tag = 0;
if(pOutContext->oformat->flags & AVFMT_GLOBALHEADER)
{
out_video_stream->codec->flags |= CODEC_FLAG_GLOBAL_HEADER;
}
}
else if (w && h && 0 != codecID)
{
//to be add byMK
AVCodecContext *videoCodecCtx;
outVedioStramIndex = pOutContext->nb_streams;
AVStream* pVideoStream = avformat_new_stream(pOutContext, NULL);
if (!pVideoStream)
{
ERRLOG("Fail to allocating output video stream!");
return -1;
}
AVDictionaryEntry *tag = NULL;
if (inPutStream)
{
tag = av_dict_get(inPutStream->metadata, "rotate", tag, 0);
if (tag != NULL)
{
//av_dict_set(&pVideoStream->metadata, "rotate", tag->value, 0);
int tmpRotate = atoi(tag->value);
if (tmpRotate == 90 || tmpRotate == 270)
{
w = w + h;
h = w - h;
w = w - h;
}
}
}
//set codec context param
pVideoStream->codec->codec = avcodec_find_encoder(codecID);
pVideoStream->codec->height = h;
pVideoStream->codec->width = w;
pVideoStream->codec->codec_id = codecID;
pVideoStream->codec->codec_type = AVMEDIA_TYPE_VIDEO;
pVideoStream->codec->qmin = 30;
pVideoStream->codec->qmax = 51;
AVRational time_base={1, frameRate};
pVideoStream->codec->time_base = time_base;
pVideoStream->time_base = time_base;
// take first format from list of supported formats
AVRational sample_aspect_ratio={0, 1};
pVideoStream->codec->sample_aspect_ratio = sample_aspect_ratio;
pVideoStream->codec->pix_fmt = AV_PIX_FMT_YUV420P;//pOutContext->streams[outVedioStramIndex]->codec->codec->pix_fmts[0];
//pVideoStream->codec->bit_rate = (w * h);
pVideoStream->codec->gop_size = frameRate;
pVideoStream->metadata;
//open encoder
if (!pVideoStream->codec->codec)
{
ERRLOG("fail to find the encoder for video stream codedID:%d", codecID);
return -1;
}
if (pOutContext->oformat->flags & AVFMT_GLOBALHEADER)
pVideoStream->codec->flags |= CODEC_FLAG_GLOBAL_HEADER;
AVDictionary *param = NULL;
if (pVideoStream->codec->codec_id == AV_CODEC_ID_H264)
{
av_dict_set(¶m, "preset", "slow", 0);
av_dict_set(¶m, "tune", "film", 0);
}
if ((ret = avcodec_open2(pVideoStream->codec, pVideoStream->codec->codec, ¶m)) < 0)
{
ERRLOG("fail to open the encoder for video stream, codecID:%d ret:%d", codecID, ret);
return ret;
}
ret = 0; //将ret的值重新置为0
}
else
{
return -1;
}
return ret;
}
int OpenInput(const char * file_name, AVFormatContext **in_fmtctx, int &videoIndex, int &audioIndex)
{
int ret = -1;
if ((ret = avformat_open_input(in_fmtctx, file_name, NULL, NULL)) < 0)
{
ERRLOG("can not open the input file:%s!", file_name);
return ret;
}
if ((ret = avformat_find_stream_info((*in_fmtctx), NULL)) < 0)
{
ERRLOG("can not find the input stream for file:%s!", file_name);
avformat_close_input(in_fmtctx);
return ret;
}
AVFormatContext *pTempInputCtx = (*in_fmtctx);
for (int i = 0; i < pTempInputCtx->nb_streams; i++)
{
if (pTempInputCtx->streams[i]->codec->codec_type == AVMEDIA_TYPE_VIDEO)
{
if (-1 == videoIndex)
{
videoIndex = i;
}
ret = avcodec_open2(pTempInputCtx->streams[i]->codec, avcodec_find_decoder(pTempInputCtx->streams[i]->codec->codec_id), NULL);
if(0 > ret)
{
ERRLOG("can not find or open video decoder for file:%s!", file_name);
avformat_close_input(in_fmtctx);
return ret;
}
}
else if (pTempInputCtx->streams[i]->codec->codec_type == AVMEDIA_TYPE_AUDIO)
{
if (-1 == audioIndex)
{
audioIndex = i;
}
ret = avcodec_open2(pTempInputCtx->streams[i]->codec, avcodec_find_decoder(pTempInputCtx->streams[i]->codec->codec_id), NULL);
if(0 > ret)
{
ERRLOG("can not find or open audio decoder for file:%s!", file_name);
avformat_close_input(in_fmtctx);
return ret;
}
}
}
return 0;
}
const char *pVideoFileName = "SourceVideo2.mp4";
const char* pOutVideoFileName = "TestAddWaterMark.mp4";
const char *pMarkPicName = "mark.png";
AVFormatContext *pInputContext = NULL;
int inputVideoIndex = -1;
int inputAudioIndex = -1;
//output
AVFormatContext *pOutContext = NULL;
int outPutVideoIndex = -1;
bool bIsFileEnd = false;
bool bWork = true;
int main()
{
av_register_all();
if ((NULL == pVideoFileName || NULL == pOutVideoFileName))
{
return -1;
}
int ret_code = 0;
int m_x = 0;
int m_y = 0;
int srcW = 0;
int srcH = 0;
OpenInput(pVideoFileName, &pInputContext, inputVideoIndex, inputAudioIndex);
if (-1 == inputVideoIndex)
{
printf("open input error\n");
return ERR_OPEN_INPUT;
}
srcW = pInputContext->streams[inputVideoIndex]->codec->width;
srcH = pInputContext->streams[inputVideoIndex]->codec->height;
int markW = 0;
int markH = 0;
//read mark pic
AVFrame *pMarkFrame = NULL;
{
AVFormatContext *pTmpContext = NULL;
int tmpVideoIndex = -1;
int tmpAudioIndex = -1;
OpenInput(pMarkPicName, &pTmpContext, tmpVideoIndex, tmpAudioIndex);
if (-1 == tmpVideoIndex)
{
printf("open input error\n");
return ERR_OPEN_INPUT;
}
markW = pTmpContext->streams[tmpVideoIndex]->codec->width;
markH = pTmpContext->streams[tmpVideoIndex]->codec->height;
while(true)
{
AVPacket* packet = new AVPacket;
av_init_packet(packet);
packet->data = NULL;
packet->size = 0;
if (av_read_frame(pTmpContext, packet) < 0)
{
break;
}
//video stream
int getFrame = 0;
if (packet->stream_index == tmpVideoIndex)
{
AVFrame* pFrameBuf = av_frame_alloc();
if (avcodec_decode_video2(pTmpContext->streams[tmpVideoIndex]->codec, pFrameBuf, &getFrame, packet) < 0)
{
av_packet_unref(packet);
delete packet;
continue;
}
if (!getFrame)
{
AVPacket tmpPacke;
av_init_pac
没有合适的资源?快使用搜索试试~ 我知道了~
ffmpeg不用filter添加水印
共190个文件
h:113个
tlog:23个
def:8个
2星 需积分: 19 25 下载量 25 浏览量
2018-06-26
15:45:45
上传
评论
收藏 32.37MB RAR 举报
温馨提示
ffmpeg不用filter添加水印,手动处理AVFrame内存数据,达到添加水印的目的
资源推荐
资源详情
资源评论
收起资源包目录
ffmpeg不用filter添加水印 (190个子文件)
libavdevice.dll.a 392KB
libavutil.dll.a 308KB
libavcodec.dll.a 160KB
libavformat.dll.a 141KB
libavfilter.dll.a 42KB
libswscale.dll.a 22KB
libswresample.dll.a 15KB
libpostproc.dll.a 7KB
AddWatermark.vcxprojResolveAssemblyReference.cache 713B
AddWaterMark.cpp 17KB
ConvertImg.cpp 12KB
MixYUV.cpp 4KB
avdevice-57.def 14KB
avutil-55.def 11KB
avcodec-57.def 6KB
avformat-57.def 6KB
avfilter-6.def 2KB
swscale-4.def 756B
swresample-2.def 478B
postproc-54.def 233B
avcodec-57.dll 20.74MB
avfilter-6.dll 5.82MB
avformat-57.dll 2.68MB
avdevice-57.dll 1.15MB
avutil-55.dll 635KB
swscale-4.dll 494KB
swresample-2.dll 315KB
postproc-54.dll 114KB
addwatermark.obj.enc 55KB
AddWatermark.vcxproj.filters 1KB
avcodec.h 206KB
avformat.h 112KB
avfilter.h 40KB
opt.h 35KB
pixfmt.h 31KB
avio.h 29KB
frame.h 24KB
mem.h 23KB
swresample.h 21KB
intreadwrite.h 18KB
avdevice.h 18KB
hwcontext.h 16KB
common.h 15KB
pixdesc.h 14KB
avstring.h 14KB
swscale.h 12KB
log.h 11KB
buffer.h 10KB
samplefmt.h 10KB
channel_layout.h 9KB
avutil.h 9KB
hash.h 8KB
imgutils.h 8KB
dict.h 8KB
_mingw.h 8KB
mathematics.h 8KB
vdpau.h 8KB
version.h 8KB
bprint.h 8KB
parseutils.h 7KB
buffersrc.h 6KB
xvmc.h 6KB
vda.h 6KB
stdint.h 6KB
audio_fifo.h 6KB
fifo.h 6KB
rational.h 6KB
inttypes.h 6KB
error.h 5KB
tree.h 5KB
timecode.h 5KB
buffersink.h 5KB
eval.h 5KB
cpu.h 5KB
version.h 5KB
attributes.h 4KB
vaapi.h 4KB
stereo3d.h 4KB
dirac.h 4KB
videotoolbox.h 4KB
qsv.h 4KB
dv_profile.h 4KB
threadmessage.h 4KB
murmur3.h 3KB
version.h 3KB
downmix_info.h 3KB
display.h 3KB
crc.h 3KB
avfft.h 3KB
postprocess.h 3KB
hmac.h 3KB
bswap.h 3KB
d3d11va.h 3KB
xtea.h 3KB
mediacodec.h 3KB
mastering_display_metadata.h 3KB
timestamp.h 3KB
version.h 3KB
avdct.h 3KB
file.h 3KB
共 190 条
- 1
- 2
资源评论
- qwe_8202021-06-11资源超时?????
dancing_night
- 粉丝: 370
- 资源: 31
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- 基于Matlab人脸肤色定理的教师人数统计+源代码+全部数据+文档说明+详细注释+使用说明+截图(高分课程设计)
- 基于Matlab霍夫曼变换的表盘读数识别+源代码+全部数据+文档说明+详细注释+使用说明+截图(高分课程设计)
- 基于Matlab火灾烟雾检测源码带GUI界面+源代码+全部数据+文档说明+详细注释+使用说明+截图(高分课程设计)
- 基于Matlab的恶劣天气交通标志识别系统+源代码+全部数据+文档说明+详细注释+使用说明+截图(高分课程设计)
- 基于MATLAB的霍夫曼变换的表盘示数识别+源代码+全部数据+文档说明+详细注释+使用说明+截图(高分课程设计)
- 基于Matlab的车道线识别系统 +源代码+全部数据+文档说明+详细注释+使用说明+截图(高分课程设计)
- 基于MATLAB的教室人数统计系统带Gui界面+源代码+全部数据+文档说明+详细注释+使用说明+截图(高分课程设计)
- 基于MATLAB的教室人数统计系统带Gui界面+源代码+全部数据+文档说明+详细注释+使用说明+截图(高分课程设计)
- 基于MATLAB 的霍夫曼变换答题卡识别源码+全部数据+文档说明+详细注释+使用说明+截图(高分课程设计)
- 基于Matlab+bp神经网络的神经网络汉字识别系统+源代码+全部数据+文档说明+详细注释+使用说明+截图(高分课程设计)
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功