Jeremiah的小程序之二:获取TS文件视频长度

本篇大部分还是扣的live555的代码。live555代码中有计算ts的clock的值的,根据clock决定rtp的发包间隔。将clock收集起来,从最后一个clock减去第一个clock就能得到TS文件的大概长度。

    本小程序前提是视频文件必须是TS文件,第一个字节是0x47,每个TS包大小188,其余情况未考虑。

 
 
  1. /******************************************************************************
  2.  * Filename:        ts_length.c  
  3.  * Created on:      Mar 8, 2010  
  4.  * Author:          jeremiah  
  5.  * Description:     打印TS各PID的时间长度  
  6.  *  
  7.  ******************************************************************************/ 
  8.  
  9. #include <stdio.h>  
  10. #include <stdint.h>  
  11.  
  12. #define TS_SYNC_BYTE 0x47  
  13. #define TS_PACKET_SIZE 188  
  14.  
  15. typedef struct {  
  16.     unsigned pid;  
  17.     double clock_begin;  
  18.     double clock_end;  
  19. } pid_t;  
  20.  
  21. pid_t pid_array[8191]; // 裤子说一个ts最多有8191个pid。那就建立一个8191的数组。  
  22. unsigned char buf[TS_PACKET_SIZE];  
  23.  
  24. void get_length(unsigned char* pkt);  
  25. void store_pid(unsigned pid, double clock);  
  26.  
  27. int main(int argc, char **argv) {  
  28.     if (argc < 2) {  
  29.         fprintf(stderr, "please use %s <file_name>/n", argv[0]);  
  30.         return 1;  
  31.     }  
  32.  
  33.     FILE *fp = fopen(argv[1], "rb");  
  34.     if (!fp) {  
  35.         perror("fopen");  
  36.         return 1;  
  37.     }  
  38.     fseek(fp, 0, SEEK_END);  
  39.     int size = ftell(fp);  
  40.     rewind(fp);  
  41.  
  42.     while (size > 0) {  
  43.         int read_size = fread(buf, 1, sizeof(buf), fp);  
  44.         size -= read_size;  
  45.         get_length(buf);  
  46.     }  
  47.  
  48.     int i;  
  49.     for (i = 0; i < 8191; i++) {  
  50.         if (pid_array[i].pid != 0) {  
  51.             printf("PID:0x%x length:%fs/n", pid_array[i].pid,   
  52.                 pid_array[i].clock_end - pid_array[i].clock_begin);  
  53.         } else {  
  54.             break;  
  55.         }  
  56.     }  
  57.     return 0;  
  58. }  
  59.  
  60. void get_length(unsigned char* pkt) {  
  61.     // Sanity check: Make sure we start with the sync byte:  
  62.     if (pkt[0] != TS_SYNC_BYTE) {  
  63.         fprintf(stderr, "Missing sync byte!/n");  
  64.         return;  
  65.     }  
  66.  
  67.     // If this packet doesn't contain a PCR, then we're not interested in it:  
  68.     uint8_t const adaptation_field_control = (pkt[3] & 0x30) >> 4;  
  69.     if (adaptation_field_control != 2 && adaptation_field_control != 3) {  
  70.         return;  
  71.     }  
  72.  
  73.     // there's no adaptation_field  
  74.     uint8_t const adaptation_field_length = pkt[4];  
  75.     if (adaptation_field_length == 0) {  
  76.         return;  
  77.     }  
  78.  
  79.     // no PCR  
  80.     uint8_t const pcr_flag = pkt[5] & 0x10;  
  81.     if (pcr_flag == 0) {  
  82.         return;  
  83.     }  
  84.  
  85.     // yes, we get a pcr  
  86.     uint32_t pcr_base_high = (pkt[6] << 24) | (pkt[7] << 16) | (pkt[8] << 8)  
  87.                         | pkt[9];  
  88.     // caculate the clock  
  89.     double clock = pcr_base_high / 45000.0;  
  90.     if ((pkt[10] & 0x80)) {  
  91.         clock += 1 / 90000.0; // add in low-bit (if set)  
  92.     }  
  93.     unsigned short pcr_extra = ((pkt[10] & 0x01) << 8) | pkt[11];  
  94.     clock += pcr_extra / 27000000.0;  
  95.  
  96.     unsigned pid = ((pkt[1] & 0x1F) << 8) | pkt[2];  
  97.     store_pid(pid, clock);  
  98. }  
  99.  
  100. void store_pid(unsigned pid, double clock) {  
  101.     int i;  
  102.     for (i = 0; i < 8191; i++) {  
  103.         if (pid == pid_array[i].pid) {  
  104.             break;  
  105.         }  
  106.     }  
  107.     if (i == 8191) {  
  108.         for (i = 0; i < 8191; i++) {  
  109.             if (pid_array[i].pid == 0) {  
  110.                 break;  
  111.             }  
  112.         }  
  113.         pid_array[i].pid = pid;  
  114.         pid_array[i].clock_begin = clock;  
  115.     } else {  
  116.         pid_array[i].clock_end = clock;  
  117.     }  

 

 

本文出自 “海狗哥的流媒体空间” 博客,请务必保留此出处http://jeremiah.blog.51cto.com/539865/281885

 

原文地址有把程序源码作为附件供下载。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
保存训练好的支持向量机模型的步骤如下:首先,根据SVM的主要步骤,我们需要对训练好的模型进行保存。这可以通过将模型参数和相关信息保存到文件中来实现。具体而言,可以使用Python中的pickle模块将模型对象保存为二进制文件。pickle模块提供了一种方便的方式来序列化和反序列化Python对象。通过将训练好的SVM模型对象保存为文件,我们可以在以后的预测任务中加载该文件并使用该模型进行预测。这样可以避免每次都重新训练模型,提高了预测的效率。因此,保存训练好的支持向量机模型可以通过使用pickle模块将模型对象保存为文件来实现。\[1\] #### 引用[.reference_title] - *1* *2* [支持向量机识别数字集(数据采集+模型训练+预测输出)](https://blog.csdn.net/weixin_47407066/article/details/123843368)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] - *3* [机器学习——支持向量机模型](https://blog.csdn.net/Jeremiah_/article/details/120700736)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值