streaming - 如何将RTP/H264流写入文件

标签 streaming ffmpeg h.264 rtp

我找不到 my previous question 的解决方案所以我决定一步步尝试。

我现在想做的是将RTP/H264流存储为文件。

到目前为止我发现的内容如下:

(首先我的RTP/H264是FU-A,它的形式是)

| RTP HEADER 12bytes long | FU INDICATOR 1byte | FU HEADER 1byte | FU payload |

根据我对 RFC 6184 文档的理解,我使用一个在 FU header 的第一位具有“1”的数据包开始 NAL,并附加在第一位设置“0”的后续数据包,直到最后一个数据包具有FU header 的第二位为“1”。

我认为这就是如何在 FU-A 打包之前获得完整的 NAL,而且我发现我需要将“起始位”(0x00000001)放在每个完整 NAL 的前面。

但到目前为止还没有运气。以下为部分日志

 ========= the new NAL is as 1400
 [0]0/0 [1]0/0 [2]0/0 [3]1/1 [4]7C/1111100 [5]85/10000101 [6]B8/10111000 [7]40/1000000 ...
 ========= adding the next NAL as 1400
 [0]0/0 [1]0/0 [2]0/0 [3]1/1 [4]7C/1111100 [5]85/10000101 [6]B8/10111000 [7]40/1000000 ...
 ========= adding the next NAL as 1400
 [0]0/0 [1]0/0 [2]0/0 [3]1/1 [4]7C/1111100 [5]85/10000101 [6]B8/10111000 [7]40/1000000 ...
 ========= adding the next NAL as 716
 [0]0/0 [1]0/0 [2]0/0 [3]1/1 [4]7C/1111100 [5]85/10000101 [6]B8/10111000 [7]40/1000000 ...
 ========= a NAL is summed up as 4866
 ========= the new NAL is as 1400
 [0]0/0 [1]0/0 [2]0/0 [3]1/1 [4]5C/1011100 [5]81/10000001 [6]E2/11100010 [7]20/100000 ...
 ========= adding the next NAL as 1400
 [0]0/0 [1]0/0 [2]0/0 [3]1/1 [4]5C/1011100 [5]81/10000001 [6]E2/11100010 [7]20/100000 ...
 ========= adding the next NAL as 1400
 [0]0/0 [1]0/0 [2]0/0 [3]1/1 [4]5C/1011100 [5]81/10000001 [6]E2/11100010 [7]20/100000 ...
 ========= adding the next NAL as 1400
 [0]0/0 [1]0/0 [2]0/0 [3]1/1 [4]5C/1011100 [5]81/10000001 [6]E2/11100010 [7]20/100000 ...
 ========= adding the next NAL as 1400
 [0]0/0 [1]0/0 [2]0/0 [3]1/1 [4]5C/1011100 [5]81/10000001 [6]E2/11100010 [7]20/100000 ...
 ========= adding the next NAL as 139
 [0]0/0 [1]0/0 [2]0/0 [3]1/1 [4]5C/1011100 [5]81/10000001 [6]E2/11100010 [7]20/100000 ...
 ========= a NAL is summed up as 7061
 ========= the new NAL is as 1377
 [0]0/0 [1]0/0 [2]0/0 [3]1/1 [4]41/1000001 [5]E4/11100100 [6]40/1000000 [7]1A/11010 ...
 ========= a NAL is summed up as 1369
 ========= the new NAL is as 1400
 [0]0/0 [1]0/0 [2]0/0 [3]1/1 [4]5C/1011100 [5]81/10000001 [6]E6/11100110 [7]60/1100000 ...
 ========= adding the next NAL as 94
 [0]0/0 [1]0/0 [2]0/0 [3]1/1 [4]5C/1011100 [5]81/10000001 [6]E6/11100110 [7]60/1100000 ...
 ========= a NAL is summed up as 1472
 ========= the new NAL is as 447
 [0]0/0 [1]0/0 [2]0/0 [3]1/1 [4]41/1000001 [5]E8/11101000 [6]80/10000000 [7]16/10110 ...
 ========= a NAL is summed up as 439
 ========= the new NAL is as 1400
 [0]0/0 [1]0/0 [2]0/0 [3]1/1 [4]5C/1011100 [5]81/10000001 [6]EA/11101010 [7]A0/10100000  ...
 ========= adding the next NAL as 1174
 [0]0/0 [1]0/0 [2]0/0 [3]1/1 [4]5C/1011100 [5]81/10000001 [6]EA/11101010 [7]A0/10100000 ...
 ========= a NAL is summed up as 2552
 ========= the new NAL is as 1400
 [0]0/0 [1]0/0 [2]0/0 [3]1/1 [4]5C/1011100 [5]81/10000001 [6]EC/11101100 [7]C0/11000000 ...
 ========= adding the next NAL as 1364
 [0]0/0 [1]0/0 [2]0/0 [3]1/1 [4]5C/1011100 [5]81/10000001 [6]EC/11101100 [7]C0/11000000 ...
 ========= a NAL is summed up as 2742
 ========= the new NAL is as 1400
 ...

我的问题是,

  1. 如果我可以通过 FU-A 从碎片数据包中获取完整的 NAL,我怎样才能将其制作为能够由 VLC 或其他播放器运行的文件?

  2. 我仍然很困惑是否必须保留 FU 指示符和 FU header 。有人说我只需要在第一个数据包中使用它们(从 FU header 处的“1”开始)

任何建议将不胜感激。

谢谢。

最佳答案

FU、STAP 和 MTAP NAL 单元仅特定于 RTP 分组化,因为它们旨在促进网络传输。换句话说,不要指望解码器能够正确解析它们。最终,您需要重新组装 NAL 单元(如 FU 数据包),或者将它们分解为多个 NAL 单元(如 STAP/MTAP)。

一旦您拥有 NAL 单元(包括 PPS、SPS、SEI、切片分区以及 1-23 范围内的所有其他类型),您就可以将每个单元的“0001”起始代码写入磁盘H.264附件B。

可以使用各种命令行工具将 H.264 附件 B 流放入 MPEG-4 等容器中(我很确定 ffmpeg 可以做到)。

关于streaming - 如何将RTP/H264流写入文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9581778/

相关文章:

swift - 如何使用ffmpeg将MOV文件头添加到原始数据写入文件中?

iphone - iPhone 上的流式 SAX XML 处理

java - 如何使用 java(在 Tomcat 上)流式传输和转码媒体文件?

node.js - 管道到 spawn stdin

使用 FFmpeg 编码的视频播放速度过快

timestamp - 如何修复不正确的时间戳计算? [OpenRtspClient]

android - Android 应用程序中 VideoView 中的 RTSP 流

ffmpeg - 组合两个 ffmpeg 命令(片段和静态照片)

audio - 如何从音频中删除一个部分并使用 ffmpeg 保留其余部分

Android 内置相机编码器 VS FFMPEG --- 速度