-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSRTProcessing.rb
More file actions
112 lines (82 loc) · 2.24 KB
/
SRTProcessing.rb
File metadata and controls
112 lines (82 loc) · 2.24 KB
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
require "pry"
require "time"
class SRTProcessing
def initialize(filename)
@entries = []
# read and fulfill the entries array with every entry
parse(filename)
end
def parse(filename)
lines_str = IO.read(filename)
entries_as_array = lines_str.split("\n\r\n")
entries_as_array.each { |entry| split_entry(entry) }
end
def split_entry(entry)
table_array = entry.split("\n")
# 00:01:58,134 --> 00:02:00,753
entry_index = table_array[0]
start_time = table_array[1].split(" --> ")[0]
end_time = table_array[1].split(" --> ")[1].gsub("\r","")
content = ""
for i in 2..table_array.length-1
if table_array[i] != nil
content += table_array[i]
end
end
@entries << Entry.new(start_time, end_time, content, entry_index)
end
def get_entries
@entries
end
def shift_all(milis)
@entries.each { |entry| entry.shift_time(milis) }
end
def make_new_file(filename)
final_output = ""
@entries.each { |entry| final_output += entry.get_string }
IO.write(filename, final_output)
end
end
class Entry
def initialize(start_time, end_time, content, entry_index)
@start_time = start_time
@end_time = end_time
@content = content
@entry_index = entry_index
end
def shift_time(milis)
starting = Timeshifter.new(@start_time)
@start_time = starting.add_time(milis)
ending = Timeshifter.new(@end_time)
@end_time = ending.add_time(milis)
end
def get_string()
@entry_index + "\n" + @start_time.strftime("%H:%M:%S,%L") + " --> " + @end_time.strftime("%H:%M:%S,%L") + "\n" + @content + "\n\n"
end
end
class Timeshifter
def initialize(time)
@ini_time = Time.parse(time)
end
def add_time(milis)
@ini_time += milis/1000.to_f
end
end
Class Spellchecker
def initialize (entries, filname)
end
def create_a_checklist
wordslist = IO.read(filname)
entries.each do |entry| {
entry.content.gsub(/[\n\r]/, ' ').split(" ").each {
if wordslist.include? "#{entry.content.}"
}
wordslist << entriescontent.spilt(" ")
}
end
end
somename = SRTProcessing.new("ShortExample.srt")
somename.shift_all(5000)
somename.shift_all(-2000)
somename.make_new_file("output.srt")
var = Spellchecker.new(somename.get_entries, "words.txt")