-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathplayer.html
297 lines (260 loc) · 8.56 KB
/
player.html
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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>AniTeams -Watch</title>
<!-- Artplayer CSS -->
<style>
html, body {
height: 100%;
margin: 0;
padding: 0;
}
.container {
width: 100vw;
height: 100vh;
display: flex;
justify-content: center; /* Center horizontally */
align-items: center; /* Center vertically */
background: #000; /* Background color if needed */
}
.artplayer-app {
width: 100%;
height: 100%;
max-width: 1240px; /* Maximum width of the player */
max-height: 605px; /* Maximum height of the player */
background: #000; /* Background color */
box-sizing: border-box; /* Include padding and border in element's total width and height */
}
/* Media queries for different screen sizes */
@media (max-width: 768px) {
.container {
padding: 10px; /* Optional padding for smaller screens */
}
.artplayer-app {
max-width: 100%;
max-height: 100%;
}
}
@media (max-width: 1024px) {
.container {
padding: 10px; /* Optional padding for medium screens */
}
.artplayer-app {
max-width: 100%;
max-height: 100%;
}
}
</style>
<!-- Artplayer Script -->
<script src="https://cdn.jsdelivr.net/npm/artplayer/dist/artplayer.js"></script>
<!-- HLS.js Script -->
<script src="https://cdn.jsdelivr.net/npm/hls.js@latest"></script>
<!-- Chromecast Plugin Script -->
<script src="https://cdn.jsdelivr.net/npm/artplayer-plugin-chromecast/dist/artplayer-plugin-chromecast.js"></script>
<!-- VTT Thumbnail Plugin Script -->
<script src="https://cdn.jsdelivr.net/npm/artplayer-plugin-vtt-thumbnail/dist/artplayer-plugin-vtt-thumbnail.js"></script>
</head>
<body>
<!-- Container for the Artplayer -->
<div class="artplayer-app"></div>
<script>
document.addEventListener('DOMContentLoaded', () => {
const baseApiUrl = 'https://aniwatch-api-net.vercel.app/api/v2/hianime/episode/sources';
let category = 'dub';
let art = null;
const getQueryParam = (param) => {
const urlParams = new URLSearchParams(window.location.search);
return urlParams.get(param);
};
const id = getQueryParam('id');
if (!id) {
console.error('No ID provided in URL');
return;
}
const fetchVideoData = (category) => {
const apiUrl = `${baseApiUrl}?animeEpisodeId=${id}&server=hd-1&category=${category}`;
console.log(`Fetching video data from: ${apiUrl}`);
return fetch(apiUrl)
.then(response => {
if (!response.ok) throw new Error('Network response was not ok');
return response.json();
})
.then(data => {
if (data.status === 502) return null;
return {
sources: data.data.sources,
tracks: data.data.tracks,
};
})
.catch(error => console.error('Error fetching API data:', error));
};
const convertVTTFormat = async (url) => {
try {
const response = await fetch(url);
const text = await response.text();
const lines = text.split('\n');
let converted = ['WEBVTT', ''];
const baseUrl = url.substring(0, url.lastIndexOf('/') + 1);
for (let line of lines) {
line = line.trim();
if (!line || line === 'WEBVTT') continue;
if (/^\d+$/.test(line)) continue;
if (line.includes('-->')) {
const [start, end] = line.split('-->').map(time => {
const match = time.trim().match(/(\d{2}):(\d{2}):(\d{2})\.(\d{3})/);
if (match) {
const [_, hh, mm, ss, ms] = match;
const totalSeconds = parseInt(hh) * 3600 + parseInt(mm) * 60 + parseInt(ss);
const minutes = Math.floor(totalSeconds / 60);
const seconds = totalSeconds % 60;
return `${minutes.toString().padStart(2, '0')}:${seconds.toString().padStart(2, '0')}.${ms}`;
}
return time.trim();
});
converted.push(`${start} --> ${end}`);
} else if (line.includes('#xywh=')) {
const [imageName, coordinates] = line.split('#');
converted.push(`${baseUrl}${imageName}#${coordinates}`);
}
}
const blob = new Blob([converted.join('\n')], { type: 'text/vtt' });
return URL.createObjectURL(blob);
} catch (error) {
console.error('Error converting VTT format:', error);
return null;
}
};
const initializePlayer = async (data) => {
if (!data || !data.sources) {
console.error('Invalid data received from API:', data);
return;
}
const videoSource = data.sources[0].url;
const subtitleTracks = data.tracks.filter(track => track.kind === 'captions');
const thumbnailTrack = data.tracks.find(track => track.kind === 'thumbnails');
if (art) art.destroy();
art = new Artplayer({
container: '.artplayer-app',
url: videoSource,
type: 'm3u8',
screenshot: true,
setting: true,
autoplay: true,
fullscreen: true,
fullscreenWeb: true,
airplay: true,
playsInline: true,
miniProgressBar: true,
pip:true,
plugins: [artplayerPluginChromecast({})],
settings: [
{
html: 'Speed',
selector: [
{ html: '1.0x', playbackRate: 1.0, default: true },
{ html: '1.5x', playbackRate: 1.5 },
{ html: '2.0x', playbackRate: 2.0 },
],
onSelect: (item) => {
art.playbackRate = item.playbackRate;
return item.html;
},
},
{
html: 'Subtitles',
selector: subtitleTracks.map(track => ({
html: track.label,
url: track.file,
default: track.default || false,
})),
onSelect: (item) => {
art.subtitle.switch(item.url, item.html);
return item.html;
},
},
{
html: 'Audio',
selector: [
{ html: 'Sub', category: 'sub', default: category === 'sub' },
{ html: 'Dub', category: 'dub', default: category === 'dub' },
],
onSelect: (item) => {
category = item.category;
fetchVideoData(category).then(data => {
if (!data) {
const otherCategory = category === 'sub' ? 'dub' : 'sub';
fetchVideoData(otherCategory).then(data => {
if (!data) {
alert(`Error loading both ${category} and ${otherCategory}`);
} else {
initializePlayer(data);
}
});
} else {
initializePlayer(data);
}
});
return item.html;
},
},
],
customType: {
m3u8: (video, url) => {
if (Hls.isSupported()) {
const hls = new Hls();
hls.loadSource(url);
hls.attachMedia(video);
hls.on(Hls.Events.MANIFEST_PARSED, () => video.play());
} else if (video.canPlayType('application/vnd.apple.mpegurl')) {
video.src = url;
video.addEventListener('loadedmetadata', () => video.play());
} else {
art.notice.show('Does not support playback of m3u8');
}
},
},
moreVideoAttr: {
'webkit-playsinline': true,
'playsinline': true,
},
});
if (thumbnailTrack) {
const thumbnailVTT = await convertVTTFormat(thumbnailTrack.file);
if (thumbnailVTT) {
art.plugins.add(artplayerPluginVttThumbnail({ vtt: thumbnailVTT }));
}
}
const saveKey = `video-progress-${id}`;
const savedTime = localStorage.getItem(saveKey);
if (savedTime) {
art.currentTime = parseFloat(savedTime);
art.notice.show(`Resuming from ${savedTime} seconds`);
}
art.on('video:timeupdate', () => {
const currentTime = art.currentTime;
localStorage.setItem(saveKey, currentTime);
});
art.on('video:ended', () => {
localStorage.removeItem(saveKey);
});
};
fetchVideoData(category).then(data => {
if (!data) {
const otherCategory = category === 'sub' ? 'dub' : 'sub';
fetchVideoData(otherCategory).then(data => {
if (!data) {
alert(`Error loading both ${category} and ${otherCategory}`);
} else {
initializePlayer(data);
}
});
} else {
initializePlayer(data);
}
});
});
</script>
</body>
</html>