使用 ffmpeg 进行网络推流:拉流->解封装->解码->处理原始数据(音频、视频)->编码->编码->推流

简要说明:

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
  • 2
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值