簡體   English   中英

將幀從網絡攝像機(RTSP)保存到mp4文件

[英]Saving frames from a network camera (RTSP) to a mp4 file

我對如何將視頻流保存到mp4文件中感到非常困惑。 我正在使用ffmpeg。 讓我解釋一下這個問題:

  1. 我使用avformat_open_input(),avformat_find_stream_info(),av_read_play()通過RTSP(H.264流)連接到網絡攝像機,並使用av_read_frame()獲得幀。
  2. 每次使用av_read_frame()獲取一幀時,我都會將相應的AVPacket存儲在循環緩沖區中。
  3. 在我的應用程序中的某些地方,選擇了此循環緩沖區的范圍。 我找到了一個關鍵的起點。
  4. 有了從關鍵幀開始的AVPacket列表后,就可以編寫標題,幀和尾部,如下面在代碼中所述。

問題是,如果我嘗試使用VLC,Windows Media Player或其他播放器觀看最終的mp4視頻,則該視頻會帶有偽影。

我還意識到,這些數據包的pt是不連續的,而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);

非常感謝,

第一:使用相機時,最好通過TCP上的RTP(TCP作為傳輸協議)進行操作。 要啟用此功能:

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

第二:數據包開始發送之后,請等待第一個關鍵幀並從此刻開始寫入文件。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM