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
+76 -52

No files matched your search

@@ -46,7 +46,7 @@ const Player: FC<Props> = ({ videos, subtitles, magnet }) => {
return (
<MediaPlayer
autoPlay
playsInline
load='eager'
storage='movie-advisor'
ref={player}
@@ -1,3 +1,4 @@
import { useEffect, useRef } from 'react';
import { create, InstanceProps } from 'react-modal-promise';
import { Loader } from 'lucide-react';
import dynamic from 'next/dynamic';
@@ -19,26 +20,10 @@ interface Props extends InstanceProps<void> {
}
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 (
<Modal className='overflow-hidden border-none bg-black p-0' onClose={onResolve}>
<div className='relative w-full overflow-hidden pt-[56.25%]'>
@@ -48,12 +33,7 @@ const showPlayer = create(({ onResolve, magnet, hash, backdrop, title }: Props)
{!isReady && (
<div className='absolute left-0 top-0 size-full'>
<img src={backdrop} className='absolute left-0 top-0 size-full' alt={title} />
<div
className='absolute left-0 top-0 flex size-full items-center justify-center bg-black/60 transition-all'
style={{
height: `${100 - preloadingProgress}%`
}}
>
<div className='absolute left-0 top-0 flex size-full items-center justify-center bg-black/60 transition-all'>
{!preloadingProgress && !peers && (
<div className='animate-spin'>
<Loader color='white' />
@@ -63,8 +43,8 @@ const showPlayer = create(({ onResolve, magnet, hash, backdrop, title }: Props)
{(downloadSpeed || peers) && (
<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'>
{Boolean(peers) && <p>Peers: {peers}</p>}
{Boolean(downloadSpeed) && <p>Download speed: {downloadSpeed}</p>}
<p className='text-white/90'>Buffering: {preloadingProgress.toFixed()}%</p>
<p className='text-white/90'>Download speed: {downloadSpeed || '0 B/s'}</p>
</div>
</div>
)}
@@ -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<Stream>({
queryKey: ['files', magnet],
queryFn: () => fetch(`${streamUrl}&stat&preload`).then((response) => response.json())
});
return {
...query,
data: (query.data?.file_stats || []).reduce<Sources>(
(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>(
(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;
@@ -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`, {
@@ -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<Props> = ({ 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);