Skip to content

Commit cdb8e90

Browse files
committed
Fetch subtitles when available
1 parent 6bc6f91 commit cdb8e90

File tree

5 files changed

+70
-17
lines changed

5 files changed

+70
-17
lines changed

src/iinvidious/config.c

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
extern unsigned char scrw;
1515
char *translit_charset;
1616
char video_size;
17+
char enable_subtitles;
18+
1719
char tmp_buf[80];
1820

1921
#pragma code-name(push, "LOWCODE")
@@ -40,9 +42,10 @@ static int save_config(void) {
4042
return -1;
4143
}
4244

43-
r = fprintf(fp, "%s\n%d\n",
45+
r = fprintf(fp, "%s\n%d\n%d\n",
4446
translit_charset,
45-
video_size);
47+
video_size,
48+
enable_subtitles);
4649

4750
if (r < 0 || fclose(fp) != 0) {
4851
cputs("Could not save settings file.\r\n");
@@ -105,6 +108,9 @@ void config(void) {
105108
cputs("\r\nVideo size (Small - more FPS / Large - less FPS)? (s/l)\r\n");
106109
video_size = get_bool('l', 's');
107110

111+
cputs("\r\nEnable subtitles? (y/n)\r\n");
112+
enable_subtitles = get_bool('y', 'n');
113+
108114
save_config();
109115
}
110116

@@ -116,6 +122,7 @@ void load_config(void) {
116122

117123
translit_charset = US_CHARSET;
118124
video_size = 0;
125+
enable_subtitles = 1;
119126

120127
cputs("Loading config...\r\n");
121128
fp = open_config("r");
@@ -128,14 +135,13 @@ void load_config(void) {
128135
if (strchr(tmp_buf, '\n')) {
129136
*strchr(tmp_buf, '\n') = '\0';
130137
}
131-
#ifdef __APPLE2ENH__
132138
translit_charset = strdup(tmp_buf);
133-
#endif
134139

135140
fgets(tmp_buf, 16, fp);
136-
#ifdef __APPLE2ENH__
137141
video_size = (tmp_buf[0]-'0');
138-
#endif
142+
143+
fgets(tmp_buf, 16, fp);
144+
enable_subtitles = (tmp_buf[0] != '0');
139145

140146
fclose(fp);
141147
}

src/iinvidious/config.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77

88
extern char *translit_charset;
99
extern char video_size;
10+
extern char enable_subtitles;
11+
extern char tmp_buf[80];
12+
1013
void config(void);
1114
void load_config(void);
1215

src/iinvidious/main.c

Lines changed: 50 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,13 @@
4747

4848
#define PING_API_ENDPOINT "/api/v1/ping"
4949

50-
#define PEERTUBE_SEARCH_API_ENDPOINT "/api/v1/search/videos?search=%s"
50+
#define PEERTUBE_SEARCH_API_ENDPOINT "/api/v1/search/videos?sort=match&search=%s"
5151
#define PEERTUBE_VIDEO_DETAILS_API_ENDPOINT "/api/v1/videos/%s"
52+
#define PEERTUBE_CAPTIONS_API_ENDPOINT "/api/v1/videos/%s/captions"
5253

5354
#define INVIDIOUS_SEARCH_API_ENDPOINT "/api/v1/search?type=video&sort=relevance&q=%s"
5455
#define INVIDIOUS_VIDEO_DETAILS_API_ENDPOINT "/api/v1/videos/%s?local=true"
56+
#define INVIDIOUS_CAPTIONS_API_ENDPOINT "/api/v1/captions/%s"
5557

5658
/* Peertube's .previewPath is an absolute URL without domain, but this can be
5759
* not addressed as the thumbnail request comes right after the video details
@@ -62,6 +64,11 @@ static const char *VIDEO_DETAILS_JSON_SELECTOR[] = {
6264
".[]|.title,.videoId,.author,(.videoThumbnails[]|select(.quality == \"medium\")|.url),.lengthSeconds"
6365
};
6466

67+
static const char *CAPTIONS_JSON_SELECTOR[] = {
68+
"(.data[]|select(.language.id == \"en\")|.captionPath),.data[0].captionPath",
69+
"(.captions[]|select(.label==\"English\")|.url),.captions[0].url"
70+
};
71+
6572
#define N_VIDEO_DETAILS 5
6673
#define VIDEO_NAME 0
6774
#define VIDEO_ID 1
@@ -102,11 +109,12 @@ static void load_indicator(char on) {
102109
}
103110

104111
static void load_video(char *id) {
112+
strcpy(tmp_buf, id); /* Make it safe, id points to BUF_8K */
105113
load_indicator(1);
106114
if (instance_type == PEERTUBE)
107-
sprintf((char *)BUF_1K_ADDR, "%s" PEERTUBE_VIDEO_DETAILS_API_ENDPOINT, url, id);
115+
sprintf((char *)BUF_1K_ADDR, "%s" PEERTUBE_VIDEO_DETAILS_API_ENDPOINT, url, tmp_buf);
108116
else
109-
sprintf((char *)BUF_1K_ADDR, "%s" INVIDIOUS_VIDEO_DETAILS_API_ENDPOINT, url, id);
117+
sprintf((char *)BUF_1K_ADDR, "%s" INVIDIOUS_VIDEO_DETAILS_API_ENDPOINT, url, tmp_buf);
110118

111119
surl_start_request(SURL_METHOD_GET, (char *)BUF_1K_ADDR, NULL, 0);
112120

@@ -117,10 +125,46 @@ static void load_video(char *id) {
117125
goto out;
118126
}
119127

120-
if (surl_get_json((char *)BUF_1K_ADDR, BUF_1K_SIZE, SURL_HTMLSTRIP_NONE, translit_charset,
128+
if (surl_get_json((char *)BUF_8K_ADDR, BUF_8K_SIZE, SURL_HTMLSTRIP_NONE, translit_charset,
121129
VIDEO_URL_JSON_SELECTOR[instance_type]) > 0) {
130+
char *captions = NULL;
131+
132+
if (enable_subtitles) {
133+
if (instance_type == PEERTUBE)
134+
sprintf((char *)BUF_1K_ADDR, "%s" PEERTUBE_CAPTIONS_API_ENDPOINT, url, tmp_buf);
135+
else
136+
sprintf((char *)BUF_1K_ADDR, "%s" INVIDIOUS_CAPTIONS_API_ENDPOINT, url, tmp_buf);
137+
138+
surl_start_request(SURL_METHOD_GET, (char *)BUF_1K_ADDR, NULL, 0);
139+
140+
if (surl_response_ok()) {
141+
int url_len = strlen(url);
142+
143+
/* Prefix first result with instance URL */
144+
strcpy((char *)BUF_1K_ADDR, url);
145+
/* Get JSON right after the instance URL */
146+
if (surl_get_json((char *)(BUF_1K_ADDR + url_len), BUF_1K_SIZE - url_len,
147+
SURL_HTMLSTRIP_NONE, translit_charset,
148+
CAPTIONS_JSON_SELECTOR[instance_type]) > 0) {
149+
char *eol = strchr((char *)BUF_1K_ADDR, '\n');
150+
/* Cut at end of first match */
151+
if (eol) {
152+
*eol = '\0';
153+
}
154+
/* If we had an absolute URL without host */
155+
if (((char *)BUF_1K_ADDR)[url_len] == '/') {
156+
/* Pass our built URL */
157+
captions = (char *)BUF_1K_ADDR;
158+
} else {
159+
/* Pass what the server returned */
160+
captions = (char *)(BUF_1K_ADDR + url_len);
161+
}
162+
}
163+
}
164+
165+
}
122166
load_indicator(0);
123-
stream_url((char *)BUF_1K_ADDR);
167+
stream_url((char *)BUF_8K_ADDR, captions);
124168

125169
backup_restore_logo("r");
126170
videomode(VIDEOMODE_80COL);
@@ -335,7 +379,7 @@ int main(void) {
335379

336380
screensize(&scrw, &scrh);
337381
set_scrollwindow(20, scrh);
338-
382+
339383
surl_ping();
340384
load_config();
341385

src/iinvidious/videoplay.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,19 +26,17 @@ static void update_progress(void) {
2626
cprintf("(About %ds remaining) ", eta*8);
2727
}
2828

29-
int stream_url(char *url) {
29+
int stream_url(char *url, char *subtitles_url) {
3030
char r;
3131

3232
surl_start_request(SURL_METHOD_STREAM_AV, url, NULL, 0);
3333
simple_serial_write(translit_charset, strlen(translit_charset));
3434
simple_serial_putc('\n');
3535
simple_serial_putc(1); /* Monochrome */
36-
simple_serial_putc(0); /* Enable subtitles */
36+
simple_serial_putc(subtitles_url != NULL); /* Enable subtitles */
3737
simple_serial_putc(video_size);
3838

3939
clrscr();
40-
/* clear text page 2 */
41-
memset((char*)0x800, ' '|0x80, 0x400);
4240

4341
cputs("Loading...\r\n"
4442
"Controls: Space: Play/Pause, Esc: Quit player,\r\n"
@@ -65,6 +63,8 @@ int stream_url(char *url) {
6563
goto wait_load;
6664

6765
} else if (r == SURL_ANSWER_STREAM_START) {
66+
/* clear text page 2 */
67+
memset((char*)0x800, ' '|0x80, 0x400);
6868
videomode(VIDEOMODE_40COL);
6969
hgr_mixoff();
7070
clrscr();

src/iinvidious/videoplay.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#ifndef __videoplay_h
22
#define __videoplay_h
33

4-
int stream_url(char *url);
4+
int stream_url(char *url, char *subtitles_url);
55

66
#endif

0 commit comments

Comments
 (0)