請移步https://higoge.github.io/,全部下載資料在那個博客都能找到。謝謝。 git
--------------------------------------------------------------------github
本篇大部分仍是扣的live555的代碼。live555代碼中有計算ts的clock的值的,根據clock決定rtp的發包間隔。將clock收集起來,從最後一個clock減去第一個clock就能獲得TS文件的大概長度。
小程序
本小程序前提是視頻文件必須是TS文件,第一個字節是0x47,每一個TS包大小188,其他狀況未考慮。數組
/****************************************************************************** * Filename: ts_length.c * Created on: Mar 8, 2010 * Author: jeremiah * Description: 打印TS各PID的時間長度 * ******************************************************************************/ #include <stdio.h> #include <stdint.h> #define TS_SYNC_BYTE 0x47 #define TS_PACKET_SIZE 188 typedef struct { unsigned pid; double clock_begin; double clock_end; } pid_t; pid_t pid_array[8191]; // 褲子說一個ts最多有8191個pid。那就創建一個8191的數組。 unsigned char buf[TS_PACKET_SIZE]; void get_length(unsigned char* pkt); void store_pid(unsigned pid, double clock); int main(int argc, char **argv) { if (argc < 2) { fprintf(stderr, "please use %s <file_name>\n", argv[0]); return 1; } FILE *fp = fopen(argv[1], "rb"); if (!fp) { perror("fopen"); return 1; } fseek(fp, 0, SEEK_END); int size = ftell(fp); rewind(fp); while (size > 0) { int read_size = fread(buf, 1, sizeof(buf), fp); size -= read_size; get_length(buf); } int i; for (i = 0; i < 8191; i++) { if (pid_array[i].pid != 0) { printf("PID:0x%x length:%fs\n", pid_array[i].pid, pid_array[i].clock_end - pid_array[i].clock_begin); } else { break; } } return 0; } void get_length(unsigned char* pkt) { // Sanity check: Make sure we start with the sync byte: if (pkt[0] != TS_SYNC_BYTE) { fprintf(stderr, "Missing sync byte!\n"); return; } // If this packet doesn't contain a PCR, then we're not interested in it: uint8_t const adaptation_field_control = (pkt[3] & 0x30) >> 4; if (adaptation_field_control != 2 && adaptation_field_control != 3) { return; } // there's no adaptation_field uint8_t const adaptation_field_length = pkt[4]; if (adaptation_field_length == 0) { return; } // no PCR uint8_t const pcr_flag = pkt[5] & 0x10; if (pcr_flag == 0) { return; } // yes, we get a pcr uint32_t pcr_base_high = (pkt[6] << 24) | (pkt[7] << 16) | (pkt[8] << 8) | pkt[9]; // caculate the clock double clock = pcr_base_high / 45000.0; if ((pkt[10] & 0x80)) { clock += 1 / 90000.0; // add in low-bit (if set) } unsigned short pcr_extra = ((pkt[10] & 0x01) << 8) | pkt[11]; clock += pcr_extra / 27000000.0; unsigned pid = ((pkt[1] & 0x1F) << 8) | pkt[2]; store_pid(pid, clock); } void store_pid(unsigned pid, double clock) { int i; for (i = 0; i < 8191; i++) { if (pid == pid_array[i].pid) { break; } } if (i == 8191) { for (i = 0; i < 8191; i++) { if (pid_array[i].pid == 0) { break; } } pid_array[i].pid = pid; pid_array[i].clock_begin = clock; } else { pid_array[i].clock_end = clock; } }