-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathvtt2srt
executable file
·139 lines (122 loc) · 4.25 KB
/
vtt2srt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
#!/usr/bin/gjs
/*
* Copyright (C) 2018 konkor <https://github.com/konkor>
*
* vtt2srt is free software: you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
* Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* vtt2srt is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program. If not, see <http://www.gnu.org/licenses/>.
*/
const Gio = imports.gi.Gio;
const GLib = imports.gi.GLib;
const Lang = imports.lang;
const Format = imports.format;
String.prototype.format = Format.format;
const txt = "USAGE: [\"VTT\" |] vtt2srt [FILE_VTT] [OUT_FILE_SRT | > OUTPUT STREAM]";
let stdin, stdout;
let uris = [], output_file;
let i, s;
var Converter = new Lang.Class ({
Name: 'Converter',
Extends: Gio.Application,
_init: function () {
GLib.set_prgname ("vtt2srt");
this.parent ({
application_id: "org.konkor.vtt2srt",
flags: Gio.ApplicationFlags.HANDLES_COMMAND_LINE
});
GLib.set_application_name ("VTT Converter");
},
vfunc_startup: function() {
this.parent();
},
vfunc_command_line: function(cmd) {
let text = this.get_stdin ();
if (text) text = this.convert (text);
for (i = 0; i < ARGV.length; i++) {
s = ARGV[i];
if ((s == "-h") || (s == "--help")) {
print (txt);
this.quit ();
} else if (this._is_vtt (s)) {
uris.push (s);
} else output_file = s;
}
if (text)
if (output_file) this.set_contents (output_file, text);
else print (text);
uris.forEach ((u)=>{
this.set_contents (this.srtname (u), this.convert (this.get_contents(u)));
});
return 0;
},
convert: function (text) {
let srt = "", index = 1, time = false;
text.split ("\n").forEach ((s)=>{
s = s.replace ("\r","");
if ((s.length == 0) && time) {
srt = srt.trim() + "\n\n";
time = false;
index++;
} else if (!time && this.is_time (s)) {
time = true;
var ar = s.replace (/\./gi,",").split (" ");
if (ar.length > 2)
srt += "%d\n%s --> %s\n".format (index,ar[0],ar[2]);
} else if ((s.length > 0) && time) {
srt += this.clear (s);
}
});
return srt;
},
clear: function (text) {
let s = text.replace (/<.*?c>/gi,"");
s = s.replace (/<c\.color.*?>/gi,"") + " ";
return s;
},
is_time: function (text) {
if (text.length < 27) return false;
if (text.indexOf (" --> ") == -1) return false;
if (text.indexOf (":") < 1) return false;
if (text.indexOf (".") < 1) return false;
return true;
},
get_contents: function (filename) {
let [res,data] = GLib.file_get_contents (filename);
if (res) return data.toString ();
return "";
},
set_contents: function (filename, text) {
//if (uris.length == 1 && !output_file) print (text);
return GLib.file_set_contents (filename, text);
},
srtname: function (filename) {
if (uris.length < 2 && output_file) return output_file;
return filename.toLowerCase().replace (".vtt",".srt");
},
_is_vtt: function (filename) {
if (!filename) return false;
if (!filename.toLowerCase().endsWith(".vtt")) return false;
let file = Gio.File.new_for_path (filename);
if (!file.query_exists (null)) return false;
return true;
},
get_stdin: function () {
let s = "";
let channel = GLib.IOChannel.unix_new(0);
if (channel.get_flags() != GLib.IOFlags.IS_READABLE) return s;
let [res, text] = channel.read_to_end();
if (text) s = text.toString();
return s;
}
});
let app = new Converter ();
app.run (ARGV);