如何延迟字幕

How to delay subtitles

本文关键字:字幕 延迟 何延迟      更新时间:2023-10-16

我必须编写一个延迟字幕的代码。

我必须打开.srt文件更改一段时间并将其保存在另一个文件中。我知道如何打开文件并将其全部复制到其他文件。

例如,如果我打开"字幕.srt",然后输入输出文件"output"的名称,我会将"字幕.srt"的复制内容复制到"输出字幕.srt"。

这没关系,但我不知道如何延迟时间,例如如果我输入"10"

原字幕.srt00:00:01,067 --> 00:00:03,963然后我输入 10输出字幕.srt00:00:11,067 --> 00:00:13,963

我必须一直改变。

#include "stdafx.h"
#include "iostream"
#include "cstdlib"
#include "fstream"
#include "string"
 using namespace std;
 int main(int argc, char *argv[]){
 ifstream input; //input
 char input_file[32]; //names of input and output

  cout << "Enter name of input fille: "; ///user gives names of input
  cin >> input_file;
 input.open(input_file);
 if (!input.good()){
cout << "File " << input_file << " dosen't exist." << endl;
return 1;
}

string row;
while (!input.eof()){    
getline(input, row);
cout << row << endl;
}
system("pause");
return 0;
}

我假设字符串格式是hour:minute:second,millisecond.好吧,这个函数将字符串作为初始时间,并以秒为单位添加一个 int 量。如果你给它一个负时间,它可能会中断,但任何合理的正时间都可以正常工作。它返回输出字符串,因此您可以突出打印旧字符串所在的位置。

string add(string str, int amount) {
    int vals[8];
    sscanf(str.c_str(), "%u:%u:%u,%u --> %u:%u:%u,%u",
    &vals[0], &vals[1], &vals[2], &vals[3],
    &vals[4], &vals[5], &vals[6], &vals[7]);
    vals[2] += amount;
    vals[6] += amount;
    while((vals[2]>=60) || (vals[1]>=60) ||
          (vals[6]>=60) || (vals[5]>=60)) {
        if(vals[2] >= 60) {
            vals[2] -= 60;
            vals[1]++;
        }
        if(vals[6] >= 60) {
            vals[6] -= 60;
            vals[5]++;
        }
        if(vals[1] >= 60) {
            vals[1] -= 60;
            vals[0]++;
        }
        if(vals[5] >= 60) {
            vals[5] -= 60;
            vals[4]++;
        }
    }
    string out;
    out.resize(str.length());
    int n = sprintf(&out[0], "%02u:%02u:%02u,%03u --> %02u:%02u:%02u,%03u", 
    vals[0], vals[1], vals[2], vals[3],
    vals[4], vals[5], vals[6], vals[7]);
    return out;
};