diff --git a/components/Preview/components/Torrents/components/Player/Player.tsx b/components/Preview/components/Torrents/components/Player/Player.tsx index 34d8a97..ef24312 100644 --- a/components/Preview/components/Torrents/components/Player/Player.tsx +++ b/components/Preview/components/Torrents/components/Player/Player.tsx @@ -46,7 +46,7 @@ const Player: FC = ({ videos, subtitles, magnet }) => { return ( { } const showPlayer = create(({ onResolve, magnet, hash, backdrop, title }: Props) => { - const { - data: { subtitles, videos }, - error, - isFetched - } = useFiles({ magnet }); + const { subtitles, videos } = useFiles({ magnet, onResolve }); const { isReady, preloadingProgress, downloadSpeed, peers } = useStats({ hash }); - const { toast } = useToast(); - - if (error) { - toast({ description: 'Download Failed: unable to retrieve the video' }); - onResolve(); - } - - if (isFetched && !videos.length) { - toast({ description: 'This torrent doesn’t contain any compatible video files' }); - onResolve(); - } - return (
@@ -48,12 +33,7 @@ const showPlayer = create(({ onResolve, magnet, hash, backdrop, title }: Props) {!isReady && (
{title} -
+
{!preloadingProgress && !peers && (
@@ -63,8 +43,8 @@ const showPlayer = create(({ onResolve, magnet, hash, backdrop, title }: Props) {(downloadSpeed || peers) && (
- {Boolean(peers) &&

Peers: {peers}

} - {Boolean(downloadSpeed) &&

Download speed: {downloadSpeed}

} +

Buffering: {preloadingProgress.toFixed()}%

+

Download speed: {downloadSpeed || '0 B/s'}

)} diff --git a/components/Preview/components/Torrents/components/Player/useFiles.ts b/components/Preview/components/Torrents/components/Player/useFiles.ts index 11addf4..bec1dc0 100644 --- a/components/Preview/components/Torrents/components/Player/useFiles.ts +++ b/components/Preview/components/Torrents/components/Player/useFiles.ts @@ -1,38 +1,63 @@ +import { useEffect, useRef } from 'react'; import { useQuery } from '@tanstack/react-query'; import { useStreamUrl } from '@/hooks/useStreamUrl'; +import { useToast } from '@/hooks/useToast'; export type Source = { name: string; src: string; type: string }; type Sources = { subtitles: Source[]; videos: Source[] }; interface Props { magnet: string; + onResolve: () => void; } -const useFiles = ({ magnet }: Props) => { +const useFiles = ({ magnet, onResolve }: Props) => { const streamUrl = `${useStreamUrl()}/stream?link=${encodeURIComponent(magnet)}`; const query = useQuery({ queryKey: ['files', magnet], queryFn: () => fetch(`${streamUrl}&stat&preload`).then((response) => response.json()) }); - return { - ...query, - data: (query.data?.file_stats || []).reduce( - (sources, file) => { - if (file.path.endsWith('.mp4')) { - sources.videos.push({ src: `${streamUrl}&index=${file.id}&play`, name: file.path, type: 'video/mp4' }); - } + const data = (query.data?.file_stats || []).reduce( + (sources, file) => { + if (file.path.endsWith('.mp4')) { + sources.videos.push({ src: `${streamUrl}&index=${file.id}&play`, name: file.path, type: 'video/mp4' }); + } - if (file.path.endsWith('.srt')) { - sources.subtitles.push({ src: `${streamUrl}&index=${file.id}&play`, name: file.path, type: 'srt' }); - } + if (file.path.endsWith('.srt')) { + sources.subtitles.push({ src: `${streamUrl}&index=${file.id}&play`, name: file.path, type: 'srt' }); + } - return sources; - }, - { subtitles: [], videos: [] } - ) - }; + return sources; + }, + { subtitles: [], videos: [] } + ); + + const isResolvedRef = useRef(false); + + const { toast } = useToast(); + + useEffect(() => { + const close = () => { + if (isResolvedRef.current) return; + + isResolvedRef.current = true; + onResolve(); + }; + + if (query.error) { + toast({ description: 'Download Failed: unable to retrieve the video' }); + close(); + } + + if (query.isFetched && !data.videos.length) { + toast({ description: 'This torrent doesn’t contain any compatible video files' }); + close(); + } + }, [query.isFetched, data.videos.length, query.error]); + + return data; }; export default useFiles; diff --git a/components/Preview/components/Torrents/components/Player/useStats.ts b/components/Preview/components/Torrents/components/Player/useStats.ts index 4d509a1..7b67f10 100644 --- a/components/Preview/components/Torrents/components/Player/useStats.ts +++ b/components/Preview/components/Torrents/components/Player/useStats.ts @@ -23,7 +23,7 @@ export const useStats = ({ hash }: Props) => { refetchInterval: ({ state }) => { if (state.data?.stat === StreamStatus.ready) return false; - return 500; + return 1000; }, queryFn: () => fetch(`${streamUrl}/torrents`, { diff --git a/components/Preview/components/Torrents/components/TorrentsTable/components/Actions/index.tsx b/components/Preview/components/Torrents/components/TorrentsTable/components/Actions/index.tsx index 0e208df..0fed86f 100644 --- a/components/Preview/components/Torrents/components/TorrentsTable/components/Actions/index.tsx +++ b/components/Preview/components/Torrents/components/TorrentsTable/components/Actions/index.tsx @@ -1,11 +1,11 @@ -import { FC } from 'react'; +import { FC, useMemo } from 'react'; import { Cast, Download, ListVideo, Loader, Magnet, Play } from 'lucide-react'; import { Button } from '@/components/ui/button'; import ButtonsGroup from '@/components/ui/buttons-group'; import { Quality } from '@/data/parsers/yts/models'; import { useStreamUrl } from '@/hooks/useStreamUrl'; -import { cn, getMagnetHash } from '@/lib/utils'; +import { cn, detectSafari, getMagnetHash } from '@/lib/utils'; import { providers } from '../../../../constants'; import { useCastMagnet, usePrefix } from '../../../../hooks/useMagnetHosts'; @@ -25,12 +25,21 @@ const Actions: FC = ({ torrent, backdrop, title, provider }) => { const cast = useCastMagnet(); const streamUrl = useStreamUrl(); - const supportedForStream = Boolean( - (provider === providers.yts.key && - torrent.seeders && - [Quality.The1080P, Quality.The720P].includes(torrent.quality as Quality)) || - torrent.container?.includes('mp4') - ); + const { isIOS, isSafari } = detectSafari(); + const isHEVC = torrent.codec?.includes('265') || torrent.codec?.toLowerCase().includes('hevc'); + + const supportedForStream = useMemo(() => { + if (provider === providers.yts.key) { + return ( + [Quality.The1080P, Quality.The720P].includes(torrent.quality as Quality) && + (isHEVC ? !isIOS && !isSafari : true) + ); + } + + const isMP4 = torrent.container?.includes('mp4'); + + return isMP4 && (isHEVC ? !isIOS && !isSafari : true); + }, [isIOS, isSafari, isHEVC, torrent, provider]); const fetchMagnet = useMagnet(torrent); diff --git a/data/parsers/yts/index.ts b/data/parsers/yts/index.ts index 2c0def7..1fc72f7 100644 --- a/data/parsers/yts/index.ts +++ b/data/parsers/yts/index.ts @@ -62,7 +62,8 @@ export class YTS { source: torrent.type, year: movie.year?.toString(), magnet: `magnet:?xt=urn:btih:${torrent.hash}&dn=${encodeURIComponent(query)}&tr=${trackers.join('&tr=')}`, - hash: torrent.hash + hash: torrent.hash, + codec: torrent.video_codec }; }) || [] ); diff --git a/lib/utils.ts b/lib/utils.ts index ae77617..261a379 100644 --- a/lib/utils.ts +++ b/lib/utils.ts @@ -42,3 +42,12 @@ export const formatBytes = (bytes?: number) => { return `${v.toFixed(v < 10 && i > 0 ? 1 : 0)} ${units[i]}`; }; + +export const detectSafari = () => { + const ua = navigator.userAgent; + const isSafari = ua.includes('Safari') && !ua.includes('Chrome') && !ua.includes('CriOS') && !ua.includes('FxiOS'); + + const isIOS = /iPad|iPhone|iPod/.test(ua) || (navigator.platform === 'MacIntel' && navigator.maxTouchPoints > 1); + + return { isSafari, isIOS }; +};