之前写过一篇博客ffmpeg利用滤镜合并两个视频,一左一右

用的是滤镜字符串解析avfilter_graph_parse_ptr实现滤镜的连接,查看了avfilter_graph_parse_ptr的实现,里面调用了avfilter_link进行滤镜的连接。
今天直接用avfilter_link的方式进行实现。
需要说明的是,针对之前的滤镜配置字符串而言。

[in0]split[main][tmp];[tmp]scale=w=960:h=1080[inn0];[main][inn0]overlay=0:0[x1];[in1]scale=w=960:h=1080[inn1];[x1][inn1]overlay=960:0[out]

split滤镜分裂成main和tmp,其中main对应的pad index为0。tmp对应的pad index为1。
对于[main][inn0]overlay=0:0[x1]这种overlay滤镜而言,main对应的pad index为0,inn0对应的pad index为1.

代码结构如下:

FfmpegMerge2FileBy_avfilter_link.cpp的内容如下:

// FfmpegFilterTest.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//#include <iostream>
#include "VideoMerge.h"#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
};
#endifint main()
{CVideoMerge cVideoMerge;const char *pFileA = "E:\\learn\\ffmpeg\\FfmpegFilterTest\\x64\\Release\\in-vs.mp4";const char *pFileB = "E:\\learn\\ffmpeg\\FfmpegFilterTest\\x64\\Release\\in-e.mp4";const char *pFileOut = "E:\\learn\\ffmpeg\\FfmpegFilterTest\\x64\\Release\\out-merge.mp4";cVideoMerge.StartMerge(pFileA, pFileB, pFileOut);cVideoMerge.WaitFinish();return 0;
}

VideoMerge.h的内容如下:

#pragma once#include <Windows.h>#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 CVideoMerge
{public:CVideoMerge();~CVideoMerge();
public:int StartMerge(const char *pFileA, const char *pFileB, const char *pFileOut);int WaitFinish();
private:int OpenFileA(const char *pFileA);int OpenFileB(const char *pFileB);int OpenOutPut(const char *pFileOut);int InitFilter(const char* filter_desc);
private:static DWORD WINAPI VideoAReadProc(LPVOID lpParam);void VideoARead();static DWORD WINAPI VideoBReadProc(LPVOID lpParam);void VideoBRead();static DWORD WINAPI VideoMergeProc(LPVOID lpParam);void VideoMerge();
private:AVFormatContext *m_pFormatCtx_FileA = NULL;AVFormatContext *m_pFormatCtx_FileB = NULL;AVCodecContext *m_pReadCodecCtx_VideoA = NULL;AVCodec *m_pReadCodec_VideoA = NULL;AVCodecContext *m_pReadCodecCtx_VideoB = NULL;AVCodec *m_pReadCodec_VideoB = NULL;AVCodecContext  *m_pCodecEncodeCtx_Video = NULL;AVFormatContext *m_pFormatCtx_Out = NULL;AVFifoBuffer *m_pVideoAFifo = NULL;AVFifoBuffer *m_pVideoBFifo = NULL;AVFilterGraph* m_pFilterGraph = NULL;AVFilterContext* m_pFilterCtxSrcVideoPad = NULL;AVFilterContext* m_pFilterCtxSrcVideoA = NULL;AVFilterContext* m_pFilterCtxSrcVideoB = NULL;AVFilterContext* m_pFilterCtxSink = NULL;int m_iMergeWidth = 1920;int m_iMergeHeight = 1080;int m_iYuv420FrameSize = 0;
private:CRITICAL_SECTION m_csVideoASection;CRITICAL_SECTION m_csVideoBSection;HANDLE m_hVideoAReadThread = NULL;HANDLE m_hVideoBReadThread = NULL;HANDLE m_hVideoMergeThread = NULL;
};

VideoMerge.cpp的代码如下:


