I found some of the movies I downloaded failed to have any perfectly matching subtitles that can be easily found online, so I have to use some of the closest. But using resync function of vobsub or so is obviously not a way round. With this tool and not too much tuning on it, I guess I may even use those most diverse ones. Complete code of the latest version with some inefficient commentary is provided here:
#include <cstdio>#include <cstring>#include <cstdlib>typedef unsigned long movtime_t;static void fgetline (char *buf, int size, FILE *f){ fgets(buf, size, f); int len = strlen(buf); buf[len - 1] = 0;}static bool isnumber (char *p){ int len = 0; for ( ; *p != '/0'; ++p, ++len) { if (*p < '0' || *p > '9') { return false; } } return (len > 0);}static bool parse_timestamp (char *p, movtime_t &t){#define ISDIGIT(c) ((c)>='0'&&(c)<='9')#define ASSERTC(c, ca) if (c!=ca) return false;#define DIGIT2(v,p) / if (!ISDIGIT((p)[0]) || !ISDIGIT((p)[1])) return false; / v = ((p)[0]-'0')*10+((p)[1]-'0');#define DIGIT3(v,p) / if (!ISDIGIT((p)[0]) || !ISDIGIT((p)[1]) || !ISDIGIT((p)[2])) return false; / v = ((p)[0]-'0')*100+((p)[1]-'0')*10+((p)[2]-'0'); int h, m, s, ms; DIGIT2(h,p); ASSERTC(p[2],':'); DIGIT2(m,p+3); ASSERTC(p[5],':'); DIGIT2(s,p+6); ASSERTC(p[8],','); DIGIT3(ms,p+9); t = (h*3600+m*60+s)*1000+ms; return true;}static bool istimestamp (char *p, movtime_t &begin, movtime_t &end){ // form 00:00:46,430 if (strlen(p) < 29) { return false; } if (!parse_timestamp(p, begin)) { return false; } // no assertion on the arrow, for it's already very certain if (!parse_timestamp(p + 17, end)) { return false; } return true;}static void analyse_time (movtime_t t, int &h, int &m, int &s, int &ms){ ms = t % 1000; t /= 1000; s = t % 60; t /= 60; m = t % 60; t /= 60; h = t;}/* it should be big enough to avoid line segmentation */#define sizebuf 2048static void rectify ( FILE **ssfpins, // ssfnum FILE **ssfpouts, // movnum int ssfnum, int movnum, movtime_t *ssfdurs, // in msec, 120min --> 7200000 movtime_t *movdurs // in msec ){ // the initial offset of both items are provided at the tail // the following are the absolute time of them movtime_t ssfabs = ssfdurs[ssfnum]; movtime_t movabs = movdurs[movnum]; char buf[sizebuf]; char outbuf[sizebuf*2] = {0, }; char oldbuf[sizebuf*2] = {0, }; char tempbuf[sizebuf]; int ssfi = 0; int movi = 0; FILE *ssfpin = ssfpins[ssfi]; FILE *ssfpout = ssfpouts[movi]; int outcount = 0; int oldcount; bool someinold = false; while (1) { if (feof(ssfpin)) {__fileend: ssfabs += ssfdurs[ssfi]; ssfi++; if (ssfi >= ssfnum ) { break; } ssfpin = ssfpins[ssfi]; } fgetline(buf, sizebuf, ssfpin); movtime_t begin, end; if (isnumber(buf)) { // print last if (someinold) { fprintf(ssfpout-1, "%d/n", oldcount); fprintf(ssfpout-1, "%s", oldbuf); someinold = false; } if (outcount > 0) { fprintf(ssfpout, "%d/n", outcount); fprintf(ssfpout, "%s", outbuf); } outbuf[0] = 0; outcount++; } else if (istimestamp(buf, begin, end)) { if (begin == 0 && end == 0) { // sign of end goto __fileend; } movtime_t absbegin, absend; movtime_t corrbegin, corrend; absbegin = ssfabs + begin; absend = ssfabs + end; // check if it's the next output file's turn if (absend >= movabs + movdurs[movi]) { if (absbegin < movabs + movdurs[movi]) { // absbegin to movabs + movdurs[movi] -> cur corrbegin = absbegin - movabs; corrend = movdurs[movi]; int h, m, s, ms; analyse_time(corrbegin, h, m, s, ms); sprintf(tempbuf, "%02d:%02d:%02d,%03d", h, m, s, ms); strcat(outbuf, tempbuf); analyse_time(corrend, h, m, s, ms); sprintf(tempbuf, " --> %02d:%02d:%02d,%03d/n", h, m, s, ms); strcat(outbuf, tempbuf); strcpy(oldbuf, outbuf); outbuf[0] = 0; oldcount = outcount; someinold = true; // movabs + movdurs[movi] to absend -> next absbegin = movabs + movdurs[movi]; } else { // totally in a new one } movabs += movdurs[movi]; movi++; if (movi >= movnum) { break; } ssfpout = ssfpouts[movi]; outcount = 1; } corrbegin = absbegin - movabs; corrend = absend - movabs; int h, m, s, ms; analyse_time(corrbegin, h, m, s, ms); sprintf(tempbuf, "%02d:%02d:%02d,%03d", h, m, s, ms); strcat(outbuf, tempbuf); analyse_time(corrend, h, m, s, ms); sprintf(tempbuf, " --> %02d:%02d:%02d,%03d/n", h, m, s, ms); strcat(outbuf, tempbuf); } else { sprintf(tempbuf, "%s/n", buf); strcat(outbuf, tempbuf); if (someinold) { strcat(oldbuf, tempbuf); } } }}struct CorrStruct{#define MAXFNUM 16 FILE *ssfpins[MAXFNUM]; // ssfnum in count FILE *ssfpouts[MAXFNUM]; // movnum in count int ssfnum; int movnum; // one more than ssfnum in count movtime_t ssfdurs[MAXFNUM]; // in msec, 120min --> 7200000 movtime_t movdurs[MAXFNUM]; // in msec};static int ParseConfig (CorrStruct *corr, FILE *fConfig){ int i; char buf[sizebuf]; fgetline(buf, sizebuf, fConfig); corr->ssfnum = atoi(buf); if (corr->ssfnum + 1 > MAXFNUM) { return -1; /* too many files */ } fgetline(buf, sizebuf, fConfig); if (!parse_timestamp(buf, corr->ssfdurs[corr->ssfnum])) { return -2; /* timestamp error */ } for (i = 0; i < corr->ssfnum; i++) { fgetline(buf, sizebuf, fConfig); corr->ssfpins[i] = fopen(buf, "r"); fgetline(buf, sizebuf, fConfig); if (!parse_timestamp(buf, corr->ssfdurs[i])) { return -2; } } fgetline(buf, sizebuf, fConfig); corr->movnum = atoi(buf); if (corr->movnum + 1 > MAXFNUM) { return -1; /* too many files */ } fgetline(buf, sizebuf, fConfig); if (!parse_timestamp(buf, corr->movdurs[corr->movnum])) { return -2; } for (i = 0; i < corr->movnum; i++) { fgetline(buf, sizebuf, fConfig); corr->ssfpouts[i] = fopen(buf, "w"); fgetline(buf, sizebuf, fConfig); if (!parse_timestamp(buf, corr->movdurs[i])) { return -2; } } return 0;}static void Finalize (CorrStruct *corr){ for (int i = 0; i < corr->ssfnum; i++) { fclose(corr->ssfpins[i]); } for (int i = 0; i < corr->movnum; i++) { fclose(corr->ssfpouts[i]); }}int main (void){ FILE *fConfig = fopen("ssresync_config.txt", "r"); CorrStruct corr; ParseConfig(&corr, fConfig); rectify(corr.ssfpins, corr.ssfpouts, corr.ssfnum, corr.movnum, corr.ssfdurs, corr.movdurs); Finalize(&corr); fclose(fConfig); return 0;}// An example of config file// It is applied on the subtitles downloaded from shooter.cn for the movie Amadeus with both the // movie and the script divided into 3 parts with some time deviation between them./*== BEGIN ==300:00:00,000inNewMov-Amadeus-CD1.eng.srt00:58:03,160inNewMov-Amadeus-CD2.eng.srt00:58:12,500inNewMov-Amadeus-CD3.eng.srt00:58:23,240300:00:00,000NewMov-Amadeus-CD1.eng.srt00:57:09,000NewMov-Amadeus-CD2.eng.srt00:57:40,000NewMov-Amadeus-CD3.eng.srt01:05:32,000== END ==*/
分享到:
相关推荐
Subtitle原理及实现,详细介绍subtitle
2、可以自动修复常见的英文字幕错误(多或少空格,“i”的大小写,长、短、重叠时间检查等等)。 3、可以手动快速、精确的创建时间轴,或进行校正(整体或单独)。 4、方便易用地合并、拆分时间轴以及字幕的功能。 5...
当辛苦从网上下载到想要的字幕时,却发现字幕时间内容跟电影不同步,是一件令人不爽的事,现在有了Subtitle Edit字幕编辑软件,可以方便你调整字幕的任意时间位置。能导入各种格式的普通字幕或蓝光字幕,从DVD视频...
Subtitle Edit是一个电影字幕编辑软件,使用Subtitle Edit,如果电影内容声音跟字幕不同步,您可以轻松地调整任何字幕的开始时间。字幕编辑软件(Subtitle Edit )是一个免费的编辑器的视频字幕编辑器。其源代码也是...
collapsingtoolbarlayout-subtitle,具有字幕支持的标准,博客附件,效果请查看博客相对应项目。
Standard CollapsingToolbarLayout with subtitle support. Using internal components of support design library allows it to behave similarly to its sibling CollapsingToolbarLayout, while also ensuring ...
原来没上传成功的words via subtitle
资源来自pypi官网。 资源全名:subtitle-2.3-py2.py3-none-any.whl
Words Via Subtitle改进:视频播放例句、加快生成WPS文档的速度
Subtitle Edit是一款免费好用的视频字幕编辑制作软件,想制作个性的MV怎么能少得了字幕,今天分享一款免费的字幕制作利器,有时候我们从网上下载的字幕在播放时可能会出现声音字幕不同步的问题,使用这个软件你可以...
Subtitle And Video Renamer0.2.1汉化版(字幕及视频更名器)
3d subtitle 32bit 将srt字幕转换为蓝光SUB字幕3d subtitle 32bit 将srt字幕转换为蓝光SUB字幕3d subtitle 32bit 将srt字幕转换为蓝光SUB字幕3d subtitle 32bit 将srt字幕转换为蓝光SUB字幕3d subtitle 32bit
Subtitle Edit字幕编辑
Subtitle Editor是一款针对文本格式类字幕的编辑工具,支持多种字符编码和字幕格式(Sub Station Alpha、Advanced Sub Station Alpha、SubRip、MicroDVD、MPL2、MPsub (MPlayer subtitle)、SubViewer 2.0、Plain-Text...
字幕编辑器,各种格式,很全,无病毒,可以编辑各种格式的字幕,很好用。
字幕编辑不可多的的软件,支持SUP、SUB、SRT、SIM、ASS、SSA等多种字幕编辑。
雷霆字幕多亏了安装pip install -U thunder-subtitle $ thunder-subtitle -husage: thunder-subtitle [-h] [-i INDEX] pathpositional arguments: path movie pathoptional arguments: -h, --help show this help ...
npm install subtitle 纱 yarn add subtitle 用法 该库提供了一些基于流的功能来处理字幕。 下面的示例解析一个SRT文件,将其重新同步并输出一个VTT文件: import fs from 'fs' import { parse , resync , ...
movies subtitle series peaky blinders