Skip to content

Instantly share code, notes, and snippets.

@baiwfg2
Created April 27, 2017 02:41
Show Gist options
  • Save baiwfg2/0d0b1b075818f6f342550e66f3f8c317 to your computer and use it in GitHub Desktop.
Save baiwfg2/0d0b1b075818f6f342550e66f3f8c317 to your computer and use it in GitHub Desktop.
change vtt format to srt format in order to be loaded by player
#include <iostream>
#include <string>
#include <fstream>
#include <thread>
#include <regex>
#include <cstdio>
using namespace std;
int main(int argc,char* argv[])
{
if (argc < 2) {
printf("filename not given\n");
exit(-1);
}
int i = 1;
while (i < argc) {
string filename(argv[i++]);
string line;
ifstream in(filename);
//把文件后缀.vtt换成.srt,
regex name_pattern("(.*\\.)vtt");
//regex name_pattern("(.*\\.)en.vtt");
string replace = "$1srt";
string newname = regex_replace(filename,name_pattern,replace);
ofstream fout(newname);
regex ecma_pattern("(\\d{2}):(\\d{2}).*");
//regex basic_pattern("[0-9]\\{2\\}:[0-9]\\{2\\}.*",regex::basic);
//用来把点号换成逗号
regex replace_period("(\\d{2}:\\d{2}:\\d{2})\\.(\\d{3} --> )(\\d{2}:\\d{2}:\\d{2})\\.");
string replace2 = "$1,$2$3,";
int cnt = 1;
int ignore = 0;
while (std::getline(in,line)) {
/*
前三行是这三行,忽略
WEBVTT
Kind: captions
Language: en
*/
if (ignore++ < 3) continue;
if (regex_match(line,ecma_pattern)) {
fout << cnt++ << endl;
fout << regex_replace(line,replace_period,replace2) << endl;
continue;
}
fout << line << endl;
}
in.close();
fout.close();
//std::remove(filename.c_str());//也许手工删除vtt文件要好
}
return 0;
}
@UMU618
Copy link

UMU618 commented Jul 2, 2022

ffmpeg -i vtt srt

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment