分离ts媒体文件中每个pid的数据

ts的包大小:188字节
ts包的开始字节:0x47
TS包头定义:
包头定义转载自:http://blog.csdn.net/shuyong1999/article/details/7095032

typedef struct TS_packet_header
{
    unsigned sync_byte                        : 8; //同步字节, 固定为0x47,表示后面的是一个TS分组
    unsigned transport_error_indicator        : 1; //传输误码指示符
    unsigned payload_unit_start_indicator    : 1; //有效荷载单元起始指示符

    unsigned transport_priority              : 1; //传输优先, 1表示高优先级,传输机制可能用到,解码用不着
    unsigned PID                            : 13; //PID
    unsigned transport_scrambling_control    : 2; //传输加扰控制 
    unsigned adaption_field_control            : 2; //自适应控制 01仅含有效负载,10仅含调整字段,11含有调整字段和有效负载。为00解码器不进行处理
    unsigned continuity_counter                : 4; //连续计数器 一个4bit的计数器,范围0-15
} TS_packet_header;

因为要分离出ts文件中每个pid的数据,所以,最重要的当然是先获取pid的值:

int pid = ((buf[1] & 0x1f) << 8) + buf[2];

其它就是逻辑问题了,详细代码如下:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#include <iostream>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <map>

using namespace std;

#define TS_SIZE 188

bool dir_exists (const string &dir)
{
     struct stat statx = {0};
     int ret = stat (dir.c_str(), &statx);
     if (ret == 0) {
         if (S_ISDIR (statx.st_mode))
             return true;
     }
     return false;
}

string i2str(int i)
{
    char buffer[20];
    sprintf (buffer, "%d", i);
    return buffer;
}

int main(int argc, char **argv)
{
    if (argc != 3) {
        cout << argv[0] << " ts_file save_dir" << endl;
        return 0;
    }
    const string ts_file = argv[1];
    const string save_dir = argv[2];

    if (!dir_exists (save_dir)) {
        mkdir (save_dir.c_str (), 0777);
    }

    FILE *stream = NULL; //to fopen input ts file
    FILE *save_file_fp = NULL;// to fopen save ts file

    unsigned char buf[TS_SIZE];

    int len;
    string pid_file = save_dir + "/pid.file"; //init ts save file

    if ((stream = fopen (ts_file.c_str (), "rb"))== NULL) {
        fclose (stream);
        cout << "ts file open error" << endl;
        return -1;
    }

    while (!feof (stream)) {
        len = fread (buf, sizeof (unsigned char), TS_SIZE, stream);
        cout << "read len:" << len << endl;

        if (buf[0] != 0x47) {
            cout << "Sync byte of TS isn't 0x47!, skip it." << endl;
            //long curpos = ftell (stream);
            fseek (stream, (1 - TS_SIZE), SEEK_CUR);
            continue;
        }

        int pid = ((buf[1] & 0x1f) << 8) + buf[2];
        cout << "pid: " << pid << endl;

        pid_file = save_dir + "/" + i2str(pid) + ".file";
        save_file_fp = fopen (pid_file.c_str (), "ab");
        len = fwrite (buf, sizeof (unsigned char), TS_SIZE, save_file_fp);
        cout << "write len:" << len << endl;
        fclose (save_file_fp);
    }

    fclose (stream);
    return 0;
}
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
提供的源码资源涵盖了安卓应用、小程序、Python应用和Java应用等多个领域,每个领域都包含了丰富的实例和项目。这些源码都是基于各自平台的最新技术和标准编写,确保了在对应环境下能够无缝运行。同时,源码配备了详细的注释和文档,帮助用户快速理解代码结构和实现逻辑。 适用人群: 这些源码资源特别适合大学生群体。无论你是计算机相关专业的学生,还是对其他领域编程感兴趣的学生,这些资源都能为你提供宝贵的学习和实践机会。通过学习和运行这些源码,你可以掌握各平台开发的基础知识,提升编程能力和项目实战经验。 使用场景及目标: 在学习阶段,你可以利用这些源码资源进行课程实践、课外项目或毕业设计。通过分析和运行源码,你将深入了解各平台开发的技术细节和最佳实践,逐步培养起自己的项目开发和问题解决能力。此外,在求职或创业过程,具备跨平台开发能力的大学生将更具竞争力。 其他说明: 为了确保源码资源的可运行性和易用性,特别注意了以下几点:首先,每份源码都提供了详细的运行环境和依赖说明,确保用户能够轻松搭建起开发环境;其次,源码的注释和文档都非常完善,方便用户快速上手和理解代码;最后,我会定期更新这些源码资源,以适应各平台技术的最新发展和市场需求。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值