轉:ffmpeg學習(二) 通過rtsp擷取H264裸流並儲存到mp4檔案

來源:互聯網
上載者:User

標籤:


本篇將使用上節http://www.cnblogs.com/wenjingu/p/3977015.html中編譯好的庫檔案通過rtsp擷取網路上的h464裸流並儲存到mp4檔案中。

1、VS2010建立VC++  win32控制台項目

2、在工程目錄下建立lib目錄和include目錄,將已編譯好的lib拷打lib下,include拷到include下,dll拷到Debug目錄下

3、工程屬性--配置屬性--VC++目錄--包含目錄,添加ffmpeg標頭檔目錄及其他第三方標頭檔目錄

                                             連結器--常規--附加庫目錄,添加lib目錄

                                             連結器--輸入--附加依賴項,添加各個lib名

4、設計和實現:

4.1 設計思路:

       組件和網路初始化——>開啟網路流——>擷取網路流資訊——>根據網路流資訊初始化輸出資料流資訊——>建立並開啟mp4檔案——>寫mp4檔案頭

                    ——>迴圈讀取輸入資料流並寫入mp4檔案——>寫檔案尾——>關閉流,關閉檔案

4.2 關鍵資料結構:

       AVFormatContext,AVStream,AVCodecContext,AVPacket,AVFrame等,它們的關係解釋如下:

       一個AVFormatContext包含多個AVStream,每個碼流包含了AVCodec和AVCodecContext,AVPicture是AVFrame的一個子集,

他們都是資料流在編解過程中用來儲存資料緩衝的對像,從資料流讀出的資料首先是儲存在AVPacket裡,也可以理解為一個AVPacket最多隻包含一個AVFrame,

而一個AVFrame可能包含好幾個AVPacket,AVPacket是種資料流分包的概念。

4.3 關鍵函數:

int avformat_open_input(AVFormatContext **ps, const char *filename, AVInputFormat *fmt, AVDictionary **options); //開啟網路流或檔案流

int avformat_write_header(AVFormatContext *s, AVDictionary **options);//根據檔案名稱的尾碼寫相應格式的檔案頭

int av_read_frame(AVFormatContext *s, AVPacket *pkt);//從輸入資料流中讀取一個分包

int av_interleaved_write_frame(AVFormatContext *s, AVPacket *pkt);//往輸出資料流中寫一個分包

int av_write_trailer(AVFormatContext *s);//寫輸出資料流(檔案)的檔案尾

4.4 代碼:

view sourceprint?

001.#include "stdafx.h"

002. 

003.#ifdef __cplusplus

004.extern "C" {

005.#endif

006. 

007.#include <libavcodec/avcodec.h>

008.#include <libavdevice/avdevice.h>

009.#include <libavformat/avformat.h>

010.#include <libavfilter/avfilter.h>

011.#include <libavutil/avutil.h>

012.#include <libswscale/swscale.h>

013. 

014.#include <stdlib.h>

015.#include <stdio.h>

016.#include <string.h>

017.#include <math.h>

018. 

019.#ifdef __cplusplus

020.}

021.#endif

022. 

023.AVFormatContext *i_fmt_ctx;

024.AVStream *i_video_stream;

025. 

026.AVFormatContext *o_fmt_ctx;

027.AVStream *o_video_stream;

028. 

029.int _tmain(int argc, char **argv)

