#include "rtmp_encode_pthread.h"
double STREAM_DURATION=10.0; /*录制视频的持续时间 秒*/
#define STREAM_FRAME_RATE 25 /* images/s 这里可以根据摄像头的采集速度来设置帧率 */
#define STREAM_PIX_FMT AV_PIX_FMT_YUV420P /* default pix_fmt */
#define SCALE_FLAGS SWS_BICUBIC
bool encode_type=1; //1录制 0推流
//存放视频的宽度和高度
int video_width=0;
int video_height=0;
bool encode_run_flag=0;
QString rtmp_push_addr; //推流地址
bool device_type=0; //0表示摄像头 1表示桌面
bool data_type=0; //0表示RTMP 1表示RTSP
QString CameraDevieName; //摄像头设备的名称
// 单个输出AVStream的包装器
typedef struct OutputStream
{
AVStream *st;
AVCodecContext *enc;
/*下一帧的点数*/
int64_t next_pts;
int samples_count;
AVFrame *frame;
AVFrame *tmp_frame;
float t, tincr, tincr2;
struct SwsContext *sws_ctx;
struct SwrContext *swr_ctx;
}OutputStream;
typedef struct IntputDev
{
AVCodecContext *pCodecCtx;
AVCodec *pCodec;
AVFormatContext *v_ifmtCtx;
int videoindex;
struct SwsContext *img_convert_ctx;
AVPacket *in_packet;
AVFrame *pFrame,*pFrameYUV;
}IntputDev;
static int write_frame(AVFormatContext *fmt_ctx, const AVRational *time_base, AVStream *st, AVPacket *pkt)
{
/* 将输出数据包时间戳值从编解码器重新调整为流时基 */
av_packet_rescale_ts(pkt, *time_base, st->time_base);
pkt->stream_index = st->index;
/*将压缩的帧写入媒体文件。*/
return av_interleaved_write_frame(fmt_ctx, pkt);
}
/*添加输出流。 */
static void add_stream(OutputStream *ost, AVFormatContext *oc,AVCodec **codec,enum AVCodecID codec_id)
{
AVCodecContext *c;
int i;
/* find the encoder */
*codec = avcodec_find_encoder(codec_id);
if (!(*codec))
{
qDebug()<<"avcodec_find_encoder error.";
exit(1);
}
ost->st = avformat_new_stream(oc, nullptr);
if (!ost->st)
{
qDebug()<<"Could not allocate stream.";
exit(1);
}
ost->st->id = oc->nb_streams-1;
c = avcodec_alloc_context3(*codec);
if (!c)
{
qDebug()<<"Could not alloc an encoding context";
exit(1);
}
ost->enc = c;
switch((*codec)->type)
{
case AVMEDIA_TYPE_AUDIO:
c->sample_fmt = (*codec)->sample_fmts ?
(*codec)->sample_fmts[0] : AV_SAMPLE_FMT_FLTP;
c->bit_rate = 64000;
c->sample_rate = 44100;
if ((*codec)->supported_samplerates) {
c->sample_rate = (*codec)->supported_samplerates[0];
for (i = 0; (*codec)->supported_samplerates[i]; i++) {
if ((*codec)->supported_samplerates[i] == 44100)
c->sample_rate = 44100;
}
}
c->channels = av_get_channel_layout_nb_channels(c->channel_layout);
c->channel_layout = AV_CH_LAYOUT_STEREO;
if ((*codec)->channel_layouts) {
c->channel_layout = (*codec)->channel_layouts[0];
for (i = 0; (*codec)->channel_layouts[i]; i++) {
if ((*codec)->channel_layouts[i] == AV_CH_LAYOUT_STEREO)
c->channel_layout = AV_CH_LAYOUT_STEREO;
}
}
c->channels = av_get_channel_layout_nb_channels(c->channel_layout);
ost->st->time_base = (AVRational){ 1, c->sample_rate };
break;
case AVMEDIA_TYPE_VIDEO:
c->codec_id = codec_id;
c->bit_rate = 2500000; //平均比特率,例子代码默认值是400000
/* 分辨率必须是2的倍数。*/
c->width=video_width;
c->height=video_height;
/*时基:这是基本的时间单位(以秒为单位)
*表示其中的帧时间戳。 对于固定fps内容,
*时基应为1 /framerate,时间戳增量应为
*等于1。*/
ost->st->time_base = (AVRational){1,STREAM_FRAME_RATE}; //帧率设置
c->time_base = ost->st->time_base;
c->gop_size = 12; /* 最多每十二帧发射一帧内帧 */
c->pix_fmt = STREAM_PIX_FMT;
if(c->codec_id == AV_CODEC_ID_MPEG2VIDEO)
{
/* 只是为了测试,我们还添加了B帧 */
c->max_b_frames = 2;
}
if(c->codec_id == AV_CODEC_ID_MPEG1VIDEO)
{
/*需要避免使用其中一些系数溢出的宏块。
*普通视频不会发生这种情况,因为
*色度平面的运动与亮度平面不匹配。 */
c->mb_decision = 2;
}
break;
default:
break;
}
/* 某些格式希望流头分开。 */
if (oc->oformat->flags & AVFMT_GLOBALHEADER)
c->flags |= AV_CODEC_FLAG_GLOBAL_HEADER;
}
static AVFrame *alloc_picture(enum AVPixelFormat pix_fmt, int width, int height)
{
AVFrame *picture;
int ret;
picture = av_frame_alloc();
if (!picture)
return nullptr;
picture->format = pix_fmt;
picture->width = width;
picture->height = height;
/* 为帧数据分配缓冲区 */
ret = av_frame_get_buffer(picture, 32);
if(ret<0)
{
qDebug()<<"为帧数据分配缓冲区错误.";
exit(1);
}
return picture;
}
static void open_video(AVFormatContext *oc, AVCodec *codec, OutputStream *ost, AVDictionary *opt_arg)
{
int ret;
AVCodecContext *c = ost->enc;
AVDictionary *opt = nullptr;
av_dict_copy(&opt, opt_arg, 0);
/* open the codec */
ret = avcodec_open2(c, codec, &opt);
av_dict_free(&opt);
if (ret < 0)
{
exit(1);
}
/* 分配并初始化可重用框架 */
ost->frame = alloc_picture(c->pix_fmt, c->width, c->height);
if (!ost->frame)
{
qDebug()<<"Could not allocate video frame";
exit(1);
}
qDebug("ost->frame alloc success fmt=%d w=%d h=%d\n",c->pix_fmt,c->width, c->height);
/*如果输出格式不是YUV420P,则为临时YUV420P
*也需要图片。 然后将其转换为所需的
*输出格式。 */
ost->tmp_frame = nullptr;
if(c->pix_fmt != AV_PIX_FMT_YUV420P)
{
ost->tmp_frame = alloc_picture(AV_PIX_FMT_YUV420P, c->width, c->height);
if (!ost->tmp_frame)
{
qDebug("Could not allocate temporary picture\n");
exit(1);
}
}
/* 将流参数复制到多路复用器*/
ret=avcodec_parameters_from_context(ost->st->codecpar, c);
if(ret<0)
{
qDebug("Could not copy the stream parameters\n");
exit(1);
}
}
/*
*编码一个视频帧
*编码完成后返回1,否则返回0
*/
static int write_video_frame(AVFormatContext *oc, OutputStream *ost,AVFrame *frame)
{
int ret;
AVCodecContext *c;
int got_packet=0;
AVPacket pkt={0};
if(frame==nullptr)
return 1;
c = ost->enc;
av_init_packet(&pkt);
/* 编码图像*/
ret = avcodec_encode_video2(c, &pkt, frame, &got_packet);
if(ret<0)
{
exit(1);
}
if(got_packet)
{
ret = write_frame(oc, &c->time_base, ost->st, &pkt);
}else
{
ret = 0;
}
if(ret<0)
{
exit(1);
}
return (frame || got_packet) ? 0 : 1;
}
static AVFrame *get_video_frame(OutputStream *ost,IntputDev* input,int *got_pic)
{
int ret, got_picture;
AVCodecContext *c = ost->enc;
AVFrame * ret_frame=nullptr;
if(av_compare_ts(ost->next_pts, c->time_base,STREAM_DURATION, (AVRational){1,1})>=0)
没有合适的资源?快使用搜索试试~ 我知道了~
基于Qt+ffmpeg设计摄像头推流录制软件(ffmpeg打开摄像头).zip
共10个文件
cpp:3个
h:2个
png:2个
1.该资源内容由用户上传,如若侵权请联系客服进行举报
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
版权申诉
5星 · 超过95%的资源 3 下载量 20 浏览量
2023-02-14
16:23:48
上传
评论 4
收藏 131KB ZIP 举报
温馨提示
功能:采用ffmpeg 推流 摄像头到rtmp、rtsp服务器 或者 保存到本地。 特点:推流、采集 都采用ffmpeg原生代码实现。 Qt版本:Qt5.12.6 minGW32位 FFMPEG版本:4.2.2 测试环境:windows 其他系统按道理一样的。
资源推荐
资源详情
资源评论
收起资源包目录
21 基于ffmpeg设计摄像头推流录制软件(ffmpeg打开摄像头).zip (10个子文件)
2022-09-03_214233.png 118KB
FFMPEG_RTMP_PushFlow
widget.ui 7KB
rtmp_encode_pthread.h 2KB
widget.cpp 4KB
logo.png 4KB
main.cpp 1024B
rtmp_encode_pthread.cpp 18KB
FFMPEG_RTMP_PushFlow.pro 2KB
logo.ico 135KB
widget.h 755B
共 10 条
- 1
资源评论
- qiaokang82023-09-01资源不错,很实用,内容全面,介绍详细,很好用,谢谢分享。
- m0_750916742023-07-15支持这个资源,内容详细,主要是能解决当下的问题,感谢大佬分享~
- 2301_774807192023-12-04资源值得借鉴的内容很多,那就浅学一下吧,值得下载!
DS小龙哥
- 粉丝: 4w+
- 资源: 522
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功