forked from oster/rtce-experiments-toolbox
-
Notifications
You must be signed in to change notification settings - Fork 1
/
convert-chat-as-srt.rb
executable file
·63 lines (49 loc) · 1.26 KB
/
convert-chat-as-srt.rb
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
#!/usr/bin/env ruby -wU
require 'json'
require 'time'
# example of usage:
#./convert-chat-as-srt.rb DATA/CHATS/films018-chat.json -1000
def format_timestamp(timestamp, delay=0)
if timestamp.nil?
return ""
else
ts = timestamp + delay
return Time.at(ts / 1000, ts % 1000 * 1000).strftime("%H:%M:%S,%L")
end
end
if ARGV.size == 0
puts "usage: convert-chat-as-srt chat_file [ delay_in_ms ]"
Process.exit(0)
else
chat_file = ARGV[0]
delay = ARGV[1].to_i
end
messages = []
File.open(chat_file, "r") do |inFile|
while (line = inFile.gets())
o = JSON.parse(line)
o["data"].each do |msg|
messages << msg
end
end
end
# This generate the same output as ./pretty-chat.js
# (except username maybe)
#
# messages.each do |msg|
# puts '[' + format_timestamp(msg['time']) + '] ' + msg['userName'] + ':> ' + msg['text'] # msg['userId']
# end
counter = 1
messages.each do |msg|
puts counter
startTime = msg['time']
if counter < messages.size
endTime = messages[counter]['time']
else
endTime = startTime + 10 * 1000 # last message will remain for 10 sec.
end
puts format_timestamp(startTime, delay) + ' --> ' + format_timestamp(endTime, delay)
puts msg['userName'] + ':> ' + msg['text']
counter = counter + 1
puts
end