-
Notifications
You must be signed in to change notification settings - Fork 436
/
transcript.go
214 lines (180 loc) · 5.57 KB
/
transcript.go
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
package youtube
import (
"context"
"encoding/json"
"errors"
"fmt"
"strconv"
"strings"
)
var (
ErrTranscriptDisabled = errors.New("transcript is disabled on this video")
)
// TranscriptSegment is a single transcipt segment spanning a few milliseconds.
type TranscriptSegment struct {
// Text is the transcipt text.
Text string `json:"text"`
// StartMs is the start timestamp in ms.
StartMs int `json:"offset"`
// OffsetText e.g. '4:00'.
OffsetText string `json:"offsetText"`
// Duration the transcript segment spans in ms.
Duration int `json:"duration"`
}
func (tr TranscriptSegment) String() string {
return tr.OffsetText + " - " + strings.TrimSpace(tr.Text)
}
type VideoTranscript []TranscriptSegment
func (vt VideoTranscript) String() string {
var str string
for _, tr := range vt {
str += tr.String() + "\n"
}
return str
}
// GetTranscript fetches the video transcript if available.
//
// Not all videos have transcripts, only relatively new videos.
// If transcripts are disabled or not available, ErrTranscriptDisabled is returned.
func (c *Client) GetTranscript(video *Video, lang string) (VideoTranscript, error) {
return c.GetTranscriptCtx(context.Background(), video, lang)
}
// GetTranscriptCtx fetches the video transcript if available.
//
// Not all videos have transcripts, only relatively new videos.
// If transcripts are disabled or not available, ErrTranscriptDisabled is returned.
func (c *Client) GetTranscriptCtx(ctx context.Context, video *Video, lang string) (VideoTranscript, error) {
c.assureClient()
if video == nil || video.ID == "" {
return nil, fmt.Errorf("no video provided")
}
body, err := c.transcriptDataByInnertube(ctx, video.ID, lang)
if err != nil {
return nil, err
}
transcript, err := parseTranscript(body)
if err != nil {
return nil, err
}
return transcript, nil
}
func parseTranscript(body []byte) (VideoTranscript, error) {
var resp transcriptResp
if err := json.Unmarshal(body, &resp); err != nil {
return nil, err
}
if len(resp.Actions) > 0 {
// Android client response
if app := resp.Actions[0].AppSegment; app != nil {
return getSegments(app)
}
// Web client response
if web := resp.Actions[0].WebSegment; web != nil {
return nil, fmt.Errorf("not implemented")
}
}
return nil, ErrTranscriptDisabled
}
type segmenter interface {
ParseSegments() []TranscriptSegment
}
func getSegments(f segmenter) (VideoTranscript, error) {
if segments := f.ParseSegments(); len(segments) > 0 {
return segments, nil
}
return nil, ErrTranscriptDisabled
}
// transcriptResp is the JSON structure as returned by the transcript API.
type transcriptResp struct {
Actions []struct {
AppSegment *appData `json:"elementsCommand"`
WebSegment *webData `json:"updateEngagementPanelAction"`
} `json:"actions"`
}
type appData struct {
TEC struct {
Args struct {
ListArgs struct {
Ow struct {
InitialSeg []struct {
TranscriptSegment struct {
StartMs string `json:"startMs"`
EndMs string `json:"endMs"`
Text struct {
String struct {
// Content is the actual transctipt text
Content string `json:"content"`
} `json:"elementsAttributedString"`
} `json:"snippet"`
StartTimeText struct {
String struct {
// Content is the fomratted timestamp, e.g. '4:00'
Content string `json:"content"`
} `json:"elementsAttributedString"`
} `json:"startTimeText"`
} `json:"transcriptSegmentRenderer"`
} `json:"initialSegments"`
} `json:"overwrite"`
} `json:"transformTranscriptSegmentListArguments"`
} `json:"arguments"`
} `json:"transformEntityCommand"`
}
func (s *appData) ParseSegments() []TranscriptSegment {
rawSegments := s.TEC.Args.ListArgs.Ow.InitialSeg
segments := make([]TranscriptSegment, 0, len(rawSegments))
for _, segment := range rawSegments {
startMs, _ := strconv.Atoi(segment.TranscriptSegment.StartMs)
endMs, _ := strconv.Atoi(segment.TranscriptSegment.EndMs)
segments = append(segments, TranscriptSegment{
Text: segment.TranscriptSegment.Text.String.Content,
StartMs: startMs,
OffsetText: segment.TranscriptSegment.StartTimeText.String.Content,
Duration: endMs - startMs,
})
}
return segments
}
type webData struct {
Content struct {
TR struct {
Body struct {
TBR struct {
Cues []struct {
Transcript struct {
FormattedStartOffset struct {
SimpleText string `json:"simpleText"`
} `json:"formattedStartOffset"`
Cues []struct {
TranscriptCueRenderer struct {
Cue struct {
SimpleText string `json:"simpleText"`
} `json:"cue"`
StartOffsetMs string `json:"startOffsetMs"`
DurationMs string `json:"durationMs"`
} `json:"transcriptCueRenderer"`
} `json:"cues"`
} `json:"transcriptCueGroupRenderer"`
} `json:"cueGroups"`
} `json:"transcriptSearchPanelRenderer"`
} `json:"content"`
} `json:"transcriptRenderer"`
} `json:"content"`
}
func (s *webData) ParseSegments() []TranscriptSegment {
// TODO: doesn't actually work now, check json.
cues := s.Content.TR.Body.TBR.Cues
segments := make([]TranscriptSegment, 0, len(cues))
for _, s := range cues {
formatted := s.Transcript.FormattedStartOffset.SimpleText
segment := s.Transcript.Cues[0].TranscriptCueRenderer
start, _ := strconv.Atoi(segment.StartOffsetMs)
duration, _ := strconv.Atoi(segment.DurationMs)
segments = append(segments, TranscriptSegment{
Text: segment.Cue.SimpleText,
StartMs: start,
OffsetText: formatted,
Duration: duration,
})
}
return segments
}