📄 subreader.c
字号:
f_fname_noext = (char*)malloc(len); f_fname_trim = (char*)malloc(len); tmp_fname_noext = (char*)malloc(len); tmp_fname_trim = (char*)malloc(len); tmp_fname_ext = (char*)malloc(len); tmpresult = (char*)malloc(len); result = (subfn*)malloc(sizeof(subfn)*MAX_SUBTITLE_FILES); memset(result, 0, sizeof(subfn)*MAX_SUBTITLE_FILES); subcnt = 0; tmp = strrchr(fname,'/');#ifdef WIN32 if(!tmp)tmp = strrchr(fname,'\\');#endif // extract filename & dirname from fname if (tmp) { strcpy(f_fname, tmp+1); pos = tmp - fname; strncpy(f_dir, fname, pos+1); f_dir[pos+1] = 0; } else { strcpy(f_fname, fname); strcpy(f_dir, "./"); } strcpy_strip_ext(f_fname_noext, f_fname); strcpy_trim(f_fname_trim, f_fname_noext); tmp_sub_id = NULL; if (dvdsub_lang && !whiteonly(dvdsub_lang)) { tmp_sub_id = (char*)malloc(strlen(dvdsub_lang)+1); strcpy_trim(tmp_sub_id, dvdsub_lang); } // 0 = nothing // 1 = any subtitle file // 2 = any sub file containing movie name // 3 = sub file containing movie name and the lang extension for (j = 0; j <= 1; j++) { d = opendir(j == 0 ? f_dir : path); if (d) { while ((de = readdir(d))) { // retrieve various parts of the filename strcpy_strip_ext(tmp_fname_noext, de->d_name); strcpy_get_ext(tmp_fname_ext, de->d_name); strcpy_trim(tmp_fname_trim, tmp_fname_noext); // does it end with a subtitle extension? found = 0;#ifdef USE_ICONV#ifdef HAVE_ENCA for (i = ((sub_cp && strncasecmp(sub_cp, "enca", 4) != 0) ? 3 : 0); sub_exts[i]; i++) {#else for (i = (sub_cp ? 3 : 0); sub_exts[i]; i++) {#endif#else for (i = 0; sub_exts[i]; i++) {#endif if (strcasecmp(sub_exts[i], tmp_fname_ext) == 0) { found = 1; break; } } // we have a (likely) subtitle file if (found) { int prio = 0; if (!prio && tmp_sub_id) { sprintf(tmpresult, "%s %s", f_fname_trim, tmp_sub_id); printf("dvdsublang...%s\n", tmpresult); if (strcmp(tmp_fname_trim, tmpresult) == 0 && sub_match_fuzziness >= 1) { // matches the movie name + lang extension prio = 5; } } if (!prio && strcmp(tmp_fname_trim, f_fname_trim) == 0) { // matches the movie name prio = 4; } if (!prio && (tmp = strstr(tmp_fname_trim, f_fname_trim)) && (sub_match_fuzziness >= 1)) { // contains the movie name tmp += strlen(f_fname_trim); if (tmp_sub_id && strstr(tmp, tmp_sub_id)) { // with sub_id specified prefer localized subtitles prio = 3; } else if ((tmp_sub_id == NULL) && whiteonly(tmp)) { // without sub_id prefer "plain" name prio = 3; } else { // with no localized subs found, try any else instead prio = 2; } } if (!prio) { // doesn't contain the movie name // don't try in the mplayer subtitle directory if ((j == 0) && (sub_match_fuzziness >= 2)) { prio = 1; } } if (prio) { prio += prio;#ifdef USE_ICONV if (i<3){ // prefer UTF-8 coded prio++; }#endif sprintf(tmpresult, "%s%s", j == 0 ? f_dir : path, de->d_name);// fprintf(stderr, "%s priority %d\n", tmpresult, prio); if ((f = fopen(tmpresult, "rt"))) { fclose(f); result[subcnt].priority = prio; result[subcnt].fname = strdup(tmpresult); subcnt++; } } } if (subcnt >= MAX_SUBTITLE_FILES) break; } closedir(d); } } if (tmp_sub_id) free(tmp_sub_id); free(f_dir); free(f_fname); free(f_fname_noext); free(f_fname_trim); free(tmp_fname_noext); free(tmp_fname_trim); free(tmp_fname_ext); free(tmpresult); qsort(result, subcnt, sizeof(subfn), compare_sub_priority); result2 = (char**)malloc(sizeof(char*)*(subcnt+1)); memset(result2, 0, sizeof(char*)*(subcnt+1)); for (i = 0; i < subcnt; i++) { result2[i] = result[i].fname; } result2[subcnt] = NULL; free(result); return result2;}void list_sub_file(sub_data* subd){ int i,j; subtitle *subs = subd->subtitles; for(j=0; j < subd->sub_num; j++){ subtitle* egysub=&subs[j]; printf ("%i line%c (%li-%li)\n", egysub->lines, (1==egysub->lines)?' ':'s', egysub->start, egysub->end); for (i=0; i<egysub->lines; i++) { printf ("\t\t%d: %s%s", i,egysub->text[i], i==egysub->lines-1?"":" \n "); } printf ("\n"); } printf ("Subtitle format %s time.\n", subd->sub_uses_time ? "uses":"doesn't use"); printf ("Read %i subtitles, %i errors.\n", subd->sub_num, subd->sub_errs);}void dump_srt(sub_data* subd, float fps){ int i,j; int h,m,s,ms; FILE * fd; subtitle * onesub; unsigned long temp; subtitle *subs = subd->subtitles; if (!subd->sub_uses_time && sub_fps == 0) sub_fps = fps; fd=fopen("dumpsub.srt","w"); if(!fd) { perror("dump_srt: fopen"); return; } for(i=0; i < subd->sub_num; i++) { onesub=subs+i; //=&subs[i]; fprintf(fd,"%d\n",i+1);//line number temp=onesub->start; if (!subd->sub_uses_time) temp = temp * 100 / sub_fps; temp -= sub_delay * 100; h=temp/360000;temp%=360000; //h =1*100*60*60 m=temp/6000; temp%=6000; //m =1*100*60 s=temp/100; temp%=100; //s =1*100 ms=temp*10; //ms=1*10 fprintf(fd,"%02d:%02d:%02d,%03d --> ",h,m,s,ms); temp=onesub->end; if (!subd->sub_uses_time) temp = temp * 100 / sub_fps; temp -= sub_delay * 100; h=temp/360000;temp%=360000; m=temp/6000; temp%=6000; s=temp/100; temp%=100; ms=temp*10; fprintf(fd,"%02d:%02d:%02d,%03d\n",h,m,s,ms); for(j=0;j<onesub->lines;j++) fprintf(fd,"%s\n",onesub->text[j]); fprintf(fd,"\n"); } fclose(fd); mp_msg(MSGT_SUBREADER,MSGL_INFO,"SUB: Subtitles dumped in \'dumpsub.srt\'.\n");}void dump_mpsub(sub_data* subd, float fps){ int i,j; FILE *fd; float a,b; subtitle *subs = subd->subtitles; mpsub_position = subd->sub_uses_time? (sub_delay*100) : (sub_delay*fps); if (sub_fps==0) sub_fps=fps; fd=fopen ("dump.mpsub", "w"); if (!fd) { perror ("dump_mpsub: fopen"); return; } if (subd->sub_uses_time) fprintf (fd,"FORMAT=TIME\n\n"); else fprintf (fd, "FORMAT=%5.2f\n\n", fps); for(j=0; j < subd->sub_num; j++){ subtitle* egysub=&subs[j]; if (subd->sub_uses_time) { a=((egysub->start-mpsub_position)/100.0); b=((egysub->end-egysub->start)/100.0); if ( (float)((int)a) == a) fprintf (fd, "%.0f",a); else fprintf (fd, "%.2f",a); if ( (float)((int)b) == b) fprintf (fd, " %.0f\n",b); else fprintf (fd, " %.2f\n",b); } else { fprintf (fd, "%ld %ld\n", (long)((egysub->start*(fps/sub_fps))-((mpsub_position*(fps/sub_fps)))), (long)(((egysub->end)-(egysub->start))*(fps/sub_fps))); } mpsub_position = egysub->end; for (i=0; i<egysub->lines; i++) { fprintf (fd, "%s\n",egysub->text[i]); } fprintf (fd, "\n"); } fclose (fd); mp_msg(MSGT_SUBREADER,MSGL_INFO,"SUB: Subtitles dumped in \'dump.mpsub\'.\n");}void dump_microdvd(sub_data* subd, float fps) { int i, delay; FILE *fd; subtitle *subs = subd->subtitles; if (sub_fps == 0) sub_fps = fps; fd = fopen("dumpsub.txt", "w"); if (!fd) { perror("dumpsub.txt: fopen"); return; } delay = sub_delay * sub_fps; for (i = 0; i < subd->sub_num; ++i) { int j, start, end; start = subs[i].start; end = subs[i].end; if (subd->sub_uses_time) { start = start * sub_fps / 100 ; end = end * sub_fps / 100; } else { start = start * sub_fps / fps; end = end * sub_fps / fps; } start -= delay; end -= delay; fprintf(fd, "{%d}{%d}", start, end); for (j = 0; j < subs[i].lines; ++j) fprintf(fd, "%s%s", j ? "|" : "", subs[i].text[j]); fprintf(fd, "\n"); } fclose(fd); mp_msg(MSGT_SUBREADER,MSGL_INFO,"SUB: Subtitles dumped in \'dumpsub.txt\'.\n");}void dump_jacosub(sub_data* subd, float fps) { int i,j; int h,m,s,cs; FILE * fd; subtitle * onesub; unsigned long temp; subtitle *subs = subd->subtitles; if (!subd->sub_uses_time && sub_fps == 0) sub_fps = fps; fd=fopen("dumpsub.jss","w"); if(!fd) { perror("dump_jacosub: fopen"); return; } fprintf(fd, "#TIMERES %d\n", (subd->sub_uses_time) ? 100 : (int)sub_fps); for(i=0; i < subd->sub_num; i++) { onesub=subs+i; //=&subs[i]; temp=onesub->start; if (!subd->sub_uses_time) temp = temp * 100 / sub_fps; temp -= sub_delay * 100; h=temp/360000;temp%=360000; //h =1*100*60*60 m=temp/6000; temp%=6000; //m =1*100*60 s=temp/100; temp%=100; //s =1*100 cs=temp; //cs=1*10 fprintf(fd,"%02d:%02d:%02d.%02d ",h,m,s,cs); temp=onesub->end; if (!subd->sub_uses_time) temp = temp * 100 / sub_fps; temp -= sub_delay * 100; h=temp/360000;temp%=360000; m=temp/6000; temp%=6000; s=temp/100; temp%=100; cs=temp; fprintf(fd,"%02d:%02d:%02d.%02d {~} ",h,m,s,cs); for(j=0;j<onesub->lines;j++) fprintf(fd,"%s%s",j ? "\\n" : "", onesub->text[j]); fprintf(fd,"\n"); } fclose(fd); mp_msg(MSGT_SUBREADER,MSGL_INFO,"SUB: Subtitles dumped in \'dumpsub.js\'.\n");}void dump_sami(sub_data* subd, float fps) { int i,j; FILE * fd; subtitle * onesub; unsigned long temp; subtitle *subs = subd->subtitles; if (!subd->sub_uses_time && sub_fps == 0) sub_fps = fps; fd=fopen("dumpsub.smi","w"); if(!fd) { perror("dump_jacosub: fopen"); return; } fprintf(fd, "<SAMI>\n" "<HEAD>\n" " <STYLE TYPE=\"Text/css\">\n" " <!--\n" " P {margin-left: 29pt; margin-right: 29pt; font-size: 24pt; text-align: center; font-family: Tahoma; font-weight: bold; color: #FCDD03; background-color: #000000;}\n" " .SUBTTL {Name: 'Subtitles'; Lang: en-US; SAMIType: CC;}\n" " -->\n" " </STYLE>\n" "</HEAD>\n" "<BODY>\n"); for(i=0; i < subd->sub_num; i++) { onesub=subs+i; //=&subs[i]; temp=onesub->start; if (!subd->sub_uses_time) temp = temp * 100 / sub_fps; temp -= sub_delay * 100; fprintf(fd,"\t<SYNC Start=%lu>\n" "\t <P>", temp * 10); for(j=0;j<onesub->lines;j++) fprintf(fd,"%s%s",j ? "<br>" : "", onesub->text[j]); fprintf(fd,"\n"); temp=onesub->end; if (!subd->sub_uses_time) temp = temp * 100 / sub_fps; temp -= sub_delay * 100; fprintf(fd,"\t<SYNC Start=%lu>\n" "\t <P> \n", temp * 10); } fprintf(fd, "</BODY>\n" "</SAMI>\n"); fclose(fd); mp_msg(MSGT_SUBREADER,MSGL_INFO,"SUB: Subtitles dumped in \'dumpsub.smi\'.\n");}void sub_free( sub_data * subd ){ int i; if ( !subd ) return; if (subd->subtitles) { for (i=0; i < subd->subtitles->lines; i++) free( subd->subtitles->text[i] ); free( subd->subtitles ); } if (subd->filename) free( subd->filename ); free( subd );}#ifdef DUMPSUBSint main(int argc, char **argv) { // for testing sub_data *subd; if(argc<2){ printf("\nUsage: subreader filename.sub\n\n"); exit(1); } sub_cp = argv[2]; subd = sub_read_file(argv[1]); if(!subd){ printf("Couldn't load file.\n"); exit(1); } list_sub_file(subd); return 0;}#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -