This commit is contained in:
Fredrik Burmester
2024-11-23 22:42:04 +01:00
parent 4699ee9c18
commit 55f8af7069
13 changed files with 366 additions and 310 deletions

View File

@@ -3,7 +3,10 @@ import { useAtom, useAtomValue } from "jotai";
import AsyncStorage from "@react-native-async-storage/async-storage";
import * as FileSystem from "expo-file-system";
import { FFmpegKit, FFmpegKitConfig } from "ffmpeg-kit-react-native";
import { BaseItemDto } from "@jellyfin/sdk/lib/generated-client/models";
import {
BaseItemDto,
MediaSourceInfo,
} from "@jellyfin/sdk/lib/generated-client/models";
import { writeToLog } from "@/utils/log";
import { useQueryClient } from "@tanstack/react-query";
import { toast } from "sonner-native";
@@ -21,22 +24,16 @@ import { apiAtom } from "@/providers/JellyfinProvider";
* @param item - The BaseItemDto object representing the media item
* @returns An object with remuxing-related functions
*/
export const useRemuxHlsToMp4 = (item: BaseItemDto) => {
export const useRemuxHlsToMp4 = () => {
const api = useAtomValue(apiAtom);
const queryClient = useQueryClient();
const { saveDownloadedItemInfo, setProcesses } = useDownload();
const router = useRouter();
const { saveImage } = useImageStorage();
if (!item.Id || !item.Name) {
writeToLog("ERROR", "useRemuxHlsToMp4 ~ missing arguments");
throw new Error("Item must have an Id and Name");
}
const output = `${FileSystem.documentDirectory}${item.Id}.mp4`;
const startRemuxing = useCallback(
async (url: string) => {
async (item: BaseItemDto, url: string, mediaSource: MediaSourceInfo) => {
const output = `${FileSystem.documentDirectory}${item.Id}.mp4`;
if (!api) throw new Error("API is not defined");
if (!item.Id) throw new Error("Item must have an Id");
@@ -74,13 +71,16 @@ export const useRemuxHlsToMp4 = (item: BaseItemDto) => {
id: "",
deviceId: "",
inputUrl: "",
item,
itemId: item.Id,
item: {
item,
mediaSource,
},
itemId: item.Id!,
outputPath: "",
progress: 0,
status: "downloading",
timestamp: new Date(),
} as JobStatus,
},
]);
FFmpegKitConfig.enableStatisticsCallback((statistics) => {
@@ -119,7 +119,7 @@ export const useRemuxHlsToMp4 = (item: BaseItemDto) => {
if (returnCode.isValueSuccess()) {
if (!item) throw new Error("Item is undefined");
await saveDownloadedItemInfo(item);
await saveDownloadedItemInfo(item, mediaSource);
toast.success("Download completed");
writeToLog(
"INFO",
@@ -134,7 +134,7 @@ export const useRemuxHlsToMp4 = (item: BaseItemDto) => {
"ERROR",
`useRemuxHlsToMp4 ~ remuxing failed for item: ${item.Name}`
);
reject(new Error("Remuxing failed")); // Reject the promise on error
reject(new Error("Remuxing failed"));
} else if (returnCode.isValueCancel()) {
writeToLog(
"INFO",
@@ -163,15 +163,13 @@ export const useRemuxHlsToMp4 = (item: BaseItemDto) => {
throw error; // Re-throw the error to propagate it to the caller
}
},
[output, item]
[]
);
const cancelRemuxing = useCallback(() => {
FFmpegKit.cancel();
setProcesses((prev) => {
return prev.filter((process) => process.itemId !== item.Id);
});
}, [item.Name]);
setProcesses([]);
}, []);
return { startRemuxing, cancelRemuxing };
};