2013-12-11 94 views
0

我对如何将视频流保存到mp4文件中感到非常困惑。我正在使用ffmpeg。让我来解释这个问题:将网络摄像机(RTSP)的帧保存到mp4文件

  1. 我通过RTSP(H.264流)与avformat_open_input(连接到网络摄像机),avformat_find_stream_info(),av_read_play(),和我得到av_read_frame帧()。
  2. 每次我用av_read_frame()获得一个帧时,我都会将相应的AVPacket存储在一个循环缓冲区中。
  3. 在我的应用程序的某些点中,选择了这个循环缓冲区的范围。我找到了一个关键帧。
  4. 一旦我从关键帧开始了AVPacket的列表,我将编写标题,帧和尾部,如下面代码中所述。

问题是,如果我尝试使用VLC,Windows Media Player或其他软件观看它,生成的mp4视频会产生人为现象。

我也意识到,该包的点不连续,而DTS是连续的。我知道B帧,但这对我来说是个问题吗?

// Prepare the output 
AVFormatContext* oc = avformat_alloc_context(); 
oc->oformat = av_guess_format(NULL, "video.mp4", NULL); 

// Must write header, packets, and trailing 
avio_open2(&oc->pb, "video.mp4", AVIO_FLAG_WRITE, NULL, NULL); 

// Write header 
AVStream* stream = avformat_new_stream(oc, (AVCodec*) context->streams[video_stream_index]->codec->codec); 
avcodec_copy_context(stream->codec, context->streams[video_stream_index]->codec); 
stream->sample_aspect_ratio = context->streams[video_stream_index]->codec->sample_aspect_ratio; 
avformat_write_header(oc, NULL); 

// FOR EACH FRAME... 
... av_write_frame(oc, circular[k]); ... 

// Write trailer and close the file 
av_write_trailer(oc); 
avcodec_close(stream->codec); 
avio_close(oc->pb); 
avformat_free_context(oc); 

太感谢你了,

+0

你可以看看rtmpdump的参考(或者甚至,直接使用librtmp) - http://rtmpdump.mplayerhq.hu/ – benjymous

回答

-1

首先:当你用相机的工作,最好是通过RTP通过TCP工作(如TCP传输协议)。 要启用此功能:

AVDictionary *ifmtdict; 
av_dict_set(&ifmtdict, "rtsp_transport", "tcp", 0); 
... 
avformat_open_input (..., &ifmtdict); 

二: 包奔涌后,等待第一个关键帧,并开始写从这一刻文件。

+0

也,我写了很多来自许多IP摄像机的视频流。所有的好:)对不起,我的英语。 – Eugene