mirror of
https://github.com/movie-web/native-app.git
synced 2025-09-13 10:23:24 +00:00
feat: implement default quality setting
This commit is contained in:
@@ -91,7 +91,7 @@ export default function SettingsScreen() {
|
|||||||
};
|
};
|
||||||
|
|
||||||
const clearCacheDirectory = async () => {
|
const clearCacheDirectory = async () => {
|
||||||
const cacheDirectory = FileSystem.cacheDirectory + "movie-web";
|
const cacheDirectory = `${FileSystem.cacheDirectory}movie-web`;
|
||||||
if (!cacheDirectory) return;
|
if (!cacheDirectory) return;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
@@ -1,7 +1,7 @@
|
|||||||
import { MaterialCommunityIcons } from "@expo/vector-icons";
|
import { MaterialCommunityIcons } from "@expo/vector-icons";
|
||||||
import { useTheme } from "tamagui";
|
import { useTheme } from "tamagui";
|
||||||
|
|
||||||
import { findHighestQuality } from "@movie-web/provider-utils";
|
import { findQuality } from "@movie-web/provider-utils";
|
||||||
|
|
||||||
import { useDownloadManager } from "~/hooks/useDownloadManager";
|
import { useDownloadManager } from "~/hooks/useDownloadManager";
|
||||||
import { convertMetaToScrapeMedia } from "~/lib/meta";
|
import { convertMetaToScrapeMedia } from "~/lib/meta";
|
||||||
@@ -21,7 +21,7 @@ export const DownloadButton = () => {
|
|||||||
let url: string | undefined | null = null;
|
let url: string | undefined | null = null;
|
||||||
|
|
||||||
if (stream?.type === "file") {
|
if (stream?.type === "file") {
|
||||||
const highestQuality = findHighestQuality(stream);
|
const highestQuality = findQuality(stream);
|
||||||
url = highestQuality ? stream.qualities[highestQuality]?.url : null;
|
url = highestQuality ? stream.qualities[highestQuality]?.url : null;
|
||||||
} else if (stream?.type === "hls") {
|
} else if (stream?.type === "hls") {
|
||||||
url = stream.playlist;
|
url = stream.playlist;
|
||||||
|
@@ -4,19 +4,14 @@ import { ScrollView } from "react-native-gesture-handler";
|
|||||||
import { useRouter } from "expo-router";
|
import { useRouter } from "expo-router";
|
||||||
import { View } from "tamagui";
|
import { View } from "tamagui";
|
||||||
|
|
||||||
import type {
|
import type { RunOutput, ScrapeMedia } from "@movie-web/provider-utils";
|
||||||
HlsBasedStream,
|
|
||||||
RunOutput,
|
|
||||||
ScrapeMedia,
|
|
||||||
} from "@movie-web/provider-utils";
|
|
||||||
import {
|
import {
|
||||||
constructFullUrl,
|
|
||||||
extractTracksFromHLS,
|
extractTracksFromHLS,
|
||||||
findHighestQuality,
|
filterAudioTracks,
|
||||||
|
findQuality,
|
||||||
} from "@movie-web/provider-utils";
|
} from "@movie-web/provider-utils";
|
||||||
|
|
||||||
import type { ItemData } from "../item/item";
|
import type { ItemData } from "../item/item";
|
||||||
import type { AudioTrack } from "./AudioTrackSelector";
|
|
||||||
import type { PlayerMeta } from "~/stores/player/slices/video";
|
import type { PlayerMeta } from "~/stores/player/slices/video";
|
||||||
import { useMeta } from "~/hooks/player/useMeta";
|
import { useMeta } from "~/hooks/player/useMeta";
|
||||||
import { useScrape } from "~/hooks/player/useSourceScrape";
|
import { useScrape } from "~/hooks/player/useSourceScrape";
|
||||||
@@ -76,9 +71,9 @@ export const ScraperProcess = ({
|
|||||||
if (!streamResult) return router.back();
|
if (!streamResult) return router.back();
|
||||||
if (download) {
|
if (download) {
|
||||||
if (streamResult.stream.type === "file") {
|
if (streamResult.stream.type === "file") {
|
||||||
const highestQuality = findHighestQuality(streamResult.stream);
|
const quality = findQuality(streamResult.stream);
|
||||||
const url = highestQuality
|
const url = quality
|
||||||
? streamResult.stream.qualities[highestQuality]?.url
|
? streamResult.stream.qualities[quality]?.url
|
||||||
: null;
|
: null;
|
||||||
if (!url) return;
|
if (!url) return;
|
||||||
startDownload(url, "mp4", scrapeMedia).catch(console.error);
|
startDownload(url, "mp4", scrapeMedia).catch(console.error);
|
||||||
@@ -89,6 +84,7 @@ export const ScraperProcess = ({
|
|||||||
}
|
}
|
||||||
return router.back();
|
return router.back();
|
||||||
}
|
}
|
||||||
|
|
||||||
setStream(streamResult.stream);
|
setStream(streamResult.stream);
|
||||||
|
|
||||||
if (streamResult.stream.type === "hls") {
|
if (streamResult.stream.type === "hls") {
|
||||||
@@ -103,28 +99,9 @@ export const ScraperProcess = ({
|
|||||||
if (tracks) setHlsTracks(tracks);
|
if (tracks) setHlsTracks(tracks);
|
||||||
|
|
||||||
if (tracks?.audio.length) {
|
if (tracks?.audio.length) {
|
||||||
const audioTracks: AudioTrack[] = tracks.audio.map((track) => ({
|
setAudioTracks(
|
||||||
uri: constructFullUrl(
|
filterAudioTracks(tracks, streamResult.stream.playlist),
|
||||||
(streamResult?.stream as HlsBasedStream).playlist,
|
);
|
||||||
track.uri,
|
|
||||||
),
|
|
||||||
name: track.properties[0]?.attributes.name?.toString() ?? "Unknown",
|
|
||||||
language:
|
|
||||||
track.properties[0]?.attributes.language?.toString() ?? "Unknown",
|
|
||||||
active: Boolean(track.properties[0]?.attributes.default) ?? false,
|
|
||||||
}));
|
|
||||||
|
|
||||||
const uniqueTracks = new Set(audioTracks.map((t) => t.language));
|
|
||||||
|
|
||||||
const filteredAudioTracks = audioTracks.filter((track) => {
|
|
||||||
if (uniqueTracks.has(track.language)) {
|
|
||||||
uniqueTracks.delete(track.language);
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
});
|
|
||||||
|
|
||||||
setAudioTracks(filteredAudioTracks);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
setPlayerStatus(PlayerStatus.READY);
|
setPlayerStatus(PlayerStatus.READY);
|
||||||
|
@@ -12,12 +12,13 @@ import { useSafeAreaInsets } from "react-native-safe-area-context";
|
|||||||
import { ResizeMode, Video } from "expo-av";
|
import { ResizeMode, Video } from "expo-av";
|
||||||
import * as Haptics from "expo-haptics";
|
import * as Haptics from "expo-haptics";
|
||||||
import * as NavigationBar from "expo-navigation-bar";
|
import * as NavigationBar from "expo-navigation-bar";
|
||||||
|
import * as Network from "expo-network";
|
||||||
import { useRouter } from "expo-router";
|
import { useRouter } from "expo-router";
|
||||||
import * as StatusBar from "expo-status-bar";
|
import * as StatusBar from "expo-status-bar";
|
||||||
import { Feather } from "@expo/vector-icons";
|
import { Feather } from "@expo/vector-icons";
|
||||||
import { Spinner, useTheme, View } from "tamagui";
|
import { Spinner, useTheme, View } from "tamagui";
|
||||||
|
|
||||||
import { findHighestQuality } from "@movie-web/provider-utils";
|
import { findHLSQuality, findQuality } from "@movie-web/provider-utils";
|
||||||
|
|
||||||
import { useAudioTrack } from "~/hooks/player/useAudioTrack";
|
import { useAudioTrack } from "~/hooks/player/useAudioTrack";
|
||||||
import { useBrightness } from "~/hooks/player/useBrightness";
|
import { useBrightness } from "~/hooks/player/useBrightness";
|
||||||
@@ -32,6 +33,8 @@ import {
|
|||||||
import { useAudioTrackStore } from "~/stores/audio";
|
import { useAudioTrackStore } from "~/stores/audio";
|
||||||
import { usePlayerStore } from "~/stores/player/store";
|
import { usePlayerStore } from "~/stores/player/store";
|
||||||
import {
|
import {
|
||||||
|
DefaultQuality,
|
||||||
|
useNetworkSettingsStore,
|
||||||
usePlayerSettingsStore,
|
usePlayerSettingsStore,
|
||||||
useWatchHistoryStore,
|
useWatchHistoryStore,
|
||||||
} from "~/stores/settings";
|
} from "~/stores/settings";
|
||||||
@@ -76,6 +79,8 @@ export const VideoPlayer = () => {
|
|||||||
const { gestureControls, autoPlay } = usePlayerSettingsStore();
|
const { gestureControls, autoPlay } = usePlayerSettingsStore();
|
||||||
const { updateWatchHistory, removeFromWatchHistory, getWatchHistoryItem } =
|
const { updateWatchHistory, removeFromWatchHistory, getWatchHistoryItem } =
|
||||||
useWatchHistoryStore();
|
useWatchHistoryStore();
|
||||||
|
const { wifiDefaultQuality, mobileDataDefaultQuality } =
|
||||||
|
useNetworkSettingsStore();
|
||||||
|
|
||||||
const updateResizeMode = (newMode: ResizeMode) => {
|
const updateResizeMode = (newMode: ResizeMode) => {
|
||||||
setResizeMode(newMode);
|
setResizeMode(newMode);
|
||||||
@@ -158,15 +163,22 @@ export const VideoPlayer = () => {
|
|||||||
}
|
}
|
||||||
setIsLoading(true);
|
setIsLoading(true);
|
||||||
|
|
||||||
|
const { type: networkType } = await Network.getNetworkStateAsync();
|
||||||
|
const defaultQuality =
|
||||||
|
networkType === Network.NetworkStateType.WIFI
|
||||||
|
? wifiDefaultQuality
|
||||||
|
: mobileDataDefaultQuality;
|
||||||
|
const highest = defaultQuality === DefaultQuality.Highest;
|
||||||
|
|
||||||
let url = null;
|
let url = null;
|
||||||
|
|
||||||
if (stream.type === "hls") {
|
if (stream.type === "hls") {
|
||||||
url = stream.playlist;
|
url = await findHLSQuality(stream.playlist, stream.headers, highest);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (stream.type === "file") {
|
if (stream.type === "file") {
|
||||||
const highestQuality = findHighestQuality(stream);
|
const chosenQuality = findQuality(stream, highest);
|
||||||
url = highestQuality ? stream.qualities[highestQuality]?.url : null;
|
url = chosenQuality ? stream.qualities[chosenQuality]?.url : null;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!url) {
|
if (!url) {
|
||||||
@@ -219,6 +231,8 @@ export const VideoPlayer = () => {
|
|||||||
updateWatchHistory,
|
updateWatchHistory,
|
||||||
videoRef?.props.positionMillis,
|
videoRef?.props.positionMillis,
|
||||||
videoSrc?.uri,
|
videoSrc?.uri,
|
||||||
|
wifiDefaultQuality,
|
||||||
|
mobileDataDefaultQuality,
|
||||||
]);
|
]);
|
||||||
|
|
||||||
const onVideoLoadStart = () => {
|
const onVideoLoadStart = () => {
|
||||||
|
@@ -184,11 +184,11 @@ export const useDownloadManager = () => {
|
|||||||
lastTimestamp = currentTime;
|
lastTimestamp = currentTime;
|
||||||
};
|
};
|
||||||
|
|
||||||
const fileUri =
|
const fileUri = `${FileSystem.cacheDirectory}movie-web${url.split("/").pop()}`;
|
||||||
FileSystem.cacheDirectory + "movie-web"
|
if (
|
||||||
? FileSystem.cacheDirectory + "movie-web" + url.split("/").pop()
|
!(await FileSystem.getInfoAsync(`${FileSystem.cacheDirectory}movie-web`))
|
||||||
: null;
|
.exists
|
||||||
if (!fileUri) {
|
) {
|
||||||
console.error("Cache directory is unavailable");
|
console.error("Cache directory is unavailable");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -238,7 +238,7 @@ export const useDownloadManager = () => {
|
|||||||
const totalSegments = segments.length;
|
const totalSegments = segments.length;
|
||||||
let segmentsDownloaded = 0;
|
let segmentsDownloaded = 0;
|
||||||
|
|
||||||
const segmentDir = FileSystem.cacheDirectory + "movie-web/segments/";
|
const segmentDir = `${FileSystem.cacheDirectory}movie-web/segments/`;
|
||||||
await ensureDirExists(segmentDir);
|
await ensureDirExists(segmentDir);
|
||||||
|
|
||||||
const updateProgress = () => {
|
const updateProgress = () => {
|
||||||
|
@@ -223,13 +223,18 @@ export const useWatchHistoryStore = create<
|
|||||||
),
|
),
|
||||||
);
|
);
|
||||||
|
|
||||||
|
export enum DefaultQuality {
|
||||||
|
Lowest = "Lowest",
|
||||||
|
Highest = "Highest",
|
||||||
|
}
|
||||||
|
|
||||||
interface NetworkSettingsStoreState {
|
interface NetworkSettingsStoreState {
|
||||||
allowMobileData: boolean;
|
allowMobileData: boolean;
|
||||||
setAllowMobileData: (enabled: boolean) => void;
|
setAllowMobileData: (enabled: boolean) => void;
|
||||||
wifiDefaultQuality: string;
|
wifiDefaultQuality: DefaultQuality;
|
||||||
setWifiDefaultQuality: (quality: string) => void;
|
setWifiDefaultQuality: (quality: DefaultQuality) => void;
|
||||||
mobileDataDefaultQuality: string;
|
mobileDataDefaultQuality: DefaultQuality;
|
||||||
setMobileDataDefaultQuality: (quality: string) => void;
|
setMobileDataDefaultQuality: (quality: DefaultQuality) => void;
|
||||||
}
|
}
|
||||||
|
|
||||||
export const useNetworkSettingsStore = create<
|
export const useNetworkSettingsStore = create<
|
||||||
@@ -240,9 +245,9 @@ export const useNetworkSettingsStore = create<
|
|||||||
(set) => ({
|
(set) => ({
|
||||||
allowMobileData: false,
|
allowMobileData: false,
|
||||||
setAllowMobileData: (enabled) => set({ allowMobileData: enabled }),
|
setAllowMobileData: (enabled) => set({ allowMobileData: enabled }),
|
||||||
wifiDefaultQuality: "Highest",
|
wifiDefaultQuality: DefaultQuality.Highest,
|
||||||
setWifiDefaultQuality: (quality) => set({ wifiDefaultQuality: quality }),
|
setWifiDefaultQuality: (quality) => set({ wifiDefaultQuality: quality }),
|
||||||
mobileDataDefaultQuality: "Lowest",
|
mobileDataDefaultQuality: DefaultQuality.Lowest,
|
||||||
setMobileDataDefaultQuality: (quality) =>
|
setMobileDataDefaultQuality: (quality) =>
|
||||||
set({ mobileDataDefaultQuality: quality }),
|
set({ mobileDataDefaultQuality: quality }),
|
||||||
}),
|
}),
|
||||||
|
@@ -2,7 +2,8 @@ import type { AppendToResponse, MovieDetails, TvShowDetails } from "tmdb-ts";
|
|||||||
|
|
||||||
import type { ScrapeMedia } from "@movie-web/providers";
|
import type { ScrapeMedia } from "@movie-web/providers";
|
||||||
|
|
||||||
import { providers } from "./video";
|
import type { HLSTracks } from "./video";
|
||||||
|
import { constructFullUrl, providers } from "./video";
|
||||||
|
|
||||||
export function getMetaData() {
|
export function getMetaData() {
|
||||||
return [...providers.listSources(), ...providers.listEmbeds()];
|
return [...providers.listSources(), ...providers.listEmbeds()];
|
||||||
@@ -59,3 +60,31 @@ export function transformSearchResultToScrapeMedia<T extends "tv" | "movie">(
|
|||||||
|
|
||||||
throw new Error("Invalid type parameter");
|
throw new Error("Invalid type parameter");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
interface AudioTrack {
|
||||||
|
uri: string;
|
||||||
|
name: string;
|
||||||
|
language: string;
|
||||||
|
active?: boolean;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function filterAudioTracks(tracks: HLSTracks, playlist: string) {
|
||||||
|
const audioTracks: AudioTrack[] = tracks.audio.map((track) => ({
|
||||||
|
uri: constructFullUrl(playlist, track.uri),
|
||||||
|
name: track.properties[0]?.attributes.name?.toString() ?? "Unknown",
|
||||||
|
language: track.properties[0]?.attributes.language?.toString() ?? "Unknown",
|
||||||
|
active: Boolean(track.properties[0]?.attributes.default) ?? false,
|
||||||
|
}));
|
||||||
|
|
||||||
|
const uniqueTracks = new Set(audioTracks.map((t) => t.language));
|
||||||
|
|
||||||
|
const filteredAudioTracks = audioTracks.filter((track) => {
|
||||||
|
if (uniqueTracks.has(track.language)) {
|
||||||
|
uniqueTracks.delete(track.language);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
});
|
||||||
|
|
||||||
|
return filteredAudioTracks;
|
||||||
|
}
|
||||||
|
@@ -149,8 +149,9 @@ export async function getVideoStreamFromEmbed({
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export function findHighestQuality(
|
export function findQuality(
|
||||||
stream: FileBasedStream,
|
stream: FileBasedStream,
|
||||||
|
highest = true,
|
||||||
): Qualities | undefined {
|
): Qualities | undefined {
|
||||||
const qualityOrder: Qualities[] = [
|
const qualityOrder: Qualities[] = [
|
||||||
"4k",
|
"4k",
|
||||||
@@ -160,6 +161,9 @@ export function findHighestQuality(
|
|||||||
"360",
|
"360",
|
||||||
"unknown",
|
"unknown",
|
||||||
];
|
];
|
||||||
|
if (!highest) {
|
||||||
|
qualityOrder.reverse();
|
||||||
|
}
|
||||||
for (const quality of qualityOrder) {
|
for (const quality of qualityOrder) {
|
||||||
if (stream.qualities[quality]) {
|
if (stream.qualities[quality]) {
|
||||||
return quality;
|
return quality;
|
||||||
@@ -193,9 +197,10 @@ export async function extractTracksFromHLS(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function extractSegmentsFromHLS(
|
export async function findHLSQuality(
|
||||||
playlistUrl: string,
|
playlistUrl: string,
|
||||||
headers: Record<string, string>,
|
headers?: Record<string, string>,
|
||||||
|
highest = true,
|
||||||
) {
|
) {
|
||||||
try {
|
try {
|
||||||
const response = await fetch(playlistUrl, { headers }).then((res) =>
|
const response = await fetch(playlistUrl, { headers }).then((res) =>
|
||||||
@@ -220,17 +225,29 @@ export async function extractSegmentsFromHLS(
|
|||||||
return widthB * heightB - widthA * heightA;
|
return widthB * heightB - widthA * heightA;
|
||||||
});
|
});
|
||||||
|
|
||||||
const highestQuality = sortedStreams[0];
|
const chosenQuality = sortedStreams[highest ? 0 : sortedStreams.length - 1];
|
||||||
if (!highestQuality) return null;
|
if (!chosenQuality) return null;
|
||||||
|
|
||||||
const highestQualityUri = constructFullUrl(playlistUrl, highestQuality.uri);
|
return chosenQuality.uri;
|
||||||
const highestQualityResponse = await fetch(highestQualityUri, {
|
} catch (e) {
|
||||||
headers,
|
return null;
|
||||||
}).then((res) => res.text());
|
}
|
||||||
const highestQualityPlaylist = hls.parse(highestQualityResponse);
|
}
|
||||||
|
|
||||||
return highestQualityPlaylist.segments.map((segment) =>
|
export async function extractSegmentsFromHLS(
|
||||||
constructFullUrl(highestQualityUri, segment.uri),
|
playlistUrl: string,
|
||||||
|
headers: Record<string, string>,
|
||||||
|
) {
|
||||||
|
try {
|
||||||
|
const highestQualityUrl = await findHLSQuality(playlistUrl, headers);
|
||||||
|
if (!highestQualityUrl) return null;
|
||||||
|
const response = await fetch(highestQualityUrl, { headers }).then((res) =>
|
||||||
|
res.text(),
|
||||||
|
);
|
||||||
|
const playlist = hls.parse(response);
|
||||||
|
|
||||||
|
return playlist.segments.map((segment) =>
|
||||||
|
constructFullUrl(highestQualityUrl, segment.uri),
|
||||||
);
|
);
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
return null;
|
return null;
|
||||||
|
589
pnpm-lock.yaml
generated
589
pnpm-lock.yaml
generated
File diff suppressed because it is too large
Load Diff
@@ -32,6 +32,8 @@ const config = {
|
|||||||
"@typescript-eslint/no-explicit-any": "off",
|
"@typescript-eslint/no-explicit-any": "off",
|
||||||
"@typescript-eslint/no-unsafe-argument": "off",
|
"@typescript-eslint/no-unsafe-argument": "off",
|
||||||
"@typescript-eslint/no-unsafe-enum-comparison": "off",
|
"@typescript-eslint/no-unsafe-enum-comparison": "off",
|
||||||
|
"@typescript-eslint/no-useless-template-literals": "error",
|
||||||
|
"prefer-template": "error",
|
||||||
},
|
},
|
||||||
ignorePatterns: [
|
ignorePatterns: [
|
||||||
"**/*.config.js",
|
"**/*.config.js",
|
||||||
|
Reference in New Issue
Block a user