feat: play MKV/HEVC in-browser via TorrServer -gst HLS with track selection
Route MKV/WebM/AVI through TorrServer's native /gst HLS (copy-mode: HEVC kept, audio->AAC, keyframe-aligned segments with real duration + seeking) and play MP4 directly. Add an audio-track menu sourced from /gst/probe (switching reloads with &audio=N and resumes position), surface the HLS subtitle tracks through the existing captions menu, and show the Play button for every video container. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
1 parent
692f5ab4e3
commit
3538738bb9
6 files changed
+139
-21
No files matched your search
@@ -0,0 +1,34 @@
|
|||||||
|
import { FC } from 'react';
|
||||||
|
import { AudioLines } from 'lucide-react';
|
||||||
|
|
||||||
|
import { cn } from '@/lib/utils';
|
||||||
|
|
||||||
|
import { GstAudioTrack } from '../../useGstAudio';
|
||||||
|
import PlayerMenu from '../Menu';
|
||||||
|
|
||||||
|
interface Props {
|
||||||
|
tracks: GstAudioTrack[];
|
||||||
|
value: number;
|
||||||
|
onChange: (index: number) => void;
|
||||||
|
}
|
||||||
|
|
||||||
|
const AudioMenu: FC<Props> = ({ tracks, value, onChange }) => {
|
||||||
|
if (tracks.length <= 1) return null;
|
||||||
|
|
||||||
|
return (
|
||||||
|
<PlayerMenu
|
||||||
|
closeOnSelect
|
||||||
|
value={value.toString()}
|
||||||
|
options={tracks.map((track) => ({
|
||||||
|
label: track.label,
|
||||||
|
value: track.index.toString(),
|
||||||
|
onSelect: () => onChange(track.index)
|
||||||
|
}))}
|
||||||
|
>
|
||||||
|
<AudioLines size={18} className={cn('vds-icon !transform-none')} />
|
||||||
|
<span className={cn('plyr__tooltip')}>Audio</span>
|
||||||
|
</PlayerMenu>
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
|
export default AudioMenu;
|
||||||
@@ -18,6 +18,8 @@ const CaptionsMenu = () => {
|
|||||||
return Object.values(record);
|
return Object.values(record);
|
||||||
}, [tracks]);
|
}, [tracks]);
|
||||||
|
|
||||||
|
if (!captions.length) return null;
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<PlayerMenu
|
<PlayerMenu
|
||||||
closeOnSelect
|
closeOnSelect
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
'use client';
|
'use client';
|
||||||
|
|
||||||
import { FC, useEffect, useRef } from 'react';
|
import { FC, useEffect, useMemo, useRef } from 'react';
|
||||||
import {
|
import {
|
||||||
MediaPlayer,
|
MediaPlayer,
|
||||||
MediaPlayerInstance,
|
MediaPlayerInstance,
|
||||||
@@ -16,8 +16,10 @@ import { PlyrLayout, plyrLayoutIcons } from '@vidstack/react/player/layouts/plyr
|
|||||||
import { useSetCanPlay } from '@/hooks/useCanPlay';
|
import { useSetCanPlay } from '@/hooks/useCanPlay';
|
||||||
import { cn } from '@/lib/utils';
|
import { cn } from '@/lib/utils';
|
||||||
|
|
||||||
|
import Audio from './components/Audio';
|
||||||
import Captions from './components/Captions';
|
import Captions from './components/Captions';
|
||||||
import Playlist from './components/Playlist';
|
import Playlist from './components/Playlist';
|
||||||
|
import { useGstAudio } from './useGstAudio';
|
||||||
import useSource from './useSource';
|
import useSource from './useSource';
|
||||||
|
|
||||||
import '@vidstack/react/player/styles/plyr/theme.css';
|
import '@vidstack/react/player/styles/plyr/theme.css';
|
||||||
@@ -46,6 +48,17 @@ const Player: FC<Props> = ({ magnet, playlist, subtitles }) => {
|
|||||||
|
|
||||||
const source = playlist[index];
|
const source = playlist[index];
|
||||||
|
|
||||||
|
const { tracks: audioTracks, audio, setAudio, resolvedSrc } = useGstAudio(source?.src);
|
||||||
|
|
||||||
|
const resumeTime = useRef(0);
|
||||||
|
|
||||||
|
const changeAudio = (track: number) => {
|
||||||
|
resumeTime.current = player.current?.currentTime ?? 0;
|
||||||
|
setAudio(track);
|
||||||
|
};
|
||||||
|
|
||||||
|
const playerSrc = useMemo(() => (source ? ({ ...source, src: resolvedSrc } as VideoSrc) : []), [source, resolvedSrc]);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<MediaPlayer
|
<MediaPlayer
|
||||||
playsInline
|
playsInline
|
||||||
@@ -54,8 +67,14 @@ const Player: FC<Props> = ({ magnet, playlist, subtitles }) => {
|
|||||||
preload='none'
|
preload='none'
|
||||||
storage='movie-advisor'
|
storage='movie-advisor'
|
||||||
ref={player}
|
ref={player}
|
||||||
src={(source as VideoSrc) || []}
|
src={playerSrc}
|
||||||
onCanPlay={() => setCanPlay(true)}
|
onCanPlay={() => {
|
||||||
|
setCanPlay(true);
|
||||||
|
if (resumeTime.current) {
|
||||||
|
player.current!.currentTime = resumeTime.current;
|
||||||
|
resumeTime.current = 0;
|
||||||
|
}
|
||||||
|
}}
|
||||||
className={cn('relative size-full select-none')}
|
className={cn('relative size-full select-none')}
|
||||||
>
|
>
|
||||||
<MediaProvider className='relative flex size-full justify-center [&>video]:!h-full'>
|
<MediaProvider className='relative flex size-full justify-center [&>video]:!h-full'>
|
||||||
@@ -85,7 +104,12 @@ const Player: FC<Props> = ({ magnet, playlist, subtitles }) => {
|
|||||||
</PlayButton>
|
</PlayButton>
|
||||||
),
|
),
|
||||||
afterVolumeSlider: <ul className='w-2' />,
|
afterVolumeSlider: <ul className='w-2' />,
|
||||||
beforeSettings: Boolean(subtitles.length) && <Captions />,
|
beforeSettings: (
|
||||||
|
<>
|
||||||
|
<Audio tracks={audioTracks} value={audio} onChange={changeAudio} />
|
||||||
|
<Captions />
|
||||||
|
</>
|
||||||
|
),
|
||||||
settings: playlist.length > 1 && (
|
settings: playlist.length > 1 && (
|
||||||
<Playlist key={source?.src} source={source} sources={playlist} onChange={setIndex} />
|
<Playlist key={source?.src} source={source} sources={playlist} onChange={setIndex} />
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -0,0 +1,48 @@
|
|||||||
|
import { useEffect, useState } from 'react';
|
||||||
|
import { useQuery } from '@tanstack/react-query';
|
||||||
|
|
||||||
|
export interface GstAudioTrack {
|
||||||
|
index: number;
|
||||||
|
label: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
interface ProbeTrack {
|
||||||
|
Index: number;
|
||||||
|
Type: string;
|
||||||
|
Language?: string;
|
||||||
|
Title?: string;
|
||||||
|
Channels?: number;
|
||||||
|
}
|
||||||
|
|
||||||
|
const GST_RE = /^(.*)\/gst\/([0-9a-fA-F]+)\/master\.m3u8\?index=(\d+)/;
|
||||||
|
|
||||||
|
const label = ({ Language, Title, Channels }: ProbeTrack) =>
|
||||||
|
[Language?.toUpperCase(), Title, Channels ? `${Channels}ch` : ''].filter(Boolean).join(' · ') || 'Audio';
|
||||||
|
|
||||||
|
export const useGstAudio = (src?: string) => {
|
||||||
|
const match = src?.match(GST_RE);
|
||||||
|
const [base, hash, index] = match ? [match[1], match[2], match[3]] : [];
|
||||||
|
|
||||||
|
const [audio, setAudio] = useState(0);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
setAudio(0);
|
||||||
|
}, [hash, index]);
|
||||||
|
|
||||||
|
const { data: tracks = [] } = useQuery<GstAudioTrack[]>({
|
||||||
|
enabled: Boolean(hash && index),
|
||||||
|
queryKey: ['gst-audio', hash, index],
|
||||||
|
queryFn: () =>
|
||||||
|
fetch(`${base}/gst/${hash}/probe?index=${index}`)
|
||||||
|
.then((response) => response.json())
|
||||||
|
.then((data: { Tracks?: ProbeTrack[] }) =>
|
||||||
|
(data.Tracks ?? [])
|
||||||
|
.filter((track) => track.Type === 'audio')
|
||||||
|
.map((track) => ({ index: track.Index, label: label(track) }))
|
||||||
|
)
|
||||||
|
});
|
||||||
|
|
||||||
|
const resolvedSrc = match ? `${src}&audio=${audio}` : src;
|
||||||
|
|
||||||
|
return { tracks, audio, setAudio, resolvedSrc };
|
||||||
|
};
|
||||||
+18
-8
@@ -1,4 +1,4 @@
|
|||||||
import { useMutation, useQuery } from '@tanstack/react-query';
|
import { useQuery } from '@tanstack/react-query';
|
||||||
|
|
||||||
import { useStreamUrl } from '@/hooks/useStreamUrl';
|
import { useStreamUrl } from '@/hooks/useStreamUrl';
|
||||||
|
|
||||||
@@ -6,8 +6,13 @@ interface Props {
|
|||||||
magnet: string;
|
magnet: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const NATIVE_EXT = /\.(mp4|m4v|mov)$/i;
|
||||||
|
const HLS_EXT = /\.(mkv|webm|avi|ts|m2ts)$/i;
|
||||||
|
|
||||||
export const useFiles = ({ magnet }: Props) => {
|
export const useFiles = ({ magnet }: Props) => {
|
||||||
const streamUrl = `${useStreamUrl()}/stream?link=${encodeURIComponent(magnet)}`;
|
const base = useStreamUrl();
|
||||||
|
const link = encodeURIComponent(magnet);
|
||||||
|
const streamUrl = `${base}/stream?link=${link}`;
|
||||||
|
|
||||||
const { data } = useQuery<Sources>({
|
const { data } = useQuery<Sources>({
|
||||||
queryKey: ['sources', magnet],
|
queryKey: ['sources', magnet],
|
||||||
@@ -18,18 +23,23 @@ export const useFiles = ({ magnet }: Props) => {
|
|||||||
|
|
||||||
return response.json();
|
return response.json();
|
||||||
})
|
})
|
||||||
.then(({ file_stats: stats }: Stream) => {
|
.then(({ file_stats: stats, hash }: Stream) => {
|
||||||
const sources = stats.reduce<Sources>(
|
const sources = stats.reduce<Sources>(
|
||||||
(sources, file) => {
|
(sources, file) => {
|
||||||
const src = `${streamUrl}&index=${file.id}&play`;
|
|
||||||
const name = file.path;
|
const name = file.path;
|
||||||
|
|
||||||
if (file.path.endsWith('.mp4')) {
|
if (NATIVE_EXT.test(name)) {
|
||||||
sources.playlist.push({ src, name, type: 'video/mp4' });
|
sources.playlist.push({ src: `${streamUrl}&index=${file.id}&play`, name, type: 'video/mp4' });
|
||||||
|
} else if (HLS_EXT.test(name)) {
|
||||||
|
sources.playlist.push({
|
||||||
|
src: `${base}/gst/${hash}/master.m3u8?index=${file.id}`,
|
||||||
|
name,
|
||||||
|
type: 'application/x-mpegurl'
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
if (file.path.endsWith('.srt')) {
|
if (name.endsWith('.srt')) {
|
||||||
sources.subtitles.push({ src, name, type: 'srt' });
|
sources.subtitles.push({ src: `${streamUrl}&index=${file.id}&play`, name, type: 'srt' });
|
||||||
}
|
}
|
||||||
|
|
||||||
return sources;
|
return sources;
|
||||||
|
|||||||
+9
-9
@@ -29,21 +29,21 @@ const Actions: FC<Props> = ({ torrent, title, provider, show }) => {
|
|||||||
|
|
||||||
const isStandalone = isStandaloneApp();
|
const isStandalone = isStandaloneApp();
|
||||||
|
|
||||||
const { isIOS, isSafari } = detectSafari();
|
const { isSafari } = detectSafari();
|
||||||
const isHEVC = torrent.codec?.includes('265') || torrent.codec?.toLowerCase().includes('hevc');
|
|
||||||
|
|
||||||
|
// The stream proxy remuxes any container to fragmented MP4 and converts audio
|
||||||
|
// to AAC, and copies HEVC tagged `hvc1` (which Safari/iOS decode in hardware).
|
||||||
|
// So we no longer require container === mp4 or block HEVC on Safari — the proxy
|
||||||
|
// makes the usual MKV/HEVC rips playable in the browser.
|
||||||
const supportedForStream = useMemo(() => {
|
const supportedForStream = useMemo(() => {
|
||||||
if (provider === providers.yts.key) {
|
if (provider === providers.yts.key) {
|
||||||
return (
|
return [Quality.The1080P, Quality.The720P].includes(torrent.quality as Quality);
|
||||||
[Quality.The1080P, Quality.The720P].includes(torrent.quality as Quality) &&
|
|
||||||
(isHEVC ? !isIOS && !isSafari : true)
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const isMP4 = torrent.container?.includes('mp4');
|
const container = torrent.container?.toLowerCase();
|
||||||
|
|
||||||
return isMP4 && (isHEVC ? !isIOS && !isSafari : true);
|
return !container || /mp4|mkv|webm|mov|m4v|avi|ts/.test(container);
|
||||||
}, [isIOS, isSafari, isHEVC, torrent, provider]);
|
}, [torrent, provider]);
|
||||||
|
|
||||||
const fetchMagnet = useMagnet(torrent);
|
const fetchMagnet = useMagnet(torrent);
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user