以下为当时做的一个功能so源码,对博主的原文做了简单的修改然后调用了其中一个接口,如果要在此源码上实现该功能,只需要将GetFileTime方法修改成main方法即可,也可以对此源码进行编译成so库,使用库连接也可实现功能:
原文参考:http://jeremiah.blog.51cto.com/539865/281885
/*
* Content: Get ts format files time length
* Time : 2017-04-06
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <stdint.h>
#define TS_SYNC_BYTE 0x47
#define TS_PACKET_SIZE 188
typedef struct{
unsigned int pid;
double clock_begin;
double clock_end;
}Pid_s;
Pid_s pid_array[8191];
unsigned char buf[TS_PACKET_SIZE];
long int GetFileTime(const char* _file);
void get_length(unsigned char* pkt);
void store_pid(unsigned int pid,double clock);
long int GetFileTime(const char* _file){
if (_file == NULL){
printf("File path is null,please check it!\n");
return -1;
}
FILE* fp = fopen(_file,"rb");
if (!fp){
//printf("can not open TS file,please check it!\n");
return;
}
fseek(fp,0,SEEK_END);
int Size = ftell(fp);
rewind(fp);
while(Size > 0){
int ReadSize = fread(buf,1,sizeof(buf),fp);
Size -= ReadSize;
get_length(buf);
}
int i = 0;
double tmp = 0;
for (i = 0;i < 8191;i++){
if (pid_array[i].pid != 0){
tmp += (pid_array[i].clock_end - pid_array[i].clock_begin);
}
else{
break;
}
}
fclose(fp);
return AddSum;
}
void get_length(unsigned char* pkt){
if (pkt[0] != TS_SYNC_BYTE){
//fprintf(stderr,"Missing sync byte!\n");
return;
}
//check this file is have PCR
uint8_t const adaptation_field_control = (pkt[3] & 0x30) >> 4;
if (adaptation_field_control != 2 && adaptation_field_control != 3){
//printf("This file is not have PCR!\n");
return;
}
uint8_t const adaptation_field_length = pkt[4];
if (adaptation_field_length == 0){
return;
}
uint8_t const pcr_flag = pkt[5] & 0x10;
if (pcr_flag == 0){
return;
}
//have a PCR
uint32_t pcr_base_high = (pkt[6] << 24) | (pkt[7] << 16) | (pkt[8] << 8) | pkt[9];
double clock = pcr_base_high / 45000.0;
if (pkt[10]&0x80){
clock += 1 / 90000.0;
}
unsigned short pcr_extra = (pkt[10]&0x01) << 8 | pkt[11];
clock += pcr_extra / 27000000.0;
unsigned int pid = ((pkt[1]&0x1F) << 8) | pkt[2];
store_pid(pid,clock);
}
void store_pid(unsigned int pid,double clock){
int i = 0;
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;
}
}
本文出自 “海狗哥的流媒体空间” 博客,请务必保留此出处http://jeremiah.blog.51cto.com/539865/281885