将网络摄像机(RTSP)中的帧保存到mp4文件中
Saving frames from a network camera (RTSP) to a mp4 file
我很困惑如何将视频流保存到mp4文件中。我正在使用ffmpeg。让我来解释一下问题:
- 我使用avformat_open_input()、avformat_find_stream_info()、av_read_play()通过RTSP(H.264流)连接到网络摄像机,并使用av_read_frame()获取帧
- 每次我用av_read_frame()获得一个帧时,我都会将相应的AVPacket存储在一个循环缓冲区中
- 在我的应用程序中的某些地方,会选择一个循环缓冲区的范围。我找到了一个用来开始的关键帧
- 一旦我有了一个从关键帧开始的AVPacket列表,我就会写标题、帧和尾部,正如我在下面的代码中所描述的那样
问题是,如果我尝试使用VLC、Windows Media Player或其他视频观看,那么生成的mp4视频会出现瑕疵。
我还意识到,数据包的pts不是连续的,而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);
第二:数据包开始到来后,等待第一个关键帧,并从此时开始写入文件。
相关文章:
- 在全局变量中保存类的实例以重新创建类(创建"backup")
- 如何在选项卡视图Qt中设置一个新项目,并保存以前的项目
- 如何使用OpenCV将RBG图像转换为HSV,并将H、S和V值保存为C++中的3个独立图像
- 将字符指针十六进制转换为字符串并保存在文本文件C++中
- 在没有定义返回类型的函数中返回布尔值,并将结果保存在无错误的char编译中-为什么
- EvtExportLogneneneba API正在将远程计算机的事件日志保存到远程PC本身.如何将其保存到主机
- 在C++中将类(带有Vector成员)保存为二进制文件
- C++.我想将更改后的特定字符行保存在字符串中
- 使用FFMPEG将RGB图像序列保存到.mp4时出现问题
- 将RGB图像保存为PPM格式
- 哈希文件递归并保存到矢量Cryptopp中
- 如何将二进制格式的 C++ 对象的 std::vector 保存到磁盘?
- 无法通过头文件保存变量的值
- 我们可以将数据永久保存为数据结构吗?
- 如何使用OpenCV-C++编写*.mp4视频?
- 我的程序有一个保存配置文件的GUI,如何双击此配置文件以直接加载带有配置数据的GUI?
- 如何使用Qt多媒体和C++将.mp4视频从OpenGL纹理中保存出来
- 在Ubuntu中使用VideOwriter(OpenCV)保存视频作为MP4时出错
- 将网络摄像机(RTSP)中的帧保存到mp4文件中
- Ffmpeg:如何保存h264原始数据为mp4文件