0
点赞
收藏
分享

微信扫一扫

QT 使用ffmpeg 学习6 ffmpeg API保存流到文件demo

一、功能说明

打开一个输入流,取帧保存到文件中。
一些函数说明:

avformat_open_input

该函数用于打开多媒体数据并且获得一些相关的信息。它的声明位于libavformat\avformat.h,如下所示:

int avformat_open_input(AVFormatContext **ps, const char *filename, AVInputFormat *fmt, AVDictionary **options);
  • ps:函数调用成功之后处理过的AVFormatContext结构体。
  • file:打开的视音频流的URL。
  • fmt:强制指定AVFormatContext中AVInputFormat的。这个参数一般情况下可以设置为NULL,这样FFmpeg可以自动检测AVInputFormat。
  • dictionay:附加的一些选项,一般情况下可以设置为NULL。

avformat_find_stream_info()

该函数可以读取一部分视音频数据并且获得一些相关的信息。avformat_find_stream_info()的声明位于libavformat\avformat.h,如下所示。

int avformat_find_stream_info(AVFormatContext *ic, AVDictionary **options);

简单解释一下它的参数的含义:

  • ic:输入的AVFormatContext。
  • options:额外的选项,目前没有深入研究过。
    函数正常执行后返回值大于等于0。

avformat_find_stream_info()的定义位于libavformat\utils.c。它的代码比较长。它除了给AVStream结构体赋值,还有以下几个关键流程:

  • 1.查找解码器:find_decoder() 用于找到合适的解码器
  • 2.打开解码器:avcodec_open2()
  • 3.读取完整的一帧压缩编码的数据:read_frame_internal()
    注:av_read_frame()内部实际上就是调用的read_frame_internal()。
  • 4.解码一些压缩编码数据:try_decode_frame()
  • 5.has_codec_parameters()用于检查AVStream中的成员变量是否都已经设置完毕
  • 6.estimate_timings()位于avformat_find_stream_info()最后面,用于估算AVFormatContext以及AVStream的时长duration。

有3种估算方法:
(1)通过pts(显示时间戳)。该方法调用estimate_timings_from_pts()。它的基本思想就是读取视音频流中的结束位置AVPacket的PTS和起始位置AVPacket的PTS,两者相减得到时长信息。
(2)通过已知流的时长。该方法调用fill_all_stream_timings()。它的代码没有细看,但从函数的注释的意思来说,应该是当有些视音频流有时长信息的时候,直接赋值给其他视音频流。
(3)通过bitrate(码率)。该方法调用estimate_timings_from_bit_rate()。它的基本思想就是获得整个文件大小,以及整个文件的bitrate,两者相除之后得到时长信息。

  • 7.estimate_timings_from_bit_rate
    (1)如果AVFormatContext中没有bit_rate信息,就把所有AVStream的bit_rate加起来作为AVFormatContext的bit_rate信息。
    (2)使用文件大小filesize除以bitrate得到时长信息。具体的方法是:
    AVStream->duration=(filesize*8/bit_rate)/time_base

1)filesize乘以8是因为需要把Byte转换为Bit
2)具体的实现函数是那个av_rescale()函数。x=av_rescale(a,b,c)的含义是x=a*b/c。
3)之所以要除以time_base,是因为AVStream中的duration的单位是time_base,注意这和AVFormatContext中的duration的单位(单位是AV_TIME_BASE,固定取值为1000000)是不一样的。

avformat_new_stream创建流通道

在 AVFormatContext 中创建 Stream 通道。AVStream 即是流通道。将 H264 和 AAC 码流存储为MP4文件的时候,就需要在 MP4文件中增加两个流通道,一个存储Video:H264,一个存储Audio:AAC。(假设H264和AAC只包含单个流通道)。

avformat_write_header

avformat_write_header()的声明位于libavformat\avformat.h

int avformat_write_header(AVFormatContext *s, AVDictionary **options);

参数说明:

  • s:用于输出的AVFormatContext。
  • options:额外的选项,一般为NULL。
    函数正常执行后返回值等于0。

二、代码demo

#ifdef __cplusplus
extern "C" {
#endif

#include <libavcodec/avcodec.h>
#include <libavdevice/avdevice.h>
#include <libavformat/avformat.h>
#include <libavfilter/avfilter.h>
#include <libavutil/avutil.h>
#include <libswscale/swscale.h>

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <math.h>

#ifdef __cplusplus
}
#endif


AVFormatContext *i_fmt_ctx;
AVStream *i_video_stream;

AVFormatContext *o_fmt_ctx;
AVStream *o_video_stream;

