//readmpls.cpp #include #include #include #include //function prototypes unsigned int BinGetInt(FILE * fp, int fl); int DisplayTIME(unsigned int pts, char * title); int IsChapter(FILE * fp, int fl); int ErrorFunc(const char * msg); int main(int argc, char *argv[]) //main calling function { FILE * mpls; unsigned int tin; unsigned int tout; unsigned int dur; unsigned int chapt[100]; //check for correct usage bail if not correct if(argc!=2) { ErrorFunc("USAGE: fixmpls_console.exe "); } //open file for binary read... bail if not opened properly mpls = fopen(argv[1], "rb"); if (mpls == NULL) { ErrorFunc("Could not open file"); } //get file size (num of bytes) long mplsSize; fseek(mpls , 0 , SEEK_END); mplsSize = ftell (mpls); rewind(mpls); printf("File Size: %d bytes\n\n",mplsSize); //get TimeIn, TimeOut, and Duration tin=BinGetInt(mpls, 82); //get TimeIN from file tout=BinGetInt(mpls, 86); //get TimeOut from file dur = tout-tin; //duration of playlist DisplayTIME(tin,"Time_IN"); DisplayTIME(tout,"Time_OUT"); DisplayTIME(dur,"Duration"); printf("\n"); //get Chapter timestamp information int b; int c=0; char chnum[7]; for(b=90;b<(mplsSize-1);b++) { if(IsChapter(mpls, b)==1) { chapt[c]=BinGetInt(mpls, b-4); sprintf(chnum,"CH %d",c); DisplayTIME(chapt[c],chnum); c++; } } fclose(mpls); //close mpls for reading //wait for user input and close printf("\n\nPress any key to CLOSE"); while(getchar()==0){} return 0; } unsigned int BinGetInt(FILE * fp, int fl) // subfunction to return unsigned int from 4 bytes of data from file pointer fp, at byte location fl { unsigned char bin_char[4]; unsigned int bin_int; fseek(fp, fl, SEEK_SET); //set file location to the beginning of the integer //int br=fscanf(fp,"%4c",&bin_char[0]); //int br=fscanf(fp,"%c%c%c%c",&bin_char[0],&bin_char[1],&bin_char[2],&bin_char[3]); //int br=fscanf(fp,"%u", &bin_int); //int br=fread(&bin_int, 4, 1, fp); //read bytes of data into a char array //bin_int=fgetc(fp); //br=br+bin_char[1]=fgetc(fp); //br=br+bin_char[2]=fgetc(fp); //br=br+bin_char[3]=fgetc(fp); int br=fread(&bin_char, 1, 4, fp); //read bytes of data into a char array bin_int=bin_char[0]<<24|bin_char[1]<<16|bin_char[2]<<8|bin_char[3]<<0; //convert char array to int if(br!=4) //if read function does not read 4 bytes of data, function failed, return NULL { fclose(fp); printf("\n\nOffset: %d (HEX: %X)\nBytesRead: %d", fl, fl, br); printf("\nHEX: %.2X %.2X %.2X %.2X", bin_char[0], bin_char[1], bin_char[2], bin_char[3]); printf("\nINT: %u", bin_int); ErrorFunc("Could not read data in BinGetInt"); } return bin_int; //return integer } int DisplayTIME(unsigned int pts, char *title) //subfunction to print out the PTS (unsigned in pts) data that is user readable { float sec=pts/45000.00; printf("\n%-8s",title); // printf("%-12u", pts); // printf("%-12.3f", sec); printf(" %02.0f:%02.0f:%06.3f",floor(sec/3600),floor(fmod(sec,3600)/60),fmod(sec,60)); return 0; } int IsChapter(FILE * fp, int fl) //subfunction to determine if bytes signify chapter timestamp //returns 1 if true, 0 otherwise { unsigned char ctemp[8]; char stemp[10]; fseek(fp,fl,SEEK_SET); fread(&ctemp, 1, 4, fp); sprintf(stemp, "%.2X%.2X%.2X%.2X",ctemp[0], ctemp[1],ctemp[2],ctemp[3]); if(strcmp(stemp,"FFFF0000\0")==0) { return 1; } else { return 0; } } int ErrorFunc(const char * msg) { printf("\n\n!!!!ERROR!!!!\n\n%s\n\nPress any key to EXIT",msg); while(getchar()==0){} exit(1); }