基于 ffplay 简化完成
运行环境为Ubuntu 20.10
main.cpp
#include <inttypes.h>
#include <math.h>
#include <limits.h>
#include <signal.h>
#include <stdint.h>
#include <sys/time.h>
#ifdef __cplusplus
extern "C" {
#endif
#include "libavutil/avstring.h"
#include "libavutil/eval.h"
#include "libavutil/mathematics.h"
#include "libavutil/pixdesc.h"
#include "libavutil/imgutils.h"
#include "libavutil/dict.h"
#include "libavutil/parseutils.h"
#include "libavutil/samplefmt.h"
#include "libavutil/avassert.h"
#include "libavutil/time.h"
#include "libavutil/bprint.h"
#include "libavformat/avformat.h"
#include "libavdevice/avdevice.h"
#include "libswscale/swscale.h"
#include "libavutil/opt.h"
#include "libavcodec/avfft.h"
#include "libswresample/swresample.h"
# include "libavfilter/avfilter.h"
# include "libavfilter/buffersink.h"
# include "libavfilter/buffersrc.h"
#ifdef __cplusplus
} // endof extern "C"
#endif
#include <opencv2/opencv.hpp>
#include <assert.h>
typedef struct VideoState {
AVFormatContext *ic;
AVCodecContext *avctx;
int video_stream;
AVStream *video_st;
int eof;
char *filename;
AVFilterContext *in_video_filter; // the first filter in the video chain
AVFilterContext *out_video_filter; // the last filter in the video chains
} VideoState;
using namespace cv;
/* options specified by the user */
static const char *input_filename = "/home/luke/untitled/IronMan.mp4";
static const char* wanted_stream_spec[AVMEDIA_TYPE_NB] = {0};
AVDictionary *sws_dict;
AVDictionary *format_opts;
Mat avframeToCvmat(const AVFrame * frame) {
int width = frame->width;
int height = frame->height;
Mat image(height, width, CV_8UC3);
int cvLinesizes[1];
cvLinesizes[0] = image.step1();
SwsContext* conversion = sws_getContext(width, height, (AVPixelFormat) frame->format, width, height, AVPixelFormat::AV_PIX_FMT_BGR24, SWS_FAST_BILINEAR, NULL, NULL, NULL);
sws_scale(conversion, frame->data, frame->linesize, 0, height, &image.data, cvLinesizes);
sws_freeContext(conversion);
return image;
}
static int configure_filtergraph(AVFilterGraph *graph, AVFilterContext *source_ctx, AVFilterContext *sink_ctx) {
int ret, i;
int nb_filters = graph->nb_filters;
AVFilterInOut *outputs = NULL, *inputs = NULL;
if ((ret = avfilter_li