int main(int argc, char *argv[])
{
avcodec_register_all();
av_register_all();
avformat_network_init();

/* should set to NULL so that avformat_open_input() allocate a new one */
i_fmt_ctx = NULL;
char rtspUrl[] = "rtsp://地址";
const char *filename = "1.mp4";
// 打开输入流
if (avformat_open_input(&i_fmt_ctx, rtspUrl, NULL, NULL)!=0)
{
fprintf(stderr, "could not open input file\n");
return -1;
}

if (avformat_find_stream_info(i_fmt_ctx, NULL)<0)
{
fprintf(stderr, "could not find stream info\n");
return -1;
}

// 打印流信息
//av_dump_format(i_fmt_ctx, 0, argv[1], 0);

/* find first video stream */
for (unsigned i=0; i<i_fmt_ctx->nb_streams; i++)
{
if (i_fmt_ctx->streams[i]->codec->codec_type == AVMEDIA_TYPE_VIDEO)
{
i_video_stream = i_fmt_ctx->streams[i];
break;
}
}
if (i_video_stream == NULL)
{
fprintf(stderr, "didn't find any video stream\n");
return -1;
}

// 初始化一个用于输出的AVFormatContext结构体
avformat_alloc_output_context2(&o_fmt_ctx, NULL, NULL, filename);

/*
* since all input files are supposed to be identical (framerate, dimension, color format, ...)
* we can safely set output codec values from first input file
*/
o_video_stream = avformat_new_stream(o_fmt_ctx, NULL);
{
//avformat_new_stream之后便在 AVFormatContext 里增加了 AVStream 通道(相关的index已经被设置了)。
//之后就可以自行设置 AVStream 的一些参数信息。例如 : codec_id , format ,bit_rate ,width , height

AVCodecContext *c;
c = o_video_stream->codec;
c->bit_rate = 400000;
c->codec_id = i_video_stream->codec->codec_id;
c->codec_type = i_video_stream->codec->codec_type;
c->time_base.num = i_video_stream->time_base.num;
c->time_base.den = i_video_stream->time_base.den;
fprintf(stderr, "time_base.num = %d time_base.den = %d\n", c->time_base.num, c->time_base.den);
c->width = i_video_stream->codec->width;
c->height = i_video_stream->codec->height;
c->pix_fmt = i_video_stream->codec->pix_fmt;
printf("%d %d %d", c->width, c->height, c->pix_fmt);
c->flags = i_video_stream->codec->flags;
c->flags |= AV_CODEC_FLAG_GLOBAL_HEADER;// CODEC_FLAG_GLOBAL_HEADER;
c->me_range = i_video_stream->codec->me_range;
c->max_qdiff = i_video_stream->codec->max_qdiff;

c->qmin = i_video_stream->codec->qmin;
c->qmax = i_video_stream->codec->qmax;

c->qcompress = i_video_stream->codec->qcompress;
}

avio_open(&o_fmt_ctx->pb, filename, AVIO_FLAG_WRITE);

avformat_write_header(o_fmt_ctx, NULL);

int last_pts = 0;
int last_dts = 0;

int64_t pts, dts;
while (1)
{
AVPacket i_pkt;
av_init_packet(&i_pkt);
i_pkt.size = 0;
i_pkt.data = NULL;
if (av_read_frame(i_fmt_ctx, &i_pkt) <0 )
break;
/*
* pts and dts should increase monotonically
* pts should be >= dts
*/
i_pkt.flags |= AV_PKT_FLAG_KEY;
pts = i_pkt.pts;
i_pkt.pts += last_pts;
dts = i_pkt.dts;
i_pkt.dts += last_dts;
i_pkt.stream_index = 0;

//printf("%lld %lld\n", i_pkt.pts, i_pkt.dts);
static int num = 1;
printf("frame %d\n", num++);
// 这里demo录一段就停止
if(num>=1000)break;
av_interleaved_write_frame(o_fmt_ctx, &i_pkt);
//av_free_packet(&i_pkt);
//av_init_packet(&i_pkt);
}
last_dts += dts;
last_pts += pts;

avformat_close_input(&i_fmt_ctx);

av_write_trailer(o_fmt_ctx);

avcodec_close(o_fmt_ctx->streams[0]->codec);
av_freep(&o_fmt_ctx->streams[0]->codec);
av_freep(&o_fmt_ctx->streams[0]);

avio_close(o_fmt_ctx->pb);
av_free(o_fmt_ctx);

return 0;
}

举报

相关推荐

0 条评论