【问题标题】:Convert h.264 stream obtained from rtp session to video file using gstreamer使用 gstreamer 将从 rtp 会话获得的 h.264 流转换为视频文件
【发布时间】:2020-05-30 06:16:07
【问题描述】:

我需要播放包含 rtp 流的 *.rtpdump 或 *.pcap 文件中的视频。我正确解析 rtp 包并从中获取有效负载。我可以使用 gstreamer 或其他工具将其转换为正确的 h.264 格式吗?

【问题讨论】:

  • 请提供一些你到目前为止所做的代码
  • 我只是使用rtptool等开源工具解析得到的*.rtpdump。我稍微修改一下以获得清晰的 h264 有效负载。现在我需要转换该流。
  • @Günel 代码如下
  • 您也可以考虑使用 gst-launch-1.0 从命令行进行解析,这样可能会帮助您入门:gst-launch-1.0 -e filesrc location=filename ! rtph264depay!文件接收器位置=输出文件

标签: gstreamer h.264 rtp


【解决方案1】:
void H264parser::parse(const rtp_info &rtpInfo, const unsigned char *data, size_t size) {
    const int type_fu_a = 28;
    const int type_stap_a = 24;
    nalu_header nalu;
    nalu.f = static_cast<uint8_t>((data[0] >> 7) & 0x1);
    nalu.nri = static_cast<uint8_t>((data[0] >> 5) & 0x3);
    nalu.type = static_cast<uint8_t>(data[0] & 0x1f);

    unsigned char identifier[4] = {0, 0, 0, 1};  // H.264 identifier.
    if (nalu.type == type_fu_a) {                  // fu_a
        fu_a_header fu_a;
        fu_a.s = static_cast<uint8_t>((data[1] >> 7) & 0x1);
        fu_a.e = static_cast<uint8_t>((data[1] >> 6) & 0x1);
        fu_a.r = static_cast<uint8_t>((data[1] >> 5) & 0x1);
        fu_a.type = static_cast<uint8_t>((data[1]) & 0x1f);
        // fprintf(stdout, "fu_a: s = %d, e = %d, type = %d\n", fu_a.s, fu_a.e, fu_a.type);
        if (fu_a.s == 1) {
            assert(cache.size() == 0);
            cache.add(identifier, 4);
            uint8_t reassembled_nalu = 0;
            reassembled_nalu |= (nalu.f << 7);
            reassembled_nalu |= (nalu.nri << 5);
            reassembled_nalu |= (fu_a.type);
            cache.add(&reassembled_nalu, 1);
        }
        data += 2;
        size -= 2;
        cache.add(data, size);
        if (fu_a.e == 1) {
            FILE *file = fopen("test.h264", "ab");
            fwrite(cache.get(), cache.size(), 1, file);
            fclose(file);
            cache.reset();
        }
    } else if (nalu.type == type_stap_a) {
        fprintf(stdout, "not supported yet\n");
    } else {   // NALU in a single packet
        cache.add(identifier, 4);
        cache.add(data, size);
        FILE *file = fopen("test.h264", "ab");
        fwrite(cache.get(), cache.size(), 1, file);
        fclose(file);
        cache.reset();
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-06-23
    • 2010-09-14
    • 2013-09-22
    • 2013-05-01
    • 2012-07-17
    • 2010-12-22
    • 2023-03-17
    • 1970-01-01
    相关资源
    最近更新 更多