I need to get PIDS from a sample MPEG-TS file

3k views Asked by At

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.

2

There are 2 answers

2
aergistal On BEST ANSWER

enter image description here

Consider a pointer p to the start of a TS packet. Check if the sync-byte p[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.

2
Christian G On

I recommend looking at two things:

  1. MPEG-2 TS specification, should be this one. This should give you a hint on how this information is packaged.

  2. FFMPEG source code via github. They have a MPEG TS parser and this should give you a hint on how you could get started.