feat: video format restrictions

This commit is contained in:
ner committed 2025-08-15 18:42:49 +03:00
1 parent 82c14c1d5c
commit 6fc34e3797
7 files changed
+64 -40

No files matched your search

@@ -46,7 +46,7 @@ const Player: FC<Props> = ({ videos, subtitles, magnet }) => {
return ( return (
<MediaPlayer <MediaPlayer
autoPlay playsInline
load='eager' load='eager'
storage='movie-advisor' storage='movie-advisor'
ref={player} ref={player}
@@ -1,3 +1,4 @@
import { useEffect, useRef } from 'react';
import { create, InstanceProps } from 'react-modal-promise'; import { create, InstanceProps } from 'react-modal-promise';
import { Loader } from 'lucide-react'; import { Loader } from 'lucide-react';
import dynamic from 'next/dynamic'; import dynamic from 'next/dynamic';
@@ -19,26 +20,10 @@ interface Props extends InstanceProps<void> {
} }
const showPlayer = create(({ onResolve, magnet, hash, backdrop, title }: Props) => { const showPlayer = create(({ onResolve, magnet, hash, backdrop, title }: Props) => {
const { const { subtitles, videos } = useFiles({ magnet, onResolve });
data: { subtitles, videos },
error,
isFetched
} = useFiles({ magnet });
const { isReady, preloadingProgress, downloadSpeed, peers } = useStats({ hash }); 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 ( return (
<Modal className='overflow-hidden border-none bg-black p-0' onClose={onResolve}> <Modal className='overflow-hidden border-none bg-black p-0' onClose={onResolve}>
<div className='relative w-full overflow-hidden pt-[56.25%]'> <div className='relative w-full overflow-hidden pt-[56.25%]'>
@@ -48,12 +33,7 @@ const showPlayer = create(({ onResolve, magnet, hash, backdrop, title }: Props)
{!isReady && ( {!isReady && (
<div className='absolute left-0 top-0 size-full'> <div className='absolute left-0 top-0 size-full'>
<img src={backdrop} className='absolute left-0 top-0 size-full' alt={title} /> <img src={backdrop} className='absolute left-0 top-0 size-full' alt={title} />
<div <div className='absolute left-0 top-0 flex size-full items-center justify-center bg-black/60 transition-all'>
className='absolute left-0 top-0 flex size-full items-center justify-center bg-black/60 transition-all'
style={{
height: `${100 - preloadingProgress}%`
}}
>
{!preloadingProgress && !peers && ( {!preloadingProgress && !peers && (
<div className='animate-spin'> <div className='animate-spin'>
<Loader color='white' /> <Loader color='white' />
@@ -63,8 +43,8 @@ const showPlayer = create(({ onResolve, magnet, hash, backdrop, title }: Props)
{(downloadSpeed || peers) && ( {(downloadSpeed || peers) && (
<div className='absolute left-0 top-0 flex size-full flex-col items-center justify-center text-white'> <div className='absolute left-0 top-0 flex size-full flex-col items-center justify-center text-white'>
<div className='flex h-[90px] w-[230px] flex-col items-center justify-center gap-2 rounded-xl bg-black/80 p-4 shadow-2xl'> <div className='flex h-[90px] w-[230px] flex-col items-center justify-center gap-2 rounded-xl bg-black/80 p-4 shadow-2xl'>
{Boolean(peers) && <p>Peers: {peers}</p>} <p className='text-white/90'>Buffering: {preloadingProgress.toFixed()}%</p>
{Boolean(downloadSpeed) && <p>Download speed: {downloadSpeed}</p>} <p className='text-white/90'>Download speed: {downloadSpeed || '0 B/s'}</p>
</div> </div>
</div> </div>
)} )}
@@ -1,24 +1,25 @@
import { useEffect, useRef } from 'react';
import { useQuery } from '@tanstack/react-query'; import { useQuery } from '@tanstack/react-query';
import { useStreamUrl } from '@/hooks/useStreamUrl'; import { useStreamUrl } from '@/hooks/useStreamUrl';
import { useToast } from '@/hooks/useToast';
export type Source = { name: string; src: string; type: string }; export type Source = { name: string; src: string; type: string };
type Sources = { subtitles: Source[]; videos: Source[] }; type Sources = { subtitles: Source[]; videos: Source[] };
interface Props { interface Props {
magnet: string; magnet: string;
onResolve: () => void;
} }
const useFiles = ({ magnet }: Props) => { const useFiles = ({ magnet, onResolve }: Props) => {
const streamUrl = `${useStreamUrl()}/stream?link=${encodeURIComponent(magnet)}`; const streamUrl = `${useStreamUrl()}/stream?link=${encodeURIComponent(magnet)}`;
const query = useQuery<Stream>({ const query = useQuery<Stream>({
queryKey: ['files', magnet], queryKey: ['files', magnet],
queryFn: () => fetch(`${streamUrl}&stat&preload`).then((response) => response.json()) queryFn: () => fetch(`${streamUrl}&stat&preload`).then((response) => response.json())
}); });
return { const data = (query.data?.file_stats || []).reduce<Sources>(
...query,
data: (query.data?.file_stats || []).reduce<Sources>(
(sources, file) => { (sources, file) => {
if (file.path.endsWith('.mp4')) { if (file.path.endsWith('.mp4')) {
sources.videos.push({ src: `${streamUrl}&index=${file.id}&play`, name: file.path, type: 'video/mp4' }); sources.videos.push({ src: `${streamUrl}&index=${file.id}&play`, name: file.path, type: 'video/mp4' });
@@ -31,8 +32,32 @@ const useFiles = ({ magnet }: Props) => {
return sources; return sources;
}, },
{ subtitles: [], videos: [] } { 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; export default useFiles;
@@ -23,7 +23,7 @@ export const useStats = ({ hash }: Props) => {
refetchInterval: ({ state }) => { refetchInterval: ({ state }) => {
if (state.data?.stat === StreamStatus.ready) return false; if (state.data?.stat === StreamStatus.ready) return false;
return 500; return 1000;
}, },
queryFn: () => queryFn: () =>
fetch(`${streamUrl}/torrents`, { fetch(`${streamUrl}/torrents`, {
@@ -1,11 +1,11 @@
import { FC } from 'react'; import { FC, useMemo } from 'react';
import { Cast, Download, ListVideo, Loader, Magnet, Play } from 'lucide-react'; import { Cast, Download, ListVideo, Loader, Magnet, Play } from 'lucide-react';
import { Button } from '@/components/ui/button'; import { Button } from '@/components/ui/button';
import ButtonsGroup from '@/components/ui/buttons-group'; import ButtonsGroup from '@/components/ui/buttons-group';
import { Quality } from '@/data/parsers/yts/models'; import { Quality } from '@/data/parsers/yts/models';
import { useStreamUrl } from '@/hooks/useStreamUrl'; import { useStreamUrl } from '@/hooks/useStreamUrl';
import { cn, getMagnetHash } from '@/lib/utils'; import { cn, detectSafari, getMagnetHash } from '@/lib/utils';
import { providers } from '../../../../constants'; import { providers } from '../../../../constants';
import { useCastMagnet, usePrefix } from '../../../../hooks/useMagnetHosts'; import { useCastMagnet, usePrefix } from '../../../../hooks/useMagnetHosts';
@@ -25,12 +25,21 @@ const Actions: FC<Props> = ({ torrent, backdrop, title, provider }) => {
const cast = useCastMagnet(); const cast = useCastMagnet();
const streamUrl = useStreamUrl(); const streamUrl = useStreamUrl();
const supportedForStream = Boolean( const { isIOS, isSafari } = detectSafari();
(provider === providers.yts.key && const isHEVC = torrent.codec?.includes('265') || torrent.codec?.toLowerCase().includes('hevc');
torrent.seeders &&
[Quality.The1080P, Quality.The720P].includes(torrent.quality as Quality)) || const supportedForStream = useMemo(() => {
torrent.container?.includes('mp4') 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); const fetchMagnet = useMagnet(torrent);
+2 -1
View File
@@ -62,7 +62,8 @@ export class YTS {
source: torrent.type, source: torrent.type,
year: movie.year?.toString(), year: movie.year?.toString(),
magnet: `magnet:?xt=urn:btih:${torrent.hash}&dn=${encodeURIComponent(query)}&tr=${trackers.join('&tr=')}`, magnet: `magnet:?xt=urn:btih:${torrent.hash}&dn=${encodeURIComponent(query)}&tr=${trackers.join('&tr=')}`,
hash: torrent.hash hash: torrent.hash,
codec: torrent.video_codec
}; };
}) || [] }) || []
); );
+9
View File
@@ -42,3 +42,12 @@ export const formatBytes = (bytes?: number) => {
return `${v.toFixed(v < 10 && i > 0 ? 1 : 0)} ${units[i]}`; 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 };
};