2013-10-20 43 views
0

我需要字幕帮助=)C/Java - 读取2个文件写入一个

我有两个带字幕的.srt文件。一个是英文的,另一个是斯洛文尼亚的。问题在于Slovene中的文件没有正确的 时间码,所以字幕比实际的线更快。我想要做的是编写一个程序,读取这两个文件,从eng.srt文件和字幕文件并写入所有complete.srt中的时间代码字幕和 。我不在乎它是否在Java或C中。我目前正在尝试用C语言编写程序,并且我会提供任何帮助。

现在证明它是我想做的事:

eng.srt (right timecode) 

1 
00:00:01,259 --> 00:00:03,734 
<i>Previously on...</i> 

2 
00:00:03,746 --> 00:00:06,910 
<i>Tom and Lynette drifted further apart,</i> 

3 
00:00:06,911 --> 00:00:09,275 
<i>and Jane took advantage.</i> 

4 
00:00:09,440 --> 00:00:10,670 
I'm scared. 

5 
00:00:10,671 --> 00:00:13,362 
<i>Roy helped Karen face her cancer.</i> 




slo.srt (right subtitles) 

1 
00:00:00,009 --> 00:00:02,484 
<i>Prejšnič...</i> 

2 
00:00:02,496 --> 00:00:05,660 
<i>Tom and Lynette 
sta se še bolj odtujila,</i> 

3 
00:00:05,661 --> 00:00:08,025 
<i>in Jane je to izkoristila.</i> 

4 
00:00:08,190 --> 00:00:09,420 
Strah me je. 

5 
00:00:09,421 --> 00:00:12,112 
<i>Roy se je pomagal Karen 
soočiti z rakom.</i> 



complete.srt (where i write) 

1 
00:00:01,259 --> 00:00:03,734 
<i>Prejšnič...</i> 

2 
00:00:03,746 --> 00:00:06,910 
<i>Tom and Lynette 
sta se še bolj odtujila,</i> 
... 

这是我到目前为止所(我打开3个文件,我将更新我的工作,因为我去):

#include <stdio.h> 
#include <stdlib.h> 

int main() 
{ 
    char ch, sf1[20], sf2[20], tf[20]; 
    FILE *source1, *source2, *target; 

    //first source file 
    printf("Enter name of first source file\n"); 
    gets(sf1); 

    source1 = fopen(sf1, "r"); 

    //seconds source file 
    printf("Enter name of second source file\n"); 
    gets(sf2); 

    source2 = fopen(sf2, "r"); 

    if(source == NULL) 
    { 
     printf("Press any key to exit...\n"); 
     exit(EXIT_FAILURE); 
    } 

    //target file 
    printf("Enter name of target file\n"); 
    gets(tf); 

    target = fopen(tf, "w"); 

    if(target == NULL) 
    { 
     fclose(source); 
     printf("Press any key to exit...\n"); 
     exit(EXIT_FAILURE); 
    } 




    printf("File writen successfully.\n"); 

    fclose(source1); 
    fclose(source2); 
    fclose(target); 

    return 0; 
} 

我的问题是,我不知道如何告诉程序从eng.srt文件只读的数字,比跳过字幕部分,并等待,比读slo.srt文件拿出字幕并跳过编号RS。

+0

请仔细阅读帮助部分。这不是简单地询问代码的地方。你有尝试过什么吗? – home

+0

当问一个关于堆栈溢出的问题时,显示代码片段是一个很好的习惯 –

+0

问题是什么? – reporter

回答

0

这很容易通过像awk这样的模式匹配语言来完成。这里的模式非常简单。对于时间码,它以2位数字(^ [0-9] [0-9])开头,字幕以(^)开头。我没有详细说明解决方案,因为我不知道您是否会使用其中一种脚本语言。

0

主要逻辑很简单。这是它的pseudo-code

for each subtitle in file1 and file2: 
    extract_time_from_file1; 
    extract_subtitle_from_file2; 
    write_into_new_file_combining_the_time_and_string 

这里完全是一个工作代码:

#include <iostream> 
#include <fstream> 
using namespace std; 
string read_title_string(ifstream& in) 
{ 
    string ans=""; 
    string tmp; 
    getline(in, tmp);//neglect the subtitle number 
    getline(in, tmp);//neglect the time.. 
    /*sub-title extraction*/ 
    while(1)//read until the blank line and store all the strings.. 
    { 
    getline(in, tmp); 
    if(tmp.length()==0) 
     break; 
    ans += tmp; 
    } 
    return ans; 
} 
string read_title_time(ifstream& in) 
{ 
    string ans=""; 
    string tmp; 
    getline(in, tmp);//ignore subtitle number.. 
    getline(in, ans);//this is what we want.. 
    while(1)//read until a blank line and ignore them.. 
    { 
     getline(in, tmp); 
     if(tmp.length()==0) 
      break; 
    } 
    return ans; 
} 
int main() 
{ 
    ifstream ins("slo.srt"),outs("eng.srt"); 
    ofstream ans("complete.srt"); 
    int count=1; 
    while(!ins.eof() && !outs.eof()) 
    { 
     ans<<count++<<endl; 
     ans<<read_title_time(outs)<<endl; 
     ans<<read_title_string(ins)<<endl; 
     ans<<endl; 
    } 
    ins.close();outs.close();ans.close(); 
    return 0; 
} 

仔细注意这个代码依赖于文件的结构。如果内容以不同的方式组织,这可能不起作用。希望这可以帮助!!

+0

嗯,这段代码不适合我。它启动并且不会报告任何错误,但它不会在complete.srt文件中写入任何内容。尝试只有3个字幕行 – user2255163

+0

我依赖于字幕文件的结构。如果有至少一个额外的空白,它会失败。尝试改进此代码以获得确切的输出。 – nitish712