简要说明:
1、可拉流:rtmp、rtsp、http
2、可推流:
#include "stdafx.h"
extern "C"
{
#include "libavcodec/avcodec.h"
#include "libavformat/avformat.h"
#include "libswscale/swscale.h"
#include "libswresample/swresample.h"
};
#include "opencv2/opencv.hpp"
#ifdef _DEBUG
#pragma comment(lib, "opencv_ts300d.lib")
#pragma comment(lib, "opencv_world300d.lib")
#else
#pragma comment(lib, "opencv_ts300.lib")
#pragma comment(lib, "opencv_world300.lib")
#endif
#pragma comment(lib, "avcodec.lib")
#pragma comment(lib, "avformat.lib")
#pragma comment(lib, "avutil.lib")
#pragma comment(lib, "swscale.lib")
#pragma comment(lib, "swresample.lib")
#define MAX_AUDIO_FRAME_SIZE 192000 // 1 second of 48KHZ 32bit audio -> 32bit / 8 * 48000 * 1 = 192000 Byte
#define SAVE_FILE 0
/*
函数功能:将图片 foreground 叠加到图片 background 上
*/
static void overlayImage(const cv::Mat &background, const cv::Mat &foreground, cv::Mat &output, cv::Point2i location)
{
background.copyTo(output);
for (int y = std::max(location.y, 0); y < background.rows; ++y)
{
int fY = y - location.y;
if (fY >= foreground.rows)
break;
for (int x = std::max(location.x, 0); x < background.cols; ++x)
{
int fX = x - location.x;
if (fX >= foreground.cols)
break;
double opacity = ((double)foreground.data[fY * foreground.step + fX * foreground.channels() + 3]) / 255.;
for (int c = 0; opacity > 0 && c < output.channels(); ++c)
{
unsigned char foregroundPx =
foreground.data[fY * foreground.step + fX * foreground.channels() + c];
unsigned char backgroundPx =
background.data[y * background.step + x * background.channels() + c];
output.data[y*output.step + output.channels()*x + c] =
backgroundPx * (1. - opacity) + foregroundPx * opacity;
}
}
}
}
int main(int argc, char* argv[])
{
AVFormatContext* ifmt_ctx = NULL;
const char* input_filepath = NULL;
AVFormatContext* ofmt_ctx = NULL;
const char* output_filepath = NULL;
int idx_audio = -1;
int idx_video = -1;
AVStream* istream_audio = NULL;
AVCodecContext* codec_ctx_audio = NULL;
AVCodec* codec_audio = NULL;
AVStream* istream_video = NULL;
AVCodecContext* codec_ctx_video = NULL;
AVCodec* codec_video = NULL;
AVStream* ostream_video = NULL;
AVStream* ostream_audio = NULL;
AVCodecContext* codec_ctx_2h264 = NULL;
AVCodec* codec_2h264 = NULL;
AVCodecContext* codec_ctx_2aac = NULL;
AVCodec* codec_2aac = NULL;
struct SwsContext* img_convert_ctx_2bgr = NULL;
struct SwsContext* img_convert_ctx_2yuv = NULL;
uint8_t* buffer_bgr = NULL;
uint8_t* buffer_yuv = NULL;
struct SwrContext* au_convert_ctx = NULL;
uint8_t* out_audio_buffer = NULL;
av_register_all();
avformat_network_init();
//input_filepath = "rtmp://live.hkstv.hk.lxdns.com/live/hks";
input_filepath = "rtsp://admin:[email protected]:55