一、功能說明
打開一個輸入流,取幀儲存到檔案中。
一些函數說明:
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
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;
}