2013-12-11 284 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