aboutsummaryrefslogtreecommitdiffstats
path: root/src/client/players/RawSubs.hx
blob: 4fcf3b6cc75244542b48cd5ba8c80cb7217a5d85 (plain) (blame)
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
package client.players;

import Types.VideoItem;
import haxe.crypto.Base64;
import haxe.io.Bytes;
import js.Browser.document;
import js.Browser.window;
import js.html.VideoElement;

using StringTools;

private typedef Duration = {
	h:Int,
	m:Int,
	s:Int,
	ms:Int
}

class RawSubs {
	public static function loadSubs(item:VideoItem, video:VideoElement):Void {
		final ext = PathTools.urlExtension(item.subs);
		// do not load subs if there is custom plugin
		if (JsApi.hasSubtitleSupport(ext)) return;
		final url = '/proxy?url=${encodeURI(item.subs)}';

		switch ext {
			case "ass":
			case "srt":
				parseSrt(video, url);
			case "vtt":
				onParsed(video, "VTT subtitles", url);
				// parseVtt(video, url);
		}
	}

	static function parseSrt(video:VideoElement, url:String):Void {
		window.fetch(url).then(response -> {
			return response.text();
		}).then(text -> {
			final subs:Array<{
				counter:String,
				time:String,
				text:String
			}> = [];
			final blocks = text.replace("\r\n", "\n").split("\n\n");
			for (block in blocks) {
				final lines = block.split("\n");
				if (lines.length < 3) continue;
				final textLines = [
					for (i in 2...lines.length) lines[i]
				];
				subs.push({
					counter: lines[0],
					time: lines[1].replace(",", "."),
					text: textLines.join("\n")
				});
			}
			var data = "WEBVTT\n\n";
			for (sub in subs) {
				data += '${sub.counter}\n';
				data += '${sub.time}\n';
				data += '${sub.text}\n\n';
			}
			trace(data);
			final textBase64 = "data:text/plain;base64,";
			final url = textBase64 + Base64.encode(Bytes.ofString(data));
			onParsed(video, "SRT subtitles", url);
		});
	}

	// function saveSubs() {
	// 	final options = [
	// 		for (i in 0...subsSelect.options.length)
	// 			subsSelect.item(i)
	// 	];
	// 	var inputId = (cast el("#id-offset") : InputElement).value;
	// 	var id = Std.parseInt(inputId);
	// 	if (id == null) id = 1;
	// 	final data:Array<String> = options.map(element -> {
	// 		final value = element.textContent;
	// 		final firstTime = Std.parseFloat(value.split("|")[0]);
	// 		final secondTime = Std.parseFloat(value.split("|")[1]);
	// 		// 00:00:00,498 --> 00:00:02,827
	// 		var time = '$id\n';
	// 		time += srvTimeFormat(stringDuration(firstTime));
	// 		time += " --> ";
	// 		time += srvTimeFormat(stringDuration(secondTime));
	// 		time += '\ntext$id\n';
	// 		id++;
	// 		return time;
	// 	});
	// 	final data = data.join("\n");
	// 	Utils.saveFile("subs.srv", TextPlain, data);
	// }

	function stringDuration(seconds:Float):Duration {
		final h = Std.int(seconds / 60 / 60);
		final m = Std.int(seconds / 60) - h * 60;
		final s = Std.int(seconds % 60);
		final ms = Std.int((seconds - Std.int(seconds)) * 1000);
		return {
			h: h,
			m: m,
			s: s,
			ms: ms
		}
	}

	function srvTimeFormat(time:Duration):String {
		final h = '${time.h}'.lpad("0", 2);
		final m = '${time.m}'.lpad("0", 2);
		final s = '${time.s}'.lpad("0", 2);
		final ms = '${time.ms}'.rpad("0", 3);
		return '$h:$m:$s,$ms';
	}

	static function parseVtt(video:VideoElement, url:String):Void {
		window.fetch(url).then(response -> response.text()).then(data -> {
			final textBase64 = "data:text/plain;base64,";
			final url = textBase64 + Base64.encode(Bytes.ofString(data));
			onParsed(video, "VTT subtitles", url);
		});
	}

	static function onParsed(video:VideoElement, name:String, dataUrl:String) {
		final trackEl = document.createTrackElement();
		trackEl.label = name;
		trackEl.kind = "subtitles";
		trackEl.src = dataUrl;
		trackEl.default_ = true;
		final track = trackEl.track;
		track.mode = SHOWING;
		video.appendChild(trackEl);
	}

	static function encodeURI(data:String):String {
		return js.Syntax.code("encodeURI({0})", data);
	}
}
send patches to the email below
yukais@pinapelz.com
include the subject [PATCH repo_name]
pinapelz.com
homepage