030.{

031.avcodec_register_all();

032.av_register_all();

033.avformat_network_init();

034. 

035./* should set to NULL so that avformat_open_input() allocate a new one */

036.i_fmt_ctx = NULL;

037.char rtspUrl[] = "rtsp://admin:[email protected]:554";

038.const char *filename = "1.mp4";

039.if (avformat_open_input(&i_fmt_ctx, rtspUrl, NULL, NULL)!=0)

040.{

041.fprintf(stderr, "could not open input file\n");

042.return -1;

043.}

044. 

045.if (avformat_find_stream_info(i_fmt_ctx, NULL)<0)

046.{

047.fprintf(stderr, "could not find stream info\n");

048.return -1;

049.}

050. 

051.//av_dump_format(i_fmt_ctx, 0, argv[1], 0);

052. 

053./* find first video stream */

054.for (unsigned i=0; i<i_fmt_ctx->nb_streams; i++)

055.{

056.if (i_fmt_ctx->streams[i]->codec->codec_type == AVMEDIA_TYPE_VIDEO)

057.{

058.i_video_stream = i_fmt_ctx->streams[i];

059.break;

060.}

061.}

062.if (i_video_stream == NULL)

063.{

064.fprintf(stderr, "didn‘t find any video stream\n");

065.return -1;

066.}

067. 

068.avformat_alloc_output_context2(&o_fmt_ctx, NULL, NULL, filename);

069. 

070./*

071.* since all input files are supposed to be identical (framerate, dimension, color format, ...)

072.* we can safely set output codec values from first input file

073.*/

074.o_video_stream = avformat_new_stream(o_fmt_ctx, NULL);

075.{

076.AVCodecContext *c;

077.c = o_video_stream->codec;

078.c->bit_rate = 400000;

079.c->codec_id = i_video_stream->codec->codec_id;

080.c->codec_type = i_video_stream->codec->codec_type;

081.c->time_base.num = i_video_stream->time_base.num;

082.c->time_base.den = i_video_stream->time_base.den;

083.fprintf(stderr, "time_base.num = %d time_base.den = %d\n", c->time_base.num, c->time_base.den);

084.c->width = i_video_stream->codec->width;

085.c->height = i_video_stream->codec->height;

086.c->pix_fmt = i_video_stream->codec->pix_fmt;

087.printf("%d %d %d", c->width, c->height, c->pix_fmt);

088.c->flags = i_video_stream->codec->flags;

089.c->flags |= CODEC_FLAG_GLOBAL_HEADER;

090.c->me_range = i_video_stream->codec->me_range;

091.c->max_qdiff = i_video_stream->codec->max_qdiff;

092. 

093.c->qmin = i_video_stream->codec->qmin;

094.c->qmax = i_video_stream->codec->qmax;

095. 

096.c->qcompress = i_video_stream->codec->qcompress;

097.}

098. 

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

100. 

101.avformat_write_header(o_fmt_ctx, NULL);

102. 

103.int last_pts = 0;

104.int last_dts = 0;

105. 

106.int64_t pts, dts;

107.while (1)

108.{

109.AVPacket i_pkt;

110.av_init_packet(&i_pkt);

111.i_pkt.size = 0;

112.i_pkt.data = NULL;

113.if (av_read_frame(i_fmt_ctx, &i_pkt) <0 )

114.break;

115./*

116.* pts and dts should increase monotonically

117.* pts should be >= dts

118.*/

119.i_pkt.flags |= AV_PKT_FLAG_KEY;

120.pts = i_pkt.pts;

121.i_pkt.pts += last_pts;

122.dts = i_pkt.dts;

123.i_pkt.dts += last_dts;

124.i_pkt.stream_index = 0;

125. 

126.//printf("%lld %lld\n", i_pkt.pts, i_pkt.dts);

127.static int num = 1;

128.printf("frame %d\n", num++);

129.av_interleaved_write_frame(o_fmt_ctx, &i_pkt);

130.//av_free_packet(&i_pkt);

131.//av_init_packet(&i_pkt);

132.}

133.last_dts += dts;

134.last_pts += pts;

135. 

136.avformat_close_input(&i_fmt_ctx);

137. 

138.av_write_trailer(o_fmt_ctx);

139. 

140.avcodec_close(o_fmt_ctx->streams[0]->codec);

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

142.av_freep(&o_fmt_ctx->streams[0]);

143. 

144.avio_close(o_fmt_ctx->pb);

145.av_free(o_fmt_ctx);

146. 

147.return 0;

148.}

5、測試

 

如為預存程序中程式的列印結果。產生的mp4檔案可以用任意支援該格式的播放器播放。

 

轉:ffmpeg學習(二) 通過rtsp擷取H264裸流並儲存到mp4檔案

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.