2013-10-20 42 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