-
Notifications
You must be signed in to change notification settings - Fork 25
/
webvtt.c
245 lines (206 loc) · 4.93 KB
/
webvtt.c
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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
/* WebVTT parser
Copyright 2011 Mozilla Foundation
This Source Code Form is subject to the terms of the Mozilla
Public License, v. 2.0. If a copy of the MPL was not distributed
with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include "webvtt.h"
#define BUFFER_SIZE 4096
#ifndef MIN
#define MIN(x, y) (((x) < (y)) ? (x) : (y))
#endif
struct webvtt_parser {
int state;
char *buffer;
long offset, length;
};
webvtt_parser *
webvtt_parse_new(void)
{
webvtt_parser *ctx = malloc(sizeof(*ctx));
if (ctx) {
ctx->state = 0;
ctx->buffer = malloc(BUFFER_SIZE);
if (ctx->buffer == NULL) {
free(ctx);
return NULL;
}
ctx->offset = 0;
ctx->length = 0;
}
return ctx;
}
void
webvtt_parse_free(webvtt_parser *ctx)
{
if (ctx) {
ctx->state = 0;
if (ctx->buffer) {
free(ctx->buffer);
ctx->buffer = NULL;
}
ctx->offset = 0;
ctx->length = 0;
}
}
int
webvtt_print_cue(FILE *out, webvtt_cue *cue)
{
int err;
int h, m, s, ms;
long time;
if (out == NULL || cue == NULL)
return -1;
time = cue->start;
h = time/3600000;
time %= 3600000;
m = time/60000;
time %= 60000;
s = time/1000;
ms = time%1000;
err = fprintf(out, "%02d:%02d:%02d.%03d", h, m, s, ms);
time = cue->end;
h = time/3600000;
time %= 3600000;
m = time/60000;
time %= 60000;
s = time/1000;
err = fprintf(out, " --> %02d:%02d:%02d.%03d\n", h, m, s, ms);
err = fprintf(out, "%s\n\n", cue->text);
return err;
}
webvtt_cue *
webvtt_parse_cue(webvtt_parser *ctx)
{
webvtt_cue *cue = NULL;
if (ctx == NULL)
return NULL;
if (ctx->buffer == NULL || ctx->length - ctx->offset < 24)
return NULL;
char *p = ctx->buffer + ctx->offset;
while (p - ctx->buffer < ctx->length && isspace(*p))
p++;
int smin,ssec,smsec;
int emin,esec,emsec;
int items = sscanf(p, "%d:%d.%d --> %d:%d.%d",
&smin, &ssec, &smsec, &emin, &esec, &emsec);
if (items < 6) {
fprintf(stderr, "Couldn't parse cue timestamps\n");
return NULL;
}
double start_time = smin*60 + ssec + smsec * 1e-3;
double end_time = emin*60 + esec + emsec * 1e-3;
while (p - ctx->buffer < ctx->length && *p != '\r' && *p != '\n')
p++;
p++;
char *e = p;
while (e - ctx->buffer + 4 < ctx->length) {
if (!memcmp(e, "\n\n", 2))
break;
if (!memcmp(e, "\r\n\r\n", 4))
break;
if (!memcmp(e, "\r\r", 2))
break;
e++;
}
// TODO: handle last four bytes properly
while (e - ctx->buffer < ctx->length && *e != '\n' && *e != '\r')
e++;
char *text = malloc(e - p + 1);
if (text == NULL) {
fprintf(stderr, "Couldn't allocate cue text buffer\n");
return NULL;
}
cue = malloc(sizeof(*cue));
if (cue == NULL) {
fprintf(stderr, "Couldn't allocate cue structure\n");
free(text);
return NULL;
}
memcpy(text, p, e - p);
text[e - p] = '\0';
cue->start = start_time * 1e3;
cue->end = end_time * 1e3;
cue->text = text;
cue->next = NULL;
ctx->offset = e - ctx->buffer;
return cue;
}
webvtt_cue *
webvtt_parse(webvtt_parser *ctx)
{
webvtt_cue *cue = NULL;
char *p = ctx->buffer;
// Check for signature
if (ctx->length < 6) {
fprintf(stderr, "Too short. Not a webvtt file\n");
return NULL;
}
if (p[0] == (char)0xef && p[1] == (char)0xbb && p[2] == (char)0xbf) {
fprintf(stderr, "Byte order mark\n");
p += 3;
if (ctx->length < 9) {
fprintf(stderr, "Too short. Not a webvtt file\n");
return NULL;
}
}
if (memcmp(p, "WEBVTT", 6)) {
fprintf(stderr, "Bad magic. Not a webvtt file?\n");
return NULL;
}
p += 6;
fprintf(stderr, "Found signature\n");
// skip whitespace
while (p - ctx->buffer < ctx->length && isspace(*p))
p++;
ctx->offset = p - ctx->buffer;
cue = webvtt_parse_cue(ctx);
if (cue) {
webvtt_cue *head = cue;
webvtt_cue *next;
do {
next = webvtt_parse_cue(ctx);
head->next = next;
head = next;
} while (next != NULL);
}
webvtt_cue *head = cue;
while (head != NULL) {
webvtt_print_cue(stderr, head);
head = head->next;
}
return cue;
}
webvtt_cue *
webvtt_parse_buffer(webvtt_parser *ctx, char *buffer, long length)
{
long bytes = MIN(length, BUFFER_SIZE - ctx->length);
memcpy(ctx->buffer, buffer, bytes);
ctx->length += bytes;
return webvtt_parse(ctx);
}
webvtt_cue *
webvtt_parse_file(webvtt_parser *ctx, FILE *in)
{
ctx->length = fread(ctx->buffer, 1, BUFFER_SIZE, in);
ctx->offset = 0;
if (ctx->length >= BUFFER_SIZE)
fprintf(stderr, "WARNING: truncating input at %d bytes."
" This is a bug,\n", BUFFER_SIZE);
return webvtt_parse(ctx);
}
webvtt_cue *
webvtt_parse_filename(webvtt_parser *ctx, const char *filename)
{
FILE *in = fopen(filename, "r");
webvtt_cue *cue = NULL;
if (in) {
cue = webvtt_parse_file(ctx, in);
fclose(in);
}
return cue;
}