I need to get PIDS from a sample MPEG-TS file,I have tried by reading the file using fopen()
and got the data in hex format. Now I am stuck in finding the PID bytes in the whole data. Can anyone help me out?
I have used the below code:
#include <stdio.h>
#include <string.h>
void main()
{
FILE *myfile;
FILE *output;
int i=0,j;
unsigned int buffer;
int o;
myfile=fopen("screen.ts","rb");
output = fopen("output2.txt","w");
do{
o=fread(&buffer, 2, 1, myfile);
if(o!=1)
break;
printf("%d: ",i);
printf("%x\n",buffer);
fprintf(output,"%x ",buffer);
i++;
}while(1);
}
I got the data from the file, now I need to locate the "PID" bytes in the data.
Consider a pointer
p
to the start of a TS packet. Check if the sync-bytep[0] == 0x47
.The PID is a 13-bit unsigned integer which you can store in a
uint16_t
and which is equal to((p[1] & 0x1f) << 8) | p[2]
.Increment the pointer by the size of a TS packet which is typically 188 bytes.
Repeat.