Skip to content

Commit 2ace1b7

Browse files
committed
Fix chat log compression by introducing ChatMessageGroup
1 parent bd16daf commit 2ace1b7

File tree

1 file changed

+36
-29
lines changed

1 file changed

+36
-29
lines changed

markut.go

Lines changed: 36 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -51,18 +51,22 @@ func millisToSubRipTs(millis Millis) string {
5151
}
5252

5353
type ChatMessage struct {
54-
TimeOffset Millis
5554
Nickname string
5655
Color string
57-
Text_ string
56+
Text string
57+
}
58+
59+
type ChatMessageGroup struct {
60+
TimeOffset Millis
61+
Messages []ChatMessage
5862
}
5963

6064
type Chunk struct {
6165
Start Millis
62-
End Millis
66+
End Millis
6367
Loc Loc
6468
InputPath string
65-
ChatLog []ChatMessage
69+
ChatLog []ChatMessageGroup
6670
Blur bool
6771
Unfinished bool
6872
}
@@ -140,7 +144,7 @@ type EvalContext struct {
140144
inputPath string
141145
inputPathLog []Token
142146
outputPath string
143-
chatLog []ChatMessage
147+
chatLog []ChatMessageGroup
144148
chunks []Chunk
145149
chapters []Chapter
146150
cuts []Cut
@@ -289,7 +293,7 @@ func (context EvalContext) containsChunkWithName(filePath string) bool {
289293
}
290294

291295
// IMPORTANT! chatLog is assumed to be sorted by TimeOffset.
292-
func sliceChatLog(chatLog []ChatMessage, start, end Millis) []ChatMessage {
296+
func sliceChatLog(chatLog []ChatMessageGroup, start, end Millis) []ChatMessageGroup {
293297
// TODO: use Binary Search for a speed up on big chat logs
294298
lower := 0
295299
for lower < len(chatLog) && chatLog[lower].TimeOffset < start {
@@ -302,25 +306,21 @@ func sliceChatLog(chatLog []ChatMessage, start, end Millis) []ChatMessage {
302306
if lower < len(chatLog) {
303307
return chatLog[lower:upper]
304308
}
305-
return []ChatMessage{}
309+
return []ChatMessageGroup{}
306310
}
307311

308312
// IMPORTANT! chatLog is assumed to be sorted by TimeOffset.
309-
// TODO: chat log compression does not make sense anymore
310-
// We need to introduce ChatMessage groups instead of concatinating rendering messages
311-
/*
312-
func compressChatLog(chatLog []ChatMessage) []ChatMessage {
313-
result := []ChatMessage{}
313+
func compressChatLog(chatLog []ChatMessageGroup) []ChatMessageGroup {
314+
result := []ChatMessageGroup{}
314315
for i := range chatLog {
315316
if len(result) > 0 && result[len(result)-1].TimeOffset == chatLog[i].TimeOffset {
316-
result[len(result)-1].Text = result[len(result)-1].Text + "\n" + chatLog[i].Text
317+
result[len(result)-1].Messages = append(result[len(result)-1].Messages, chatLog[i].Messages...)
317318
} else {
318319
result = append(result, chatLog[i])
319320
}
320321
}
321322
return result
322323
}
323-
*/
324324

325325
type Func struct {
326326
Description string
@@ -333,8 +333,8 @@ var funcs map[string]Func;
333333

334334
// This function is compatible with the format https://www.twitchchatdownloader.com/ generates.
335335
// It does not use encoding/csv because that website somehow generates unparsable garbage.
336-
func loadTwitchChatDownloaderCSVButParseManually(path string) ([]ChatMessage, error) {
337-
chatLog := []ChatMessage{}
336+
func loadTwitchChatDownloaderCSVButParseManually(path string) ([]ChatMessageGroup, error) {
337+
chatLog := []ChatMessageGroup{}
338338
f, err := os.Open(path);
339339
if err != nil {
340340
return chatLog, err
@@ -366,19 +366,19 @@ func loadTwitchChatDownloaderCSVButParseManually(path string) ([]ChatMessage, er
366366
text = text[1:len(text)-1]
367367
}
368368

369-
chatLog = append(chatLog, ChatMessage{
369+
chatLog = append(chatLog, ChatMessageGroup{
370370
TimeOffset: Millis(secs*1000),
371-
Color: color,
372-
Nickname: nickname,
373-
Text_: text,
371+
Messages: []ChatMessage{
372+
{Color: color, Nickname: nickname, Text: text},
373+
},
374374
})
375375
}
376376

377377
sort.Slice(chatLog, func(i, j int) bool {
378378
return chatLog[i].TimeOffset < chatLog[j].TimeOffset
379379
})
380380

381-
return chatLog, nil
381+
return compressChatLog(chatLog), nil
382382
}
383383

384384
func (context *EvalContext) evalMarkutContent(content string, path string) bool {
@@ -652,7 +652,7 @@ func ffmpegGenerateConcatList(chunks []Chunk, outputPath string) error {
652652
return nil
653653
}
654654

655-
func captionsRingPush(ring []ChatMessage, message ChatMessage, capacity int) []ChatMessage {
655+
func captionsRingPush(ring []ChatMessageGroup, message ChatMessageGroup, capacity int) []ChatMessageGroup {
656656
if len(ring) < capacity {
657657
return append(ring, message)
658658
}
@@ -926,17 +926,20 @@ var Subcommands = map[string]Subcommand{
926926
fmt.Printf("time,user_name,user_color,message\n");
927927
var cursor Millis = 0
928928
for _, chunk := range context.chunks {
929-
for _, message := range chunk.ChatLog {
930-
timestamp := cursor + message.TimeOffset - chunk.Start;
931-
fmt.Printf("%d,%s,%s,\"%s\"\n", timestamp, message.Nickname, message.Color, message.Text_);
929+
for _, messageGroup := range chunk.ChatLog {
930+
timestamp := cursor + messageGroup.TimeOffset - chunk.Start;
931+
for _, message := range messageGroup.Messages {
932+
fmt.Printf("%d,%s,%s,\"%s\"\n", timestamp, message.Nickname, message.Color, message.Text);
933+
}
932934
}
933935
cursor += chunk.End - chunk.Start;
934936
}
935937
} else {
936938
capacity := 1
937-
ring := []ChatMessage{}
939+
ring := []ChatMessageGroup{}
938940
timeCursor := Millis(0)
939941
subRipCounter := 0;
942+
sb := strings.Builder{}
940943
for _, chunk := range context.chunks {
941944
prevTime := chunk.Start
942945
for _, message := range chunk.ChatLog {
@@ -946,8 +949,12 @@ var Subcommands = map[string]Subcommand{
946949
subRipCounter += 1
947950
fmt.Printf("%d\n", subRipCounter);
948951
fmt.Printf("%s --> %s\n", millisToSubRipTs(timeCursor), millisToSubRipTs(timeCursor + deltaTime));
949-
for _, ringMessage := range ring {
950-
fmt.Printf("[%s] %s\n", ringMessage.Nickname, ringMessage.Text_);
952+
for _, ringMessageGroup := range ring {
953+
sb.Reset();
954+
for _, message := range ringMessageGroup.Messages {
955+
sb.WriteString(fmt.Sprintf("[%s] %s\n", message.Nickname, message.Text));
956+
}
957+
fmt.Printf("%s", sb.String());
951958
}
952959
fmt.Printf("\n")
953960
}
@@ -1375,7 +1382,7 @@ func main() {
13751382
Category: "Chat",
13761383
Signature: "--",
13771384
Run: func(context *EvalContext, command string, token Token) bool {
1378-
context.chatLog = []ChatMessage{}
1385+
context.chatLog = []ChatMessageGroup{}
13791386
return true
13801387
},
13811388
},

0 commit comments

Comments
 (0)