[英]Saving frames from a network camera (RTSP) to a mp4 file
我對如何將視頻流保存到mp4文件中感到非常困惑。 我正在使用ffmpeg。 讓我解釋一下這個問題:
問題是,如果我嘗試使用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.