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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348 | import { useEffect, useRef, useState } from 'react';
import './PlayerStyles.css';
import {
isHLSProvider,
MediaPlayer,
MediaProvider,
Poster,
Track,
type MediaProviderAdapter,
type MediaProviderChangeEvent,
type MediaPlayerInstance,
} from '@vidstack/react';
import styled from 'styled-components';
import {
fetchSkipTimes,
fetchAnimeStreamingLinks,
useSettings,
} from '../../../index';
import {
DefaultAudioLayout,
defaultLayoutIcons,
DefaultVideoLayout,
} from '@vidstack/react/player/layouts/default';
import { TbPlayerTrackPrev, TbPlayerTrackNext } from 'react-icons/tb';
import { FaCheck } from 'react-icons/fa6';
import { RiCheckboxBlankFill } from 'react-icons/ri';
const Button = styled.button<{ $autoskip?: boolean }>`
padding: 0.25rem;
font-size: 0.8rem;
border: none;
margin-right: 0.25rem;
border-radius: var(--global-border-radius);
cursor: pointer;
background-color: var(--global-div);
color: var(--global-text);
svg {
margin-bottom: -0.1rem;
color: grey;
}
@media (max-width: 500px) {
font-size: 0.7rem;
}
&.active {
background-color: var(--primary-accent);
}
${({ $autoskip }) =>
$autoskip &&
`
color: #d69e00;
svg {
color: #d69e00;
}
`}
`;
type PlayerProps = {
episodeId: string;
banner?: string;
malId?: string;
updateDownloadLink: (link: string) => void;
onEpisodeEnd: () => Promise<void>;
onPrevEpisode: () => void;
onNextEpisode: () => void;
animeTitle?: string;
};
type StreamingSource = {
url: string;
quality: string;
};
type SkipTime = {
interval: {
startTime: number;
endTime: number;
};
skipType: string;
};
type FetchSkipTimesResponse = {
results: SkipTime[];
};
export function Player({
episodeId,
banner,
malId,
updateDownloadLink,
onEpisodeEnd,
onPrevEpisode,
onNextEpisode,
animeTitle,
}: PlayerProps) {
const player = useRef<MediaPlayerInstance>(null);
const [src, setSrc] = useState<string>('');
const [vttUrl, setVttUrl] = useState<string>('');
const [currentTime, setCurrentTime] = useState<number>(0);
const [skipTimes, setSkipTimes] = useState<SkipTime[]>([]);
const [totalDuration, setTotalDuration] = useState<number>(0);
const [vttGenerated, setVttGenerated] = useState<boolean>(false);
const episodeNumber = getEpisodeNumber(episodeId);
const animeVideoTitle = animeTitle;
const { settings, setSettings } = useSettings();
const { autoPlay, autoNext, autoSkip } = settings;
useEffect(() => {
setCurrentTime(parseFloat(localStorage.getItem('currentTime') || '0'));
fetchAndSetAnimeSource();
fetchAndProcessSkipTimes();
return () => {
if (vttUrl) URL.revokeObjectURL(vttUrl);
};
}, [episodeId, malId, updateDownloadLink]);
useEffect(() => {
if (autoPlay && player.current) {
player.current
.play()
.catch((e) =>
console.log('Playback failed to start automatically:', e),
);
}
}, [autoPlay, src]);
useEffect(() => {
if (player.current && currentTime) {
player.current.currentTime = currentTime;
}
}, [currentTime]);
function onProviderChange(
provider: MediaProviderAdapter | null,
_nativeEvent: MediaProviderChangeEvent,
) {
if (isHLSProvider(provider)) {
provider.config = {};
}
}
function onLoadedMetadata() {
if (player.current) {
setTotalDuration(player.current.duration);
}
}
function onTimeUpdate() {
if (player.current) {
const currentTime = player.current.currentTime;
const duration = player.current.duration || 1;
const playbackPercentage = (currentTime / duration) * 100;
const playbackInfo = {
currentTime,
playbackPercentage,
};
const allPlaybackInfo = JSON.parse(
localStorage.getItem('all_episode_times') || '{}',
);
allPlaybackInfo[episodeId] = playbackInfo;
localStorage.setItem(
'all_episode_times',
JSON.stringify(allPlaybackInfo),
);
if (autoSkip && skipTimes.length) {
const skipInterval = skipTimes.find(
({ interval }) =>
currentTime >= interval.startTime && currentTime < interval.endTime,
);
if (skipInterval) {
player.current.currentTime = skipInterval.interval.endTime;
}
}
}
}
function generateWebVTTFromSkipTimes(
skipTimes: FetchSkipTimesResponse,
totalDuration: number,
): string {
let vttString = 'WEBVTT\n\n';
let previousEndTime = 0;
const sortedSkipTimes = skipTimes.results.sort(
(a, b) => a.interval.startTime - b.interval.startTime,
);
sortedSkipTimes.forEach((skipTime, index) => {
const { startTime, endTime } = skipTime.interval;
const skipType =
skipTime.skipType.toUpperCase() === 'OP' ? 'Opening' : 'Outro';
// Insert default title chapter before this skip time if there's a gap
if (previousEndTime < startTime) {
vttString += `${formatTime(previousEndTime)} --> ${formatTime(startTime)}\n`;
vttString += `${animeVideoTitle} - Episode ${episodeNumber}\n\n`;
}
// Insert this skip time
vttString += `${formatTime(startTime)} --> ${formatTime(endTime)}\n`;
vttString += `${skipType}\n\n`;
previousEndTime = endTime;
// Insert default title chapter after the last skip time
if (index === sortedSkipTimes.length - 1 && endTime < totalDuration) {
vttString += `${formatTime(endTime)} --> ${formatTime(totalDuration)}\n`;
vttString += `${animeVideoTitle} - Episode ${episodeNumber}\n\n`;
}
});
return vttString;
}
async function fetchAndProcessSkipTimes() {
if (malId && episodeId) {
const episodeNumber = getEpisodeNumber(episodeId);
try {
const response: FetchSkipTimesResponse = await fetchSkipTimes({
malId: malId.toString(),
episodeNumber,
});
const filteredSkipTimes = response.results.filter(
({ skipType }) => skipType === 'op' || skipType === 'ed',
);
if (!vttGenerated) {
const vttContent = generateWebVTTFromSkipTimes(
{ results: filteredSkipTimes },
totalDuration,
);
const blob = new Blob([vttContent], { type: 'text/vtt' });
const vttBlobUrl = URL.createObjectURL(blob);
setVttUrl(vttBlobUrl);
setSkipTimes(filteredSkipTimes);
setVttGenerated(true);
}
} catch (error) {
console.error('Failed to fetch skip times', error);
}
}
}
async function fetchAndSetAnimeSource() {
try {
const response = await fetchAnimeStreamingLinks(episodeId);
const backupSource = response.sources.find(
(source: StreamingSource) => source.quality === 'default',
);
if (backupSource) {
setSrc(backupSource.url);
updateDownloadLink(response.download);
} else {
console.error('Backup source not found');
}
} catch (error) {
console.error('Failed to fetch anime streaming links', error);
}
}
function formatTime(seconds: number): string {
const minutes = Math.floor(seconds / 60);
const remainingSeconds = Math.floor(seconds % 60);
return `${minutes.toString().padStart(2, '0')}:${remainingSeconds.toString().padStart(2, '0')}`;
}
function getEpisodeNumber(id: string): string {
const parts = id.split('-');
return parts[parts.length - 1];
}
const toggleAutoPlay = () =>
setSettings({ ...settings, autoPlay: !autoPlay });
const toggleAutoNext = () =>
setSettings({ ...settings, autoNext: !autoNext });
const toggleAutoSkip = () =>
setSettings({ ...settings, autoSkip: !autoSkip });
const handlePlaybackEnded = async () => {
if (!autoNext) return;
try {
player.current?.pause();
await new Promise((resolve) => setTimeout(resolve, 200)); // Delay for transition
await onEpisodeEnd();
} catch (error) {
console.error('Error moving to the next episode:', error);
}
};
return (
<div style={{ animation: 'popIn 0.25s ease-in-out' }}>
<MediaPlayer
className='player'
title={`${animeVideoTitle} - Episode ${episodeNumber}`}
src={src}
autoplay={autoPlay}
crossorigin
playsinline
onLoadedMetadata={onLoadedMetadata}
onProviderChange={onProviderChange}
onTimeUpdate={onTimeUpdate}
ref={player}
aspectRatio='16/9'
load='eager'
posterLoad='eager'
streamType='on-demand'
storage='storage-key'
keyTarget='player'
onEnded={handlePlaybackEnded}
>
<MediaProvider>
<Poster className='vds-poster' src={banner} alt='' />
{vttUrl && (
<Track kind='chapters' src={vttUrl} default label='Skip Times' />
)}
</MediaProvider>
<DefaultAudioLayout icons={defaultLayoutIcons} />
<DefaultVideoLayout icons={defaultLayoutIcons} />
</MediaPlayer>
<div
className='player-menu'
style={{
backgroundColor: 'var(--global-div-tr)',
borderRadius: 'var(--global-border-radius)',
}}
>
<Button onClick={toggleAutoPlay}>
{autoPlay ? <FaCheck /> : <RiCheckboxBlankFill />} Autoplay
</Button>
<Button $autoskip onClick={toggleAutoSkip}>
{autoSkip ? <FaCheck /> : <RiCheckboxBlankFill />} Auto Skip
</Button>
<Button onClick={onPrevEpisode}>
<TbPlayerTrackPrev /> Prev
</Button>
<Button onClick={onNextEpisode}>
<TbPlayerTrackNext /> Next
</Button>
<Button onClick={toggleAutoNext}>
{autoNext ? <FaCheck /> : <RiCheckboxBlankFill />} Auto Next
</Button>
</div>
</div>
);
}
|