mirror of
https://github.com/streamyfin/streamyfin.git
synced 2025-08-20 18:37:18 +02:00
refactor: Feature/offline mode rework (#859)
Co-authored-by: lostb1t <coding-mosses0z@icloud.com> Co-authored-by: Fredrik Burmester <fredrik.burmester@gmail.com> Co-authored-by: Gauvain <68083474+Gauvino@users.noreply.github.com> Co-authored-by: Gauvino <uruknarb20@gmail.com> Co-authored-by: storm1er <le.storm1er@gmail.com> Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com> Co-authored-by: Chris <182387676+whoopsi-daisy@users.noreply.github.com> Co-authored-by: arch-fan <55891793+arch-fan@users.noreply.github.com> Co-authored-by: Alex Kim <alexkim@Alexs-MacBook-Pro.local>
This commit is contained in:
@@ -2,8 +2,8 @@ import type { BaseItemDto } from "@jellyfin/sdk/lib/generated-client/models";
|
||||
import { atom, useAtom } from "jotai";
|
||||
import { useEffect } from "react";
|
||||
import { processesAtom } from "@/providers/DownloadProvider";
|
||||
import { JobStatus } from "@/providers/Downloads/types";
|
||||
import { useSettings } from "@/utils/atoms/settings";
|
||||
import type { JobStatus } from "@/utils/optimize-server";
|
||||
|
||||
export interface Job {
|
||||
id: string;
|
||||
@@ -68,5 +68,5 @@ export const useJobProcessor = () => {
|
||||
console.info("Processing queue", queue);
|
||||
queueActions.processJob(queue, setQueue, setRunning);
|
||||
}
|
||||
}, [processes, queue, running, setQueue, setRunning]);
|
||||
}, [processes, queue, running, setQueue, setRunning, settings]);
|
||||
};
|
||||
|
||||
@@ -81,7 +81,6 @@ export type DefaultLanguageOption = {
|
||||
|
||||
export enum DownloadMethod {
|
||||
Remux = "remux",
|
||||
Optimized = "optimized",
|
||||
}
|
||||
|
||||
export type Home = {
|
||||
@@ -155,7 +154,6 @@ export type Settings = {
|
||||
defaultVideoOrientation: ScreenOrientation.OrientationLock;
|
||||
forwardSkipTime: number;
|
||||
rewindSkipTime: number;
|
||||
optimizedVersionsServerUrl?: string | null;
|
||||
downloadMethod: DownloadMethod;
|
||||
autoDownload: boolean;
|
||||
showCustomMenuLinks: boolean;
|
||||
@@ -212,7 +210,6 @@ const defaultValues: Settings = {
|
||||
defaultVideoOrientation: ScreenOrientation.OrientationLock.DEFAULT,
|
||||
forwardSkipTime: 30,
|
||||
rewindSkipTime: 10,
|
||||
optimizedVersionsServerUrl: null,
|
||||
downloadMethod: DownloadMethod.Remux,
|
||||
autoDownload: false,
|
||||
showCustomMenuLinks: false,
|
||||
|
||||
@@ -19,7 +19,9 @@ class EventBus {
|
||||
}
|
||||
|
||||
emit<T = void>(event: string, data?: T): void {
|
||||
this.listeners[event]?.forEach((callback) => callback(data));
|
||||
this.listeners[event]?.forEach((callback) => {
|
||||
callback(data);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -51,18 +51,9 @@ export function getDefaultPlaySettings(
|
||||
|
||||
const mediaSource = item.MediaSources?.[0];
|
||||
|
||||
// 2. Get default or preferred audio
|
||||
const defaultAudioIndex = mediaSource?.DefaultAudioStreamIndex;
|
||||
const _preferedAudioIndex = mediaSource?.MediaStreams?.find(
|
||||
(x) => x.Type === "Audio" && x.Language === settings?.defaultAudioLanguage,
|
||||
)?.Index;
|
||||
const _firstAudioIndex = mediaSource?.MediaStreams?.find(
|
||||
(x) => x.Type === "Audio",
|
||||
)?.Index;
|
||||
|
||||
// We prefer the previous track over the default track.
|
||||
const trackOptions: TrackOptions = {
|
||||
DefaultAudioStreamIndex: defaultAudioIndex ?? -1,
|
||||
DefaultAudioStreamIndex: mediaSource?.DefaultAudioStreamIndex ?? -1,
|
||||
DefaultSubtitleStreamIndex: mediaSource?.DefaultSubtitleStreamIndex ?? -1,
|
||||
};
|
||||
|
||||
|
||||
68
utils/jellyfin/media/getDownloadUrl.ts
Normal file
68
utils/jellyfin/media/getDownloadUrl.ts
Normal file
@@ -0,0 +1,68 @@
|
||||
import type { Api } from "@jellyfin/sdk";
|
||||
import type {
|
||||
BaseItemDto,
|
||||
MediaSourceInfo,
|
||||
} from "@jellyfin/sdk/lib/generated-client/models";
|
||||
import { Bitrate } from "@/components/BitrateSelector";
|
||||
import { generateDeviceProfile } from "@/utils/profiles/native";
|
||||
import { getDownloadStreamUrl, getStreamUrl } from "./getStreamUrl";
|
||||
|
||||
export const getDownloadUrl = async ({
|
||||
api,
|
||||
item,
|
||||
userId,
|
||||
mediaSource,
|
||||
maxBitrate,
|
||||
audioStreamIndex,
|
||||
subtitleStreamIndex,
|
||||
deviceId,
|
||||
}: {
|
||||
api: Api;
|
||||
item: BaseItemDto;
|
||||
userId: string;
|
||||
mediaSource: MediaSourceInfo;
|
||||
maxBitrate: Bitrate;
|
||||
audioStreamIndex: number;
|
||||
subtitleStreamIndex: number;
|
||||
deviceId: string;
|
||||
}): Promise<{
|
||||
url: string | null;
|
||||
mediaSource: MediaSourceInfo | null;
|
||||
} | null> => {
|
||||
const streamDetails = await getStreamUrl({
|
||||
api,
|
||||
item,
|
||||
userId,
|
||||
startTimeTicks: 0,
|
||||
mediaSourceId: mediaSource.Id,
|
||||
maxStreamingBitrate: maxBitrate.value,
|
||||
audioStreamIndex,
|
||||
subtitleStreamIndex,
|
||||
deviceId,
|
||||
deviceProfile: generateDeviceProfile(),
|
||||
});
|
||||
|
||||
if (maxBitrate.key === "Max" && !streamDetails?.mediaSource?.TranscodingUrl) {
|
||||
console.log("Downloading item directly");
|
||||
return {
|
||||
url: `${api.basePath}/Items/${item.Id}/Download?api_key=${api.accessToken}`,
|
||||
mediaSource: streamDetails?.mediaSource ?? null,
|
||||
};
|
||||
}
|
||||
|
||||
const downloadStreamDetails = await getDownloadStreamUrl({
|
||||
api,
|
||||
item,
|
||||
userId,
|
||||
mediaSourceId: mediaSource.Id,
|
||||
deviceId,
|
||||
maxStreamingBitrate: maxBitrate.value,
|
||||
audioStreamIndex,
|
||||
subtitleStreamIndex,
|
||||
});
|
||||
|
||||
return {
|
||||
url: downloadStreamDetails?.url ?? null,
|
||||
mediaSource: downloadStreamDetails?.mediaSource ?? null,
|
||||
};
|
||||
};
|
||||
@@ -4,7 +4,7 @@ import type {
|
||||
MediaSourceInfo,
|
||||
} from "@jellyfin/sdk/lib/generated-client/models";
|
||||
import { getMediaInfoApi } from "@jellyfin/sdk/lib/utils/api";
|
||||
import generateDeviceProfile from "@/utils/profiles/native";
|
||||
import download from "@/utils/profiles/download";
|
||||
|
||||
export const getStreamUrl = async ({
|
||||
api,
|
||||
@@ -13,11 +13,10 @@ export const getStreamUrl = async ({
|
||||
startTimeTicks = 0,
|
||||
maxStreamingBitrate,
|
||||
playSessionId,
|
||||
deviceProfile = generateDeviceProfile(),
|
||||
deviceProfile,
|
||||
audioStreamIndex = 0,
|
||||
subtitleStreamIndex = undefined,
|
||||
mediaSourceId,
|
||||
download = false,
|
||||
deviceId,
|
||||
}: {
|
||||
api: Api | null | undefined;
|
||||
@@ -26,12 +25,11 @@ export const getStreamUrl = async ({
|
||||
startTimeTicks: number;
|
||||
maxStreamingBitrate?: number;
|
||||
playSessionId?: string | null;
|
||||
deviceProfile?: any;
|
||||
deviceProfile: any;
|
||||
audioStreamIndex?: number;
|
||||
subtitleStreamIndex?: number;
|
||||
height?: number;
|
||||
mediaSourceId?: string | null;
|
||||
download?: bool;
|
||||
deviceId?: string | null;
|
||||
}): Promise<{
|
||||
url: string | null;
|
||||
@@ -71,12 +69,16 @@ export const getStreamUrl = async ({
|
||||
}
|
||||
|
||||
sessionId = res.data.PlaySessionId || null;
|
||||
mediaSource = res.data.MediaSources[0];
|
||||
let transcodeUrl = mediaSource.TranscodingUrl;
|
||||
mediaSource = res.data.MediaSources?.[0];
|
||||
let transcodeUrl = mediaSource?.TranscodingUrl;
|
||||
|
||||
if (transcodeUrl) {
|
||||
if (download) {
|
||||
transcodeUrl = transcodeUrl.replace("master.m3u8", "stream");
|
||||
// We need to change the subtitle method to hls for the transcoded url.
|
||||
if (subtitleStreamIndex === -1) {
|
||||
transcodeUrl = transcodeUrl.replace(
|
||||
"SubtitleMethod=Encode",
|
||||
"SubtitleMethod=Hls",
|
||||
);
|
||||
}
|
||||
console.log("Video is being transcoded:", transcodeUrl);
|
||||
return {
|
||||
@@ -86,21 +88,6 @@ export const getStreamUrl = async ({
|
||||
};
|
||||
}
|
||||
|
||||
let downloadParams = {};
|
||||
|
||||
if (download) {
|
||||
// We need to disable static so we can have a remux with subtitle.
|
||||
downloadParams = {
|
||||
subtitleMethod: "Embed",
|
||||
enableSubtitlesInManifest: true,
|
||||
static: "false",
|
||||
allowVideoStreamCopy: true,
|
||||
allowAudioStreamCopy: true,
|
||||
playSessionId: sessionId || "",
|
||||
container: "ts",
|
||||
};
|
||||
}
|
||||
|
||||
const streamParams = new URLSearchParams({
|
||||
static: "true",
|
||||
container: "mp4",
|
||||
@@ -111,8 +98,7 @@ export const getStreamUrl = async ({
|
||||
api_key: api.accessToken,
|
||||
startTimeTicks: startTimeTicks.toString(),
|
||||
maxStreamingBitrate: maxStreamingBitrate?.toString() || "",
|
||||
userId: userId || "",
|
||||
...downloadParams,
|
||||
userId: userId,
|
||||
});
|
||||
|
||||
const directPlayUrl = `${
|
||||
@@ -123,7 +109,113 @@ export const getStreamUrl = async ({
|
||||
|
||||
return {
|
||||
url: directPlayUrl,
|
||||
sessionId: sessionId || playSessionId,
|
||||
sessionId: sessionId || playSessionId || null,
|
||||
mediaSource,
|
||||
};
|
||||
};
|
||||
|
||||
export const getDownloadStreamUrl = async ({
|
||||
api,
|
||||
item,
|
||||
userId,
|
||||
maxStreamingBitrate,
|
||||
audioStreamIndex = 0,
|
||||
subtitleStreamIndex = undefined,
|
||||
mediaSourceId,
|
||||
deviceId,
|
||||
}: {
|
||||
api: Api | null | undefined;
|
||||
item: BaseItemDto | null | undefined;
|
||||
userId: string | null | undefined;
|
||||
maxStreamingBitrate?: number;
|
||||
audioStreamIndex?: number;
|
||||
subtitleStreamIndex?: number;
|
||||
mediaSourceId?: string | null;
|
||||
deviceId?: string | null;
|
||||
}): Promise<{
|
||||
url: string | null;
|
||||
sessionId: string | null;
|
||||
mediaSource: MediaSourceInfo | undefined;
|
||||
} | null> => {
|
||||
if (!api || !userId || !item?.Id) {
|
||||
console.warn("Missing required parameters for getStreamUrl");
|
||||
return null;
|
||||
}
|
||||
|
||||
let mediaSource: MediaSourceInfo | undefined;
|
||||
let sessionId: string | null | undefined;
|
||||
|
||||
const res = await getMediaInfoApi(api).getPlaybackInfo(
|
||||
{
|
||||
itemId: item.Id!,
|
||||
},
|
||||
{
|
||||
method: "POST",
|
||||
data: {
|
||||
userId,
|
||||
deviceProfile: download,
|
||||
subtitleStreamIndex,
|
||||
startTimeTicks: 0,
|
||||
isPlayback: true,
|
||||
autoOpenLiveStream: true,
|
||||
maxStreamingBitrate,
|
||||
audioStreamIndex,
|
||||
mediaSourceId,
|
||||
},
|
||||
},
|
||||
);
|
||||
|
||||
if (res.status !== 200) {
|
||||
console.error("Error getting playback info:", res.status, res.statusText);
|
||||
}
|
||||
|
||||
sessionId = res.data.PlaySessionId || null;
|
||||
mediaSource = res.data.MediaSources?.[0];
|
||||
let transcodeUrl = mediaSource?.TranscodingUrl;
|
||||
|
||||
if (transcodeUrl) {
|
||||
transcodeUrl = transcodeUrl.replace("master.m3u8", "stream");
|
||||
console.log("Video is being transcoded:", transcodeUrl);
|
||||
return {
|
||||
url: `${api.basePath}${transcodeUrl}`,
|
||||
sessionId,
|
||||
mediaSource,
|
||||
};
|
||||
}
|
||||
|
||||
const downloadParams = {
|
||||
// We need to disable static so we can have a remux with subtitle.
|
||||
subtitleMethod: "Embed",
|
||||
enableSubtitlesInManifest: true,
|
||||
allowVideoStreamCopy: true,
|
||||
allowAudioStreamCopy: true,
|
||||
playSessionId: sessionId || "",
|
||||
};
|
||||
|
||||
const streamParams = new URLSearchParams({
|
||||
static: "false",
|
||||
container: "ts",
|
||||
mediaSourceId: mediaSource?.Id || "",
|
||||
subtitleStreamIndex: subtitleStreamIndex?.toString() || "",
|
||||
audioStreamIndex: audioStreamIndex?.toString() || "",
|
||||
deviceId: deviceId || api.deviceInfo.id,
|
||||
api_key: api.accessToken,
|
||||
startTimeTicks: "0",
|
||||
maxStreamingBitrate: maxStreamingBitrate?.toString() || "",
|
||||
userId: userId,
|
||||
});
|
||||
|
||||
Object.entries(downloadParams).forEach(([key, value]) => {
|
||||
streamParams.append(key, value.toString());
|
||||
});
|
||||
|
||||
const directPlayUrl = `${
|
||||
api.basePath
|
||||
}/Videos/${item.Id}/stream?${streamParams.toString()}`;
|
||||
|
||||
return {
|
||||
url: directPlayUrl,
|
||||
sessionId: sessionId || null,
|
||||
mediaSource,
|
||||
};
|
||||
};
|
||||
|
||||
@@ -1,45 +0,0 @@
|
||||
import type { Api } from "@jellyfin/sdk";
|
||||
import type { AxiosError } from "axios";
|
||||
|
||||
interface MarkAsNotPlayedParams {
|
||||
api: Api | null | undefined;
|
||||
itemId: string | null | undefined;
|
||||
userId: string | null | undefined;
|
||||
}
|
||||
|
||||
/**
|
||||
* Marks a media item as not played for a specific user.
|
||||
*
|
||||
* @param params - The parameters for marking an item as not played
|
||||
* @returns A promise that resolves to true if the operation was successful, false otherwise
|
||||
*/
|
||||
export const markAsNotPlayed = async ({
|
||||
api,
|
||||
itemId,
|
||||
userId,
|
||||
}: MarkAsNotPlayedParams): Promise<void> => {
|
||||
if (!api || !itemId || !userId) {
|
||||
console.error("Invalid parameters for markAsNotPlayed");
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
await api.axiosInstance.delete(
|
||||
`${api.basePath}/UserPlayedItems/${itemId}`,
|
||||
{
|
||||
params: { userId },
|
||||
headers: {
|
||||
Authorization: `MediaBrowser DeviceId="${api.deviceInfo.id}", Token="${api.accessToken}"`,
|
||||
},
|
||||
},
|
||||
);
|
||||
} catch (error) {
|
||||
const axiosError = error as AxiosError;
|
||||
console.error(
|
||||
"Failed to mark item as not played:",
|
||||
axiosError.message,
|
||||
axiosError.response?.status,
|
||||
);
|
||||
return;
|
||||
}
|
||||
};
|
||||
@@ -1,37 +0,0 @@
|
||||
import type { Api } from "@jellyfin/sdk";
|
||||
import type { BaseItemDto } from "@jellyfin/sdk/lib/generated-client/models";
|
||||
import { getPlaystateApi } from "@jellyfin/sdk/lib/utils/api";
|
||||
|
||||
interface MarkAsPlayedParams {
|
||||
api: Api | null | undefined;
|
||||
item: BaseItemDto | null | undefined;
|
||||
userId: string | null | undefined;
|
||||
}
|
||||
|
||||
/**
|
||||
* Marks a media item as played and updates its progress to completion.
|
||||
*
|
||||
* @param params - The parameters for marking an item as played∏
|
||||
* @returns A promise that resolves to true if the operation was successful, false otherwise
|
||||
*/
|
||||
export const markAsPlayed = async ({
|
||||
api,
|
||||
item,
|
||||
userId,
|
||||
}: MarkAsPlayedParams): Promise<boolean> => {
|
||||
if (!api || !item?.Id || !userId || !item.RunTimeTicks) {
|
||||
console.error("Invalid parameters for markAsPlayed");
|
||||
return false;
|
||||
}
|
||||
|
||||
try {
|
||||
const response = await getPlaystateApi(api).markPlayedItem({
|
||||
itemId: item.Id,
|
||||
datePlayed: new Date().toISOString(),
|
||||
});
|
||||
|
||||
return response.status === 200;
|
||||
} catch (_error) {
|
||||
return false;
|
||||
}
|
||||
};
|
||||
@@ -1,60 +0,0 @@
|
||||
import type { Api } from "@jellyfin/sdk";
|
||||
import { getPlaystateApi } from "@jellyfin/sdk/lib/utils/api";
|
||||
import type { Settings } from "@/utils/atoms/settings";
|
||||
|
||||
interface ReportPlaybackProgressParams {
|
||||
api?: Api | null;
|
||||
sessionId?: string | null;
|
||||
itemId?: string | null;
|
||||
positionTicks?: number | null;
|
||||
IsPaused?: boolean;
|
||||
deviceProfile?: Settings["deviceProfile"];
|
||||
}
|
||||
|
||||
/**
|
||||
* Reports playback progress to the Jellyfin server.
|
||||
*
|
||||
* @param params - The parameters for reporting playback progress
|
||||
* @throws {Error} If any required parameter is missing
|
||||
*/
|
||||
export const reportPlaybackProgress = async ({
|
||||
api,
|
||||
sessionId,
|
||||
itemId,
|
||||
positionTicks,
|
||||
IsPaused = false,
|
||||
}: ReportPlaybackProgressParams): Promise<void> => {
|
||||
if (!api || !sessionId || !itemId || !positionTicks) {
|
||||
return;
|
||||
}
|
||||
|
||||
console.info("reportPlaybackProgress ~ IsPaused", IsPaused);
|
||||
|
||||
try {
|
||||
await getPlaystateApi(api).onPlaybackProgress({
|
||||
itemId,
|
||||
audioStreamIndex: 0,
|
||||
subtitleStreamIndex: 0,
|
||||
mediaSourceId: itemId,
|
||||
positionTicks: Math.round(positionTicks),
|
||||
isPaused: IsPaused,
|
||||
isMuted: false,
|
||||
playMethod: "Transcode",
|
||||
});
|
||||
// await api.axiosInstance.post(
|
||||
// `${api.basePath}/Sessions/Playing/Progress`,
|
||||
// {
|
||||
// ItemId: itemId,
|
||||
// PlaySessionId: sessionId,
|
||||
// IsPaused,
|
||||
// PositionTicks: Math.round(positionTicks),
|
||||
// CanSeek: true,
|
||||
// MediaSourceId: itemId,
|
||||
// EventName: "timeupdate",
|
||||
// },
|
||||
// { headers: getAuthHeaders(api) }
|
||||
// );
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
}
|
||||
};
|
||||
@@ -1,7 +1,7 @@
|
||||
import type { Api } from "@jellyfin/sdk";
|
||||
import type { AxiosResponse } from "axios";
|
||||
import type { Settings } from "@/utils/atoms/settings";
|
||||
import generateDeviceProfile from "@/utils/profiles/native";
|
||||
import { generateDeviceProfile } from "@/utils/profiles/native";
|
||||
import { getAuthHeaders } from "../jellyfin";
|
||||
|
||||
interface PostCapabilitiesParams {
|
||||
|
||||
@@ -1,233 +0,0 @@
|
||||
import type {
|
||||
BaseItemDto,
|
||||
MediaSourceInfo,
|
||||
} from "@jellyfin/sdk/lib/generated-client";
|
||||
import axios from "axios";
|
||||
import { storage } from "@/utils/mmkv";
|
||||
import { writeToLog } from "./log";
|
||||
|
||||
interface IJobInput {
|
||||
deviceId?: string | null;
|
||||
authHeader?: string | null;
|
||||
url?: string | null;
|
||||
}
|
||||
|
||||
export interface JobStatus {
|
||||
id: string;
|
||||
status:
|
||||
| "queued"
|
||||
| "optimizing"
|
||||
| "completed"
|
||||
| "failed"
|
||||
| "cancelled"
|
||||
| "downloading";
|
||||
progress: number;
|
||||
outputPath: string;
|
||||
inputUrl: string;
|
||||
deviceId: string;
|
||||
itemId: string;
|
||||
item: BaseItemDto;
|
||||
speed?: number;
|
||||
timestamp: Date;
|
||||
base64Image?: string;
|
||||
}
|
||||
|
||||
/**
|
||||
* Fetches all jobs for a specific device.
|
||||
*
|
||||
* @param {IGetAllDeviceJobs} params - The parameters for the API request.
|
||||
* @param {string} params.deviceId - The ID of the device to fetch jobs for.
|
||||
* @param {string} params.authHeader - The authorization header for the API request.
|
||||
* @param {string} params.url - The base URL for the API endpoint.
|
||||
*
|
||||
* @returns {Promise<JobStatus[]>} A promise that resolves to an array of job statuses.
|
||||
*
|
||||
* @throws {Error} Throws an error if the API request fails or returns a non-200 status code.
|
||||
*/
|
||||
export async function getAllJobsByDeviceId({
|
||||
deviceId,
|
||||
authHeader,
|
||||
url,
|
||||
}: IJobInput): Promise<JobStatus[]> {
|
||||
const statusResponse = await axios.get(`${url}all-jobs`, {
|
||||
headers: {
|
||||
Authorization: authHeader,
|
||||
},
|
||||
params: {
|
||||
deviceId,
|
||||
},
|
||||
});
|
||||
if (statusResponse.status !== 200) {
|
||||
console.error(
|
||||
statusResponse.status,
|
||||
statusResponse.data,
|
||||
statusResponse.statusText,
|
||||
);
|
||||
throw new Error("Failed to fetch job status");
|
||||
}
|
||||
|
||||
return statusResponse.data;
|
||||
}
|
||||
|
||||
interface ICancelJob {
|
||||
authHeader: string;
|
||||
url: string;
|
||||
id: string;
|
||||
}
|
||||
|
||||
export async function cancelJobById({
|
||||
authHeader,
|
||||
url,
|
||||
id,
|
||||
}: ICancelJob): Promise<boolean> {
|
||||
const statusResponse = await axios.delete(`${url}cancel-job/${id}`, {
|
||||
headers: {
|
||||
Authorization: authHeader,
|
||||
},
|
||||
});
|
||||
if (statusResponse.status !== 200) {
|
||||
throw new Error("Failed to cancel process");
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
export async function cancelAllJobs({ authHeader, url, deviceId }: IJobInput) {
|
||||
if (!deviceId) return false;
|
||||
if (!authHeader) return false;
|
||||
if (!url) return false;
|
||||
|
||||
try {
|
||||
await getAllJobsByDeviceId({
|
||||
deviceId,
|
||||
authHeader,
|
||||
url,
|
||||
}).then((jobs) => {
|
||||
for (const job of jobs) {
|
||||
cancelJobById({
|
||||
authHeader,
|
||||
url,
|
||||
id: job.id,
|
||||
});
|
||||
}
|
||||
});
|
||||
} catch (error) {
|
||||
writeToLog("ERROR", "Failed to cancel all jobs", error);
|
||||
console.error(error);
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Fetches statistics for a specific device.
|
||||
*
|
||||
* @param {IJobInput} params - The parameters for the API request.
|
||||
* @param {string} params.deviceId - The ID of the device to fetch statistics for.
|
||||
* @param {string} params.authHeader - The authorization header for the API request.
|
||||
* @param {string} params.url - The base URL for the API endpoint.
|
||||
*
|
||||
* @returns {Promise<any | null>} A promise that resolves to the statistics data or null if the request fails.
|
||||
*
|
||||
* @throws {Error} Throws an error if any required parameter is missing.
|
||||
*/
|
||||
export async function getStatistics({
|
||||
authHeader,
|
||||
url,
|
||||
deviceId,
|
||||
}: IJobInput): Promise<any | null> {
|
||||
if (!deviceId || !authHeader || !url) {
|
||||
return null;
|
||||
}
|
||||
|
||||
try {
|
||||
const statusResponse = await axios.get(`${url}statistics`, {
|
||||
headers: {
|
||||
Authorization: authHeader,
|
||||
},
|
||||
params: {
|
||||
deviceId,
|
||||
},
|
||||
});
|
||||
|
||||
return statusResponse.data;
|
||||
} catch (error) {
|
||||
console.error("Failed to fetch statistics:", error);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Saves the download item info to disk - this data is used temporarily to fetch additional download information
|
||||
* in combination with the optimize server. This is used to not have to send all item info to the optimize server.
|
||||
*
|
||||
* @param {BaseItemDto} item - The item to save.
|
||||
* @param {MediaSourceInfo} mediaSource - The media source of the item.
|
||||
* @param {string} url - The URL of the item.
|
||||
* @return {boolean} A promise that resolves when the item info is saved.
|
||||
*/
|
||||
export function saveDownloadItemInfoToDiskTmp(
|
||||
item: BaseItemDto,
|
||||
mediaSource: MediaSourceInfo,
|
||||
url: string,
|
||||
): boolean {
|
||||
try {
|
||||
const downloadInfo = JSON.stringify({
|
||||
item,
|
||||
mediaSource,
|
||||
url,
|
||||
});
|
||||
|
||||
storage.set(`tmp_download_info_${item.Id}`, downloadInfo);
|
||||
|
||||
return true;
|
||||
} catch (error) {
|
||||
console.error("Failed to save download item info to disk:", error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieves the download item info from disk.
|
||||
*
|
||||
* @param {string} itemId - The ID of the item to retrieve.
|
||||
* @return {{
|
||||
* item: BaseItemDto;
|
||||
* mediaSource: MediaSourceInfo;
|
||||
* url: string;
|
||||
* } | null} The retrieved download item info or null if not found.
|
||||
*/
|
||||
export function getDownloadItemInfoFromDiskTmp(itemId: string): {
|
||||
item: BaseItemDto;
|
||||
mediaSource: MediaSourceInfo;
|
||||
url: string;
|
||||
} | null {
|
||||
try {
|
||||
const rawInfo = storage.getString(`tmp_download_info_${itemId}`);
|
||||
|
||||
if (rawInfo) {
|
||||
return JSON.parse(rawInfo);
|
||||
}
|
||||
return null;
|
||||
} catch (error) {
|
||||
console.error("Failed to retrieve download item info from disk:", error);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Deletes the download item info from disk.
|
||||
*
|
||||
* @param {string} itemId - The ID of the item to delete.
|
||||
* @return {boolean} True if the item info was successfully deleted, false otherwise.
|
||||
*/
|
||||
export function deleteDownloadItemInfoFromDiskTmp(itemId: string): boolean {
|
||||
try {
|
||||
storage.delete(`tmp_download_info_${itemId}`);
|
||||
return true;
|
||||
} catch (error) {
|
||||
console.error("Failed to delete download item info from disk:", error);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -59,80 +59,55 @@ export default {
|
||||
],
|
||||
SubtitleProfiles: [
|
||||
// Official foramts
|
||||
{ Format: "vtt", Method: "Embed" },
|
||||
{ Format: "vtt", Method: "Encode" },
|
||||
|
||||
{ Format: "webvtt", Method: "Embed" },
|
||||
{ Format: "webvtt", Method: "Encode" },
|
||||
|
||||
{ Format: "srt", Method: "Embed" },
|
||||
{ Format: "srt", Method: "Encode" },
|
||||
|
||||
{ Format: "subrip", Method: "Embed" },
|
||||
{ Format: "subrip", Method: "Encode" },
|
||||
|
||||
{ Format: "ttml", Method: "Embed" },
|
||||
{ Format: "ttml", Method: "Encode" },
|
||||
|
||||
{ Format: "dvbsub", Method: "Embed" },
|
||||
{ Format: "dvdsub", Method: "Encode" },
|
||||
|
||||
{ Format: "ass", Method: "Embed" },
|
||||
{ Format: "ass", Method: "Encode" },
|
||||
|
||||
{ Format: "idx", Method: "Embed" },
|
||||
{ Format: "idx", Method: "Encode" },
|
||||
|
||||
{ Format: "pgs", Method: "Embed" },
|
||||
{ Format: "pgs", Method: "Encode" },
|
||||
|
||||
{ Format: "pgssub", Method: "Embed" },
|
||||
{ Format: "pgssub", Method: "Encode" },
|
||||
|
||||
{ Format: "ssa", Method: "Embed" },
|
||||
{ Format: "ssa", Method: "Encode" },
|
||||
|
||||
// Other formats
|
||||
{ Format: "microdvd", Method: "Embed" },
|
||||
{ Format: "microdvd", Method: "Encode" },
|
||||
|
||||
{ Format: "mov_text", Method: "Embed" },
|
||||
{ Format: "mov_text", Method: "Encode" },
|
||||
|
||||
{ Format: "mpl2", Method: "Embed" },
|
||||
{ Format: "mpl2", Method: "Encode" },
|
||||
|
||||
{ Format: "pjs", Method: "Embed" },
|
||||
{ Format: "pjs", Method: "Encode" },
|
||||
|
||||
{ Format: "realtext", Method: "Embed" },
|
||||
{ Format: "realtext", Method: "Encode" },
|
||||
|
||||
{ Format: "scc", Method: "Embed" },
|
||||
{ Format: "scc", Method: "Encode" },
|
||||
|
||||
{ Format: "smi", Method: "Embed" },
|
||||
{ Format: "smi", Method: "Encode" },
|
||||
|
||||
{ Format: "stl", Method: "Embed" },
|
||||
{ Format: "stl", Method: "Encode" },
|
||||
|
||||
{ Format: "sub", Method: "Embed" },
|
||||
{ Format: "sub", Method: "Encode" },
|
||||
|
||||
{ Format: "subviewer", Method: "Embed" },
|
||||
{ Format: "subviewer", Method: "Encode" },
|
||||
|
||||
{ Format: "teletext", Method: "Embed" },
|
||||
{ Format: "teletext", Method: "Encode" },
|
||||
|
||||
{ Format: "text", Method: "Embed" },
|
||||
{ Format: "text", Method: "Encode" },
|
||||
|
||||
{ Format: "vplayer", Method: "Embed" },
|
||||
{ Format: "vplayer", Method: "Encode" },
|
||||
|
||||
{ Format: "xsub", Method: "Embed" },
|
||||
{ Format: "xsub", Method: "Encode" },
|
||||
],
|
||||
};
|
||||
|
||||
@@ -4,13 +4,14 @@
|
||||
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
||||
*/
|
||||
import MediaTypes from "../../constants/MediaTypes";
|
||||
import { getSubtitleProfiles } from "./subtitles";
|
||||
|
||||
export const generateDeviceProfile = async () => {
|
||||
export const generateDeviceProfile = ({ transcode = false } = {}) => {
|
||||
/**
|
||||
* Device profile for Native video player
|
||||
*/
|
||||
const profile = {
|
||||
Name: "1. Vlc Player",
|
||||
Name: `1. Vlc Player${transcode ? " (Transcoding)" : ""}`,
|
||||
MaxStaticBitrate: 999_999_999,
|
||||
MaxStreamingBitrate: 999_999_999,
|
||||
CodecProfiles: [
|
||||
@@ -74,89 +75,8 @@ export const generateDeviceProfile = async () => {
|
||||
MaxAudioChannels: "2",
|
||||
},
|
||||
],
|
||||
SubtitleProfiles: [
|
||||
// Official formats
|
||||
{ Format: "vtt", Method: "Embed" },
|
||||
{ Format: "vtt", Method: "External" },
|
||||
|
||||
{ Format: "webvtt", Method: "Embed" },
|
||||
{ Format: "webvtt", Method: "External" },
|
||||
|
||||
{ Format: "srt", Method: "Embed" },
|
||||
{ Format: "srt", Method: "External" },
|
||||
|
||||
{ Format: "subrip", Method: "Embed" },
|
||||
{ Format: "subrip", Method: "External" },
|
||||
|
||||
{ Format: "ttml", Method: "Embed" },
|
||||
{ Format: "ttml", Method: "External" },
|
||||
|
||||
{ Format: "dvbsub", Method: "Embed" },
|
||||
{ Format: "dvdsub", Method: "Encode" },
|
||||
|
||||
{ Format: "ass", Method: "Embed" },
|
||||
{ Format: "ass", Method: "External" },
|
||||
|
||||
{ Format: "idx", Method: "Embed" },
|
||||
{ Format: "idx", Method: "Encode" },
|
||||
|
||||
{ Format: "pgs", Method: "Embed" },
|
||||
{ Format: "pgs", Method: "Encode" },
|
||||
|
||||
{ Format: "pgssub", Method: "Embed" },
|
||||
{ Format: "pgssub", Method: "Encode" },
|
||||
|
||||
{ Format: "ssa", Method: "Embed" },
|
||||
{ Format: "ssa", Method: "External" },
|
||||
|
||||
// Other formats
|
||||
{ Format: "microdvd", Method: "Embed" },
|
||||
{ Format: "microdvd", Method: "External" },
|
||||
|
||||
{ Format: "mov_text", Method: "Embed" },
|
||||
{ Format: "mov_text", Method: "External" },
|
||||
|
||||
{ Format: "mpl2", Method: "Embed" },
|
||||
{ Format: "mpl2", Method: "External" },
|
||||
|
||||
{ Format: "pjs", Method: "Embed" },
|
||||
{ Format: "pjs", Method: "External" },
|
||||
|
||||
{ Format: "realtext", Method: "Embed" },
|
||||
{ Format: "realtext", Method: "External" },
|
||||
|
||||
{ Format: "scc", Method: "Embed" },
|
||||
{ Format: "scc", Method: "External" },
|
||||
|
||||
{ Format: "smi", Method: "Embed" },
|
||||
{ Format: "smi", Method: "External" },
|
||||
|
||||
{ Format: "stl", Method: "Embed" },
|
||||
{ Format: "stl", Method: "External" },
|
||||
|
||||
{ Format: "sub", Method: "Embed" },
|
||||
{ Format: "sub", Method: "External" },
|
||||
|
||||
{ Format: "subviewer", Method: "Embed" },
|
||||
{ Format: "subviewer", Method: "External" },
|
||||
|
||||
{ Format: "teletext", Method: "Embed" },
|
||||
{ Format: "teletext", Method: "Encode" },
|
||||
|
||||
{ Format: "text", Method: "Embed" },
|
||||
{ Format: "text", Method: "External" },
|
||||
|
||||
{ Format: "vplayer", Method: "Embed" },
|
||||
{ Format: "vplayer", Method: "External" },
|
||||
|
||||
{ Format: "xsub", Method: "Embed" },
|
||||
{ Format: "xsub", Method: "External" },
|
||||
],
|
||||
SubtitleProfiles: getSubtitleProfiles(transcode ? "hls" : "External"),
|
||||
};
|
||||
|
||||
return profile;
|
||||
};
|
||||
|
||||
export default async () => {
|
||||
return await generateDeviceProfile();
|
||||
};
|
||||
|
||||
56
utils/profiles/subtitles.js
Normal file
56
utils/profiles/subtitles.js
Normal file
@@ -0,0 +1,56 @@
|
||||
/**
|
||||
* This Source Code Form is subject to the terms of the Mozilla Public
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
||||
*/
|
||||
|
||||
const COMMON_SUBTITLE_PROFILES = [
|
||||
// Official formats
|
||||
|
||||
{ Format: "dvdsub", Method: "Embed" },
|
||||
{ Format: "dvdsub", Method: "Encode" },
|
||||
|
||||
{ Format: "idx", Method: "Embed" },
|
||||
{ Format: "idx", Method: "Encode" },
|
||||
|
||||
{ Format: "pgs", Method: "Embed" },
|
||||
{ Format: "pgs", Method: "Encode" },
|
||||
|
||||
{ Format: "pgssub", Method: "Embed" },
|
||||
{ Format: "pgssub", Method: "Encode" },
|
||||
|
||||
{ Format: "teletext", Method: "Embed" },
|
||||
{ Format: "teletext", Method: "Encode" },
|
||||
];
|
||||
|
||||
const VARYING_SUBTITLE_FORMATS = [
|
||||
"webvtt",
|
||||
"vtt",
|
||||
"srt",
|
||||
"subrip",
|
||||
"ttml",
|
||||
"ass",
|
||||
"ssa",
|
||||
"microdvd",
|
||||
"mov_text",
|
||||
"mpl2",
|
||||
"pjs",
|
||||
"realtext",
|
||||
"scc",
|
||||
"smi",
|
||||
"stl",
|
||||
"sub",
|
||||
"subviewer",
|
||||
"text",
|
||||
"vplayer",
|
||||
"xsub",
|
||||
];
|
||||
|
||||
export const getSubtitleProfiles = (secondaryMethod) => {
|
||||
const profiles = [...COMMON_SUBTITLE_PROFILES];
|
||||
for (const format of VARYING_SUBTITLE_FORMATS) {
|
||||
profiles.push({ Format: format, Method: "Embed" });
|
||||
profiles.push({ Format: format, Method: secondaryMethod });
|
||||
}
|
||||
return profiles;
|
||||
};
|
||||
114
utils/segments.ts
Normal file
114
utils/segments.ts
Normal file
@@ -0,0 +1,114 @@
|
||||
import { Api } from "@jellyfin/sdk";
|
||||
import { useQuery } from "@tanstack/react-query";
|
||||
import { useAtom } from "jotai";
|
||||
import { useDownload } from "@/providers/DownloadProvider";
|
||||
import { DownloadedItem, MediaTimeSegment } from "@/providers/Downloads/types";
|
||||
import { apiAtom } from "@/providers/JellyfinProvider";
|
||||
import { getAuthHeaders } from "./jellyfin/jellyfin";
|
||||
|
||||
interface IntroTimestamps {
|
||||
EpisodeId: string;
|
||||
HideSkipPromptAt: number;
|
||||
IntroEnd: number;
|
||||
IntroStart: number;
|
||||
ShowSkipPromptAt: number;
|
||||
Valid: boolean;
|
||||
}
|
||||
|
||||
interface CreditTimestamps {
|
||||
Introduction: {
|
||||
Start: number;
|
||||
End: number;
|
||||
Valid: boolean;
|
||||
};
|
||||
Credits: {
|
||||
Start: number;
|
||||
End: number;
|
||||
Valid: boolean;
|
||||
};
|
||||
}
|
||||
|
||||
export const useSegments = (itemId: string, isOffline: boolean) => {
|
||||
const [api] = useAtom(apiAtom);
|
||||
const { downloadedFiles } = useDownload();
|
||||
const downloadedItem = downloadedFiles?.find(
|
||||
(d: DownloadedItem) => d.item.Id === itemId,
|
||||
);
|
||||
|
||||
return useQuery({
|
||||
queryKey: ["segments", itemId, isOffline],
|
||||
queryFn: async () => {
|
||||
if (isOffline && downloadedItem) {
|
||||
return getSegmentsForItem(downloadedItem);
|
||||
}
|
||||
if (!api) {
|
||||
throw new Error("API client is not available");
|
||||
}
|
||||
return fetchAndParseSegments(itemId, api);
|
||||
},
|
||||
enabled: !!api,
|
||||
});
|
||||
};
|
||||
|
||||
export const getSegmentsForItem = (
|
||||
item: DownloadedItem,
|
||||
): {
|
||||
introSegments: MediaTimeSegment[];
|
||||
creditSegments: MediaTimeSegment[];
|
||||
} => {
|
||||
return {
|
||||
introSegments: item.introSegments || [],
|
||||
creditSegments: item.creditSegments || [],
|
||||
};
|
||||
};
|
||||
|
||||
export const fetchAndParseSegments = async (
|
||||
itemId: string,
|
||||
api: Api,
|
||||
): Promise<{
|
||||
introSegments: MediaTimeSegment[];
|
||||
creditSegments: MediaTimeSegment[];
|
||||
}> => {
|
||||
const introSegments: MediaTimeSegment[] = [];
|
||||
const creditSegments: MediaTimeSegment[] = [];
|
||||
|
||||
try {
|
||||
const [introRes, creditRes] = await Promise.allSettled([
|
||||
api.axiosInstance.get<IntroTimestamps>(
|
||||
`${api.basePath}/Episode/${itemId}/IntroTimestamps`,
|
||||
{
|
||||
headers: getAuthHeaders(api),
|
||||
},
|
||||
),
|
||||
api.axiosInstance.get<CreditTimestamps>(
|
||||
`${api.basePath}/Episode/${itemId}/Timestamps`,
|
||||
{
|
||||
headers: getAuthHeaders(api),
|
||||
},
|
||||
),
|
||||
]);
|
||||
|
||||
if (introRes.status === "fulfilled" && introRes.value.data.Valid) {
|
||||
introSegments.push({
|
||||
startTime: introRes.value.data.IntroStart,
|
||||
endTime: introRes.value.data.IntroEnd,
|
||||
text: "Intro",
|
||||
});
|
||||
}
|
||||
|
||||
if (
|
||||
creditRes.status === "fulfilled" &&
|
||||
creditRes.value.data.Credits.Valid
|
||||
) {
|
||||
creditSegments.push({
|
||||
startTime: creditRes.value.data.Credits.Start,
|
||||
endTime: creditRes.value.data.Credits.End,
|
||||
text: "Credits",
|
||||
});
|
||||
}
|
||||
} catch (error) {
|
||||
console.error("Failed to fetch segments", error);
|
||||
}
|
||||
|
||||
return { introSegments, creditSegments };
|
||||
};
|
||||
Reference in New Issue
Block a user