-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtranscriptData.js
More file actions
130 lines (117 loc) · 3.51 KB
/
Copy pathtranscriptData.js
File metadata and controls
130 lines (117 loc) · 3.51 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
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
class TranscriptData {
constructor(segments, videoId) {
this.segments = segments;
this.videoId = videoId;
this.cache = new Map();
}
getCacheKey(format, includeTimestamps) {
return `${format}-${includeTimestamps}`;
}
clearCache() {
this.cache.clear();
}
// Format as plain text
toText(includeTimestamps = false) {
return this.segments
.map((segment) => {
if (includeTimestamps) {
const time = this.formatTimestamp(segment.start);
return `[${time}] ${segment.text}`;
}
return segment.text;
})
.join('\n');
}
// Format as SRT
toSRT() {
return this.segments
.map((segment, index) => {
const startTime = this.formatSRTTimestamp(segment.start);
const endTime = this.formatSRTTimestamp(
segment.start + segment.duration
);
return `${index + 1}\n${startTime} --> ${endTime}\n${segment.text}\n`;
})
.join('\n');
}
// Format as VTT
toVTT() {
const header = 'WEBVTT\n\n';
const body = this.segments
.map((segment, index) => {
const startTime = this.formatVTTTimestamp(segment.start);
const endTime = this.formatVTTTimestamp(
segment.start + segment.duration
);
return `${startTime} --> ${endTime}\n${segment.text}\n`;
})
.join('\n');
return header + body;
}
// Format timestamp for plain text (MM:SS)
formatTimestamp(seconds) {
const minutes = Math.floor(seconds / 60);
const remainingSeconds = Math.floor(seconds % 60);
return `${minutes.toString().padStart(2, '0')}:${remainingSeconds
.toString()
.padStart(2, '0')}`;
}
// Format timestamp for SRT (HH:MM:SS,mmm)
formatSRTTimestamp(seconds) {
const hours = Math.floor(seconds / 3600);
const minutes = Math.floor((seconds % 3600) / 60);
const secs = Math.floor(seconds % 60);
const ms = Math.floor((seconds % 1) * 1000);
return `${hours.toString().padStart(2, '0')}:${minutes
.toString()
.padStart(2, '0')}:${secs.toString().padStart(2, '0')},${ms
.toString()
.padStart(3, '0')}`;
}
// Format timestamp for VTT (HH:MM:SS.mmm)
formatVTTTimestamp(seconds) {
const hours = Math.floor(seconds / 3600);
const minutes = Math.floor((seconds % 3600) / 60);
const secs = Math.floor(seconds % 60);
const ms = Math.floor((seconds % 1) * 1000);
return `${hours.toString().padStart(2, '0')}:${minutes
.toString()
.padStart(2, '0')}:${secs.toString().padStart(2, '0')}.${ms
.toString()
.padStart(3, '0')}`;
}
// Get suggested filename based on format
getFilename(format) {
const title = `youtube_transcript_${this.videoId}`;
switch (format) {
case 'srt':
return `${title}.srt`;
case 'vtt':
return `${title}.vtt`;
default:
return `${title}.txt`;
}
}
// Get formatted transcript based on format
getFormattedTranscript(format, includeTimestamps = false) {
const cacheKey = this.getCacheKey(format, includeTimestamps);
// Return cached version if available
if (this.cache.has(cacheKey)) {
return this.cache.get(cacheKey);
}
// Generate and cache new formatted transcript
let formatted;
switch (format) {
case 'srt':
formatted = this.toSRT();
break;
case 'vtt':
formatted = this.toVTT();
break;
default:
formatted = this.toText(includeTimestamps);
}
this.cache.set(cacheKey, formatted);
return formatted;
}
}