/* A simple filter to extract multiple streams from a multiplexed TS. */ #include #include #include #include #include int main(int argc, char **argv) { int pid,n; int filters[8192]; unsigned int i=0; unsigned int j=0; unsigned int k=0; unsigned int l=0; // char buf[188]; unsigned char buf[188]; unsigned char my_cc[8192]; int errors=0; int nb_bytes=0; char *entree_filename = NULL; char *sortie_filename = NULL; FILE *sortie,*entree; int c,option_index=0; char eitbuf[4096]; const char short_options[] = "e:s:"; const struct option long_options[] = { {"entree", required_argument, NULL, 'e'}, {"sortie", required_argument, NULL, 's'}, {0, 0, 0, 0} }; while (1) { c = getopt_long (argc, argv, short_options, long_options, &option_index); if (c == -1) { break; } switch (c) { case 'e': entree_filename = malloc (strlen (optarg) + 1); strncpy (entree_filename, optarg, strlen (optarg) + 1); break; case 's': sortie_filename = malloc (strlen (optarg) + 1); strncpy (sortie_filename, optarg, strlen (optarg) + 1); break; case 'h': break; } } if (optind < argc) { fprintf(stderr,"Too few arguments\n Usage : extract_pid -e file_in -s file_out"); exit(1); } if (!entree_filename || !sortie_filename) { fprintf(stderr,"You must specify filenames\n"); exit(2); } entree=fopen(entree_filename, "r"); if (!entree) { fprintf (stderr, "%s: %s\n", entree_filename, strerror (errno)); exit(3); } sortie=fopen(sortie_filename, "w"); if (!entree) { fprintf (stderr, "%s: %s\n", sortie_filename, strerror (errno)); exit(3); } for (i=0;i<8192;i++) { filters[i]=0; my_cc[i]=0xff;} n=fread(buf,1,188,entree); i=0; while (n==188&&i<2000000) { if (buf[0]!=0x47) { // TO DO: Re-sync. fprintf(stderr,"FATAL ERROR IN STREAM AT PACKET %d\n",i); // exit; } pid=(((buf[1] & 0x1f) << 8) | buf[2]); if (my_cc[pid]==0xff) my_cc[pid]=buf[3]&0x0f; //MOdifiy Here to choos your pids if((pid==0)||(pid==18)) { fwrite(buf,1,188,sortie); j++; } filters[pid]++; n=fread(buf,1,188,entree); i++; } fclose(sortie); fprintf(stderr,"Read %d packets, wrote %d.\n",i,j); fprintf(stderr,"%d incontinuity errors.\n",errors); return(0); }