#include "VideoMerge.h"
#include "log/log.h"CVideoMerge::CVideoMerge()
{InitializeCriticalSection(&m_csVideoASection);InitializeCriticalSection(&m_csVideoBSection);
}CVideoMerge::~CVideoMerge()
{DeleteCriticalSection(&m_csVideoASection);DeleteCriticalSection(&m_csVideoBSection);
}int CVideoMerge::StartMerge(const char *pFileA, const char *pFileB, const char *pFileOut)
{avdevice_register_all();int ret = -1;do{ret = OpenFileA(pFileA);if (ret != 0){break;}ret = OpenFileB(pFileB);if (ret != 0){break;}ret = OpenOutPut(pFileOut);if (ret != 0){break;}///这个滤镜的效果是以第一个视频做模板,且第一个视频的铺满全屏,第二个视频覆盖在第一个视频的右半部分//const char* filter_desc = "[in0]pad=1920:1080[x1];[in1]scale=w=960:h=1080[inn1];[x1][inn1]overlay=960:0[out]";const char* filter_desc = "[in0]split[main][tmp];[tmp]scale=w=960:h=1080[inn0];[main][inn0]overlay=0:0[x1];[in1]scale=w=960:h=1080[inn1];[x1][inn1]overlay=960:0[out]";ret = InitFilter(filter_desc);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_pVideoBFifo = av_fifo_alloc(30 * m_iYuv420FrameSize);m_hVideoAReadThread = CreateThread(NULL, 0, VideoAReadProc, this, 0, NULL);m_hVideoBReadThread = CreateThread(NULL, 0, VideoBReadProc, this, 0, NULL);m_hVideoMergeThread = CreateThread(NULL, 0, VideoMergeProc, this, 0, NULL);} while (0);return ret;
}int CVideoMerge::WaitFinish()
{int ret = 0;do{if (NULL == m_hVideoAReadThread || NULL == m_hVideoBReadThread){break;}WaitForSingleObject(m_hVideoAReadThread, INFINITE);WaitForSingleObject(m_hVideoBReadThread, INFINITE);CloseHandle(m_hVideoAReadThread);m_hVideoAReadThread = NULL;CloseHandle(m_hVideoBReadThread);m_hVideoBReadThread = NULL;WaitForSingleObject(m_hVideoMergeThread, INFINITE);CloseHandle(m_hVideoMergeThread);m_hVideoMergeThread = NULL;} while (0);return ret;
}int CVideoMerge::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_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 CVideoMerge::OpenFileB(const char *pFileB)
{int ret = -1;do{if ((ret = avformat_open_input(&m_pFormatCtx_FileB, pFileB, 0, 0)) < 0) {printf("Could not open input file.");break;}if ((ret = avformat_find_stream_info(m_pFormatCtx_FileB, 0)) < 0) {printf("Failed to retrieve input stream information");break;}if (m_pFormatCtx_FileB->streams[0]->codecpar->codec_type != AVMEDIA_TYPE_VIDEO){break;}m_pReadCodec_VideoB = (AVCodec *)avcodec_find_decoder(m_pFormatCtx_FileB->streams[0]->codecpar->codec_id);m_pReadCodecCtx_VideoB = avcodec_alloc_context3(m_pReadCodec_VideoB);if (m_pReadCodecCtx_VideoB == NULL){break;}avcodec_parameters_to_context(m_pReadCodecCtx_VideoB, m_pFormatCtx_FileB->streams[0]->codecpar);m_pReadCodecCtx_VideoB->framerate = m_pFormatCtx_FileB->streams[0]->r_frame_rate;if (avcodec_open2(m_pReadCodecCtx_VideoB, m_pReadCodec_VideoB, NULL) < 0){break;}ret = 0;} while (0);return ret;
}int CVideoMerge::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_iMergeWidth;m_pCodecEncodeCtx_Video->height = m_iMergeHeight;//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;
}//const char* filter_desc = "[in0]split[main][tmp];[tmp]scale=w=960:h=1080[inn0];[main][inn0]overlay=0:0[x1];[in1]scale=w=960:h=1080[inn1];[x1][inn1]overlay=960:0[out]";
int CVideoMerge::InitFilter(const char* filter_desc)
{int ret = 0;char args_pad[512];const char* pad_name_videoPad = "pad0";char args_videoA[512];const char* pad_name_videoA = "in0";char args_videoB[512];const char* pad_name_videoB = "in1";const char* scale_Tmp = "tmp";AVFilter* filter_src_videoPad = (AVFilter *)avfilter_get_by_name("pad");AVFilter* filter_src_videoA = (AVFilter *)avfilter_get_by_name("buffer");AVFilter* filter_src_videoB = (AVFilter *)avfilter_get_by_name("buffer");AVFilter* filter_sink = (AVFilter *)avfilter_get_by_name("buffersink");AVFilter *filter_split = (AVFilter *)avfilter_get_by_name("split");AVFilter *filter_overlayVideoA = (AVFilter *)avfilter_get_by_name("overlay");AVFilter *filter_overlayVideoB = (AVFilter *)avfilter_get_by_name("overlay");AVFilter *filter_scaleTmp = (AVFilter *)avfilter_get_by_name("scale");AVFilter *filter_scaleVideoB = (AVFilter *)avfilter_get_by_name("scale");AVFilterInOut* filter_output_videoPad = avfilter_inout_alloc();AVFilterInOut* filter_output_videoA = avfilter_inout_alloc();AVFilterInOut* filter_output_videoB = 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_pad, sizeof(args_pad), "width=%d:height=%d", 1920, 1080);_snprintf(args_videoA, sizeof(args_videoA),"video_size=%dx%d:pix_fmt=%d:time_base=%d/%d:pixel_aspect=%d/%d",1920, 1080, AV_PIX_FMT_YUV420P,timeBase.num, timeBase.den,timeAspect.num,timeAspect.den);_snprintf(args_videoB, sizeof(args_videoB),"video_size=%dx%d:pix_fmt=%d:time_base=%d/%d:pixel_aspect=%d/%d",1920, 1080, AV_PIX_FMT_YUV420P,timeBase.num, timeBase.den,timeAspect.num,timeAspect.den);AVFilterInOut* filter_outputs[3];do{AVFilterContext *splitFilter_ctx;ret = avfilter_graph_create_filter(&splitFilter_ctx, filter_split, "split", "outputs=2", NULL, m_pFilterGraph);if (ret < 0){break;}AVFilterContext *overlayVideoAFilter_ctx;ret = avfilter_graph_create_filter(&overlayVideoAFilter_ctx, filter_overlayVideoA, "overlayVideoA", "x=0:y=0", NULL, m_pFilterGraph);if (ret < 0) {break;}AVFilterContext *overlayVideoBFilter_ctx;ret = avfilter_graph_create_filter(&overlayVideoBFilter_ctx, filter_overlayVideoB, "overlayVideoB", "x=960:y=0", NULL, m_pFilterGraph);if (ret < 0){break;}AVFilterContext *scaleTmpFilter_ctx;ret = avfilter_graph_create_filter(&scaleTmpFilter_ctx, filter_scaleTmp, "tmp", "w=960:h=1080", NULL, m_pFilterGraph);if (ret < 0){break;}AVFilterContext *scaleVideoBFilter_ctx;ret = avfilter_graph_create_filter(&scaleVideoBFilter_ctx, filter_scaleVideoB, "in1", "w=960:h=1080", NULL, m_pFilterGraph);if (ret < 0){break;}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_pFilterCtxSrcVideoB, filter_src_videoB, pad_name_videoB, args_videoB, 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);//const char* filter_desc = "[in0]split[main][tmp];[tmp]scale=w=960:h=1080[inn0];[main][inn0]overlay=0:0[x1];[in1]scale=w=960:h=1080[inn1];[x1][inn1]overlay=960:0[out]";ret = avfilter_link(m_pFilterCtxSrcVideoA, 0, splitFilter_ctx, 0);if (ret != 0) {break;}ret = avfilter_link(splitFilter_ctx, 1, scaleTmpFilter_ctx, 0);if (ret != 0) {break;}ret = avfilter_link(splitFilter_ctx, 0, overlayVideoAFilter_ctx, 0);if (ret != 0){break;}ret = avfilter_link(scaleTmpFilter_ctx, 0, overlayVideoAFilter_ctx, 1);if (ret != 0){break;}ret = avfilter_link(m_pFilterCtxSrcVideoB, 0, scaleVideoBFilter_ctx, 0);if (ret != 0){break;}ret = avfilter_link(overlayVideoAFilter_ctx, 0, overlayVideoBFilter_ctx, 0);if (ret != 0){break;}ret = avfilter_link(scaleVideoBFilter_ctx, 0, overlayVideoBFilter_ctx, 1);if (ret != 0){break;}ret = avfilter_link(overlayVideoBFilter_ctx, 0, m_pFilterCtxSink, 0);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);av_free(filter_src_videoB);avfilter_inout_free(filter_outputs);char* temp = avfilter_graph_dump(m_pFilterGraph, NULL);return ret;
}DWORD WINAPI CVideoMerge::VideoAReadProc(LPVOID lpParam)
{CVideoMerge *pVideoMerge = (CVideoMerge *)lpParam;if (pVideoMerge != NULL){pVideoMerge->VideoARead();}return 0;
}void CVideoMerge::VideoARead()
{AVFrame *pFrame;pFrame = av_frame_alloc();int y_size = m_pReadCodecCtx_VideoA->width * m_pReadCodecCtx_VideoA->height;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){EnterCriticalSection(&m_csVideoASection);av_fifo_generic_write(m_pVideoAFifo, pFrame->data[0], y_size, NULL);av_fifo_generic_write(m_pVideoAFifo, pFrame->data[1], y_size / 4, NULL);av_fifo_generic_write(m_pVideoAFifo, pFrame->data[2], y_size / 4, NULL);LeaveCriticalSection(&m_csVideoASection);break;}else{Sleep(100);}}}if (ret == AVERROR(EAGAIN)){continue;}}av_frame_free(&pFrame);
}DWORD WINAPI CVideoMerge::VideoBReadProc(LPVOID lpParam)
{CVideoMerge *pVideoMerge = (CVideoMerge *)lpParam;if (pVideoMerge != NULL){pVideoMerge->VideoBRead();}return 0;
}void CVideoMerge::VideoBRead()
{AVFrame *pFrame;pFrame = av_frame_alloc();int y_size = m_pReadCodecCtx_VideoB->width * m_pReadCodecCtx_VideoB->height;AVPacket packet = { 0 };int ret = 0;int iCount = 0;while (1){av_packet_unref(&packet);ret = av_read_frame(m_pFormatCtx_FileB, &packet);if (ret == AVERROR(EAGAIN)){continue;}else if (ret == AVERROR_EOF){break;}else if (ret < 0){break;}ret = avcodec_send_packet(m_pReadCodecCtx_VideoB, &packet);if (ret >= 0){ret = avcodec_receive_frame(m_pReadCodecCtx_VideoB, pFrame);if (ret == AVERROR(EAGAIN)){continue;}else if (ret == AVERROR_EOF){break;}else if (ret < 0) {break;}while (1){if (av_fifo_space(m_pVideoBFifo) >= m_iYuv420FrameSize){EnterCriticalSection(&m_csVideoBSection);av_fifo_generic_write(m_pVideoBFifo, pFrame->data[0], y_size, NULL);av_fifo_generic_write(m_pVideoBFifo, pFrame->data[1], y_size / 4, NULL);av_fifo_generic_write(m_pVideoBFifo, pFrame->data[2], y_size / 4, NULL);LeaveCriticalSection(&m_csVideoBSection);break;}else{Sleep(100);}}}if (ret == AVERROR(EAGAIN)){continue;}}av_frame_free(&pFrame);
}DWORD WINAPI CVideoMerge::VideoMergeProc(LPVOID lpParam)
{CVideoMerge *pVideoMerge = (CVideoMerge *)lpParam;if (pVideoMerge != NULL){pVideoMerge->VideoMerge();}return 0;
}void CVideoMerge::VideoMerge()
{int ret = 0;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);AVFrame *pFrameVideoB = av_frame_alloc();uint8_t *videoB_buffer_yuv420 = (uint8_t *)av_malloc(m_iYuv420FrameSize);av_image_fill_arrays(pFrameVideoB->data, pFrameVideoB->linesize, videoB_buffer_yuv420, AV_PIX_FMT_YUV420P, m_pReadCodecCtx_VideoB->width, m_pReadCodecCtx_VideoB->height, 1);int iOutVideoWidth = m_pReadCodecCtx_VideoB->width;int iOutVideoHeight = m_pReadCodecCtx_VideoB->height;AVPacket packet = { 0 };int iPicCount = 0;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_pReadCodecCtx_VideoA->width, m_pReadCodecCtx_VideoA->height, 1);while (1){if (NULL == m_pVideoAFifo){break;}if (NULL == m_pVideoBFifo){break;}int iVideoASize = av_fifo_size(m_pVideoAFifo);int iVideoBSize = av_fifo_size(m_pVideoBFifo);if (iVideoASize >= m_iYuv420FrameSize && iVideoBSize >= m_iYuv420FrameSize){EnterCriticalSection(&m_csVideoASection);av_fifo_generic_read(m_pVideoAFifo, videoA_buffer_yuv420, m_iYuv420FrameSize, NULL);LeaveCriticalSection(&m_csVideoASection);EnterCriticalSection(&m_csVideoBSection);av_fifo_generic_read(m_pVideoBFifo, videoB_buffer_yuv420, m_iYuv420FrameSize, NULL);LeaveCriticalSection(&m_csVideoBSection);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;pFrameVideoA->width = iOutVideoWidth;pFrameVideoA->height = iOutVideoHeight;pFrameVideoA->format = AV_PIX_FMT_YUV420P;pFrameVideoB->pkt_dts = pFrameVideoB->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));pFrameVideoB->pkt_duration = 0;pFrameVideoB->pkt_pos = -1;pFrameVideoB->width = iOutVideoWidth;pFrameVideoB->height = iOutVideoHeight;pFrameVideoB->format = AV_PIX_FMT_YUV420P;//if (iPicCount == 0){LOG_INFO("begin av_buffersrc_add_frame");//ret = av_buffersrc_add_frame_flags(m_pFilterCtxSrcVideoA, pFrameVideoA, 0);ret = av_buffersrc_add_frame(m_pFilterCtxSrcVideoA, pFrameVideoA);if (ret < 0){break;}//ret = av_buffersrc_add_frame_flags(m_pFilterCtxSrcVideoB, pFrameVideoB, 0);ret = av_buffersrc_add_frame(m_pFilterCtxSrcVideoB, pFrameVideoB);if (ret < 0){break;}}do{//while (1){ret = av_buffersink_get_frame(m_pFilterCtxSink, pFrame_out);if (ret < 0){//printf("Mixer: failed to call av_buffersink_get_frame_flags\n");break;}LOG_INFO("end av_buffersink_get_frame_flags");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 = iOutVideoWidth;pFrame_out->height = iOutVideoHeight;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++;av_frame_unref(pFrame_out);}} while (0);}else{if (m_hVideoAReadThread == NULL && m_hVideoBReadThread == NULL){break;}Sleep(1);}}av_write_trailer(m_pFormatCtx_Out);avio_close(m_pFormatCtx_Out->pb);av_frame_free(&pFrame_out);av_frame_free(&pFrameVideoA);av_frame_free(&pFrameVideoB);
}

ffmpeg利用滤镜合并两个视频,一左一右---avfilter_link实现相关推荐

  1. ffmpeg利用滤镜合并四个视频,左一右三

    今天利用ffmpeg的滤镜功能合并,左一右三方式,如下所示: 读者需要先对滤镜的描述字符串有所了解,读者可以参看我写的一篇博客: ffmpeg利用滤镜进行视频混合(命令行) 四个文件都是1920x10 ...

  2. ffmpeg利用滤镜合并两个视频,一左一右

    ffmpeg关于视频合并的例子,命令行的一堆,代码的感觉不多,命令行跟代码感觉还是有些差距,代码上要求还是高一些,关于滤镜的命令行,读者可以看我的一篇博客 ffmpeg利用滤镜进行视频混合(命令行) ...

  3. 如何将两个视频一左一右合并

    如何将两个视频一左一右合并?当我们在做新媒体工作的时候,时候会将2个类似的视频进行合并,这样做成对比.对于那些没有视频处理经验的人来说,视频的合并也可能是很花费时间和精力的一个事情,但是如果你有这方面 ...

  4. html怎么设置两块区域,将两个视频一左一右拼接 可裁剪画面并设置视频画面大小及位置...

    现在分享小视频依然是很火的,小编每次刷小视频的时候,总会被视频中漂亮小姐姐的手势舞所吸引,嘻嘻--很多人分享的小视频都会采用分屏效果,即一个画面中有两个视频内容,这样可以形成很明显的对比,当然啦,如果 ...

  5. ffmpeg合并两个视频

    我需要在程序里面实现合并两个视频的功能,用的是C#,但是直接能使用的成熟工具好像并不多,这两天找了很多工具: aforge.NET 可以处理视频但是只能处理视频画面,声音处理不了,对我来说没用. ac ...

  6. 利用canconvert合并两个dbc,FileNotFoundError解决!!!

    项目场景: 利用canconvert合并两个dbc 问题描述 利用canconvert合并两个dbc,总是报错,翻遍了百度,都是拷贝的答案,说话说一半! 长这样: 就戛然而止!!!就真的很气!!! 痛 ...

  7. 怎么把两个div一左一右放

    怎么把两个div一左一右放 1.代码 <%@ page contentType="text/html;charset=UTF-8" language="java&q ...

  8. vue怎么合并两个视频_【软件分享】视频分割合并软件哪款好用呢?怎么剪切合并视频?...

    现在有很多小伙伴都喜欢喜欢自己拍视频上传到各大视频平台,有时候会遇到关于视频剪辑方面的难题,比如说,视频怎么剪切?怎么将两段小视频合并成一个呢?这些都是一些比较常常遇到的问题,今天小编就给大家介绍一款 ...

  9. FFmpeg混流:将两个视频放在一个画布并以rtmp流输出

    1.将其中一个视频置于画布右下角(这种情况直播用的最多,这也是我用FFmpeg想要实现的效果) 命令如下: ./ffmpeg -i 飞驰人生HD国语中字.mkv -i 小丑BD中英双字.mp4 -fi ...

最新文章

  1. Java fork join ForkJoinPool 用法例子
  2. 要命的定义函数。。。参数组合。请认真理解!
  3. matlab的NLP功能,pyhanlp 共性分析与短语提取内容详解
  4. UWP 查找模板中的控件
  5. 武汉理工计算机拟录取,2017武汉理工计算机复试
  6. 地砖中间高四边低_地砖上墙到底好不好?幸好我家没这么做否则全毁了!
  7. documentFragment深入理解
  8. S3C2440、S3C2450和S3C6410之间区别
  9. Linux安装Jenkins
  10. 微信小程序入门三:轮播图
  11. MVC设计模式-学习笔记
  12. 如何报名腾讯云认证考试?
  13. Python使用struct处理二进制(转)
  14. javamailsender注入失败_关于SpringBoot使用Redis空指针的问题(不能成功注入的问题)...
  15. 设置网页地址栏小图标
  16. 点击图片实现图片放大
  17. 地心一号-基于STM8的超迷你自平衡小车-DIY套件
  18. 03系统服务器下安装WMP10实际经验分析
  19. 论文的每一页最上方都有一条横线(word2007)
  20. CAD/CAM 软件架构总结

热门文章

  1. OI退役记,第五部分,CTSCAPIO2017
  2. java socket解决半包、粘包问题
  3. 搭档之家:“假欧冠决赛”竟1万多人观看,刺不刺激,惊不惊喜,意不意外?
  4. 名图怎么弄云服务器_“双车”战略,名图如何驱动新兴细分市场
  5. DirectX9.0 入门手册
  6. 推荐几款文字翻译软件,快速实现翻译
  7. 看来不止一次的电影(电影经典给你好看准备下载下来,免得以后收费了)
  8. Redis 编译报zmalloc.h相关的错
  9. Codepage的定义和历史
  10. 艾兰岛编辑器-选项对话