This commit is contained in:
Fredrik Burmester
2024-09-28 15:45:00 +02:00
parent 79020c357f
commit 41a23d3437
18 changed files with 806 additions and 783 deletions

View File

@@ -1,222 +0,0 @@
import { apiAtom } from "@/providers/JellyfinProvider";
import { runningProcesses } from "@/utils/atoms/downloads";
import { writeToLog } from "@/utils/log";
import { BaseItemDto } from "@jellyfin/sdk/lib/generated-client/models";
import { download } from "@kesha-antonov/react-native-background-downloader";
import AsyncStorage from "@react-native-async-storage/async-storage";
import { useQueryClient } from "@tanstack/react-query";
import * as FileSystem from "expo-file-system";
import { FFmpegKit, ReturnCode } from "ffmpeg-kit-react-native";
import { useAtom } from "jotai";
import { useCallback, useEffect, useState } from "react";
import { toast } from "sonner-native";
export const useDownloadM3U8Files = (item: BaseItemDto) => {
const [_, setProgress] = useAtom(runningProcesses);
const queryClient = useQueryClient();
const [api] = useAtom(apiAtom);
const [totalSegments, setTotalSegments] = useState<number>(0);
const [downloadedSegments, setDownloadedSegments] = useState<number[]>([]);
if (!item.Id || !item.Name) {
throw new Error("Item must have an Id and Name");
}
const startBackgroundDownload = useCallback(
async (url: string) => {
if (!api) {
throw new Error("API is not defined");
}
toast.success("Download started", { invert: true });
writeToLog("INFO", `Starting download for item ${item.Name}`);
setProgress({
startTime: new Date(),
item,
progress: 0,
});
try {
const directoryPath = `${FileSystem.documentDirectory}${item.Id}`;
await FileSystem.makeDirectoryAsync(directoryPath, {
intermediates: true,
});
const m3u8Content = await FileSystem.downloadAsync(
url,
`${directoryPath}/original.m3u8`
);
if (m3u8Content.status !== 200) {
throw new Error("Failed to download m3u8 file");
}
const m3u8Text = await FileSystem.readAsStringAsync(m3u8Content.uri);
const segments = await fetchSegmentInfo(
m3u8Text,
api.basePath,
item.Id!
);
setTotalSegments(segments.length);
for (let i = 0; i < segments.length; i++) {
const segment = segments[i];
const segmentUrl = `${api.basePath}/videos/${item.Id}/${segment.path}`;
const destination = `${directoryPath}/${i}.ts`;
download({
id: `${item.Id}_segment_${i}`,
url: segmentUrl,
destination: destination,
}).done(() => {
setDownloadedSegments((prev) => [...prev, i]);
});
}
await createLocalM3U8File(segments, directoryPath);
await saveDownloadedItemInfo(item);
writeToLog("INFO", `Download completed for item: ${item.Name}`);
await queryClient.invalidateQueries({ queryKey: ["downloaded_files"] });
await queryClient.invalidateQueries({ queryKey: ["downloaded"] });
} catch (error) {
console.error("Failed to download:", error);
writeToLog("ERROR", `Download failed for item: ${item.Name}`);
setProgress(null);
throw error;
}
},
[item, queryClient, api]
);
useEffect(() => {
if (totalSegments === 0) return;
console.log("[0]", downloadedSegments.length, totalSegments);
const progress = (downloadedSegments.length / totalSegments) * 100;
setProgress((prev) => ({
...prev!,
progress,
}));
if (progress > 99) {
setProgress(null);
}
}, [downloadedSegments, totalSegments]);
return { startBackgroundDownload };
};
interface Segment {
duration: number;
path: string;
}
async function fetchSegmentInfo(
masterM3U8Content: string,
baseUrl: string,
itemId: string
): Promise<Segment[]> {
const lines = masterM3U8Content.split("\n");
const mainPlaylistLine = lines.find((line) => line.startsWith("main.m3u8"));
if (!mainPlaylistLine) {
throw new Error("Main playlist URL not found in the master M3U8");
}
const url = `${baseUrl}/videos/${itemId}/${mainPlaylistLine}`;
const response = await fetch(url);
const mainPlaylistContent = await response.text();
const segments: Segment[] = [];
const mainPlaylistLines = mainPlaylistContent.split("\n");
for (let i = 0; i < mainPlaylistLines.length; i++) {
if (mainPlaylistLines[i].startsWith("#EXTINF:")) {
const durationMatch = mainPlaylistLines[i].match(
/#EXTINF:(\d+(?:\.\d+)?)/
);
const duration = durationMatch ? parseFloat(durationMatch[1]) : 0;
const path = mainPlaylistLines[i + 1];
if (path) {
segments.push({ duration, path });
}
i++;
}
}
return segments;
}
async function createLocalM3U8File(segments: Segment[], directoryPath: string) {
let localM3U8Content = "#EXTM3U\n#EXT-X-VERSION:3\n";
localM3U8Content += `#EXT-X-TARGETDURATION:${Math.ceil(
Math.max(...segments.map((s) => s.duration))
)}\n`;
localM3U8Content += "#EXT-X-MEDIA-SEQUENCE:0\n";
segments.forEach((segment, index) => {
console.log(segment.path.split(".")[1]);
localM3U8Content += `#EXTINF:${segment.duration.toFixed(3)},\n`;
localM3U8Content += `${directoryPath}/${index}.ts\n`;
});
localM3U8Content += "#EXT-X-ENDLIST\n";
const localM3U8Path = `${directoryPath}/local.m3u8`;
await FileSystem.writeAsStringAsync(localM3U8Path, localM3U8Content);
}
export async function saveDownloadedItemInfo(item: BaseItemDto) {
try {
const downloadedItems = await AsyncStorage.getItem("downloadedItems");
let items: BaseItemDto[] = downloadedItems
? JSON.parse(downloadedItems)
: [];
const existingItemIndex = items.findIndex((i) => i.Id === item.Id);
if (existingItemIndex !== -1) {
items[existingItemIndex] = item;
} else {
items.push(item);
}
await AsyncStorage.setItem("downloadedItems", JSON.stringify(items));
} catch (error) {
console.error("Failed to save downloaded item information:", error);
}
}
export async function deleteDownloadedItem(itemId: string) {
try {
const downloadedItems = await AsyncStorage.getItem("downloadedItems");
let items: BaseItemDto[] = downloadedItems
? JSON.parse(downloadedItems)
: [];
items = items.filter((item) => item.Id !== itemId);
await AsyncStorage.setItem("downloadedItems", JSON.stringify(items));
const directoryPath = `${FileSystem.documentDirectory}${itemId}`;
await FileSystem.deleteAsync(directoryPath, { idempotent: true });
} catch (error) {
console.error("Failed to delete downloaded item:", error);
}
}
export async function getAllDownloadedItems(): Promise<BaseItemDto[]> {
try {
const downloadedItems = await AsyncStorage.getItem("downloadedItems");
if (downloadedItems) {
return JSON.parse(downloadedItems) as BaseItemDto[];
} else {
return [];
}
} catch (error) {
console.error("Failed to retrieve downloaded items:", error);
return [];
}
}

View File

@@ -1,117 +0,0 @@
import { useCallback, useRef, useState } from "react";
import { useAtom } from "jotai";
import AsyncStorage from "@react-native-async-storage/async-storage";
import * as FileSystem from "expo-file-system";
import { Api } from "@jellyfin/sdk";
import { BaseItemDto } from "@jellyfin/sdk/lib/generated-client/models";
import { runningProcesses } from "@/utils/atoms/downloads";
/**
* Custom hook for downloading media using the Jellyfin API.
*
* @param api - The Jellyfin API instance
* @param userId - The user ID
* @returns An object with download-related functions and state
*/
export const useDownloadMedia = (api: Api | null, userId?: string | null) => {
const [isDownloading, setIsDownloading] = useState(false);
const [error, setError] = useState<string | null>(null);
const [_, setProgress] = useAtom(runningProcesses);
const downloadResumableRef = useRef<FileSystem.DownloadResumable | null>(
null,
);
const downloadMedia = useCallback(
async (item: BaseItemDto | null): Promise<boolean> => {
if (!item?.Id || !api || !userId) {
setError("Invalid item or API");
return false;
}
setIsDownloading(true);
setError(null);
setProgress({ item, progress: 0 });
try {
const filename = item.Id;
const fileUri = `${FileSystem.documentDirectory}${filename}`;
const url = `${api.basePath}/Items/${item.Id}/File`;
downloadResumableRef.current = FileSystem.createDownloadResumable(
url,
fileUri,
{
headers: {
Authorization: `MediaBrowser DeviceId="${api.deviceInfo.id}", Token="${api.accessToken}"`,
},
},
(downloadProgress) => {
const currentProgress =
downloadProgress.totalBytesWritten /
downloadProgress.totalBytesExpectedToWrite;
setProgress({ item, progress: currentProgress * 100 });
},
);
const res = await downloadResumableRef.current.downloadAsync();
if (!res?.uri) {
throw new Error("Download failed: No URI returned");
}
await updateDownloadedFiles(item);
setIsDownloading(false);
setProgress(null);
return true;
} catch (error) {
console.error("Error downloading media:", error);
setError("Failed to download media");
setIsDownloading(false);
setProgress(null);
return false;
}
},
[api, userId, setProgress],
);
const cancelDownload = useCallback(async (): Promise<void> => {
if (!downloadResumableRef.current) return;
try {
await downloadResumableRef.current.pauseAsync();
setIsDownloading(false);
setError("Download cancelled");
setProgress(null);
downloadResumableRef.current = null;
} catch (error) {
console.error("Error cancelling download:", error);
setError("Failed to cancel download");
}
}, [setProgress]);
return { downloadMedia, isDownloading, error, cancelDownload };
};
/**
* Updates the list of downloaded files in AsyncStorage.
*
* @param item - The item to add to the downloaded files list
*/
async function updateDownloadedFiles(item: BaseItemDto): Promise<void> {
try {
const currentFiles: BaseItemDto[] = JSON.parse(
(await AsyncStorage.getItem("downloaded_files")) ?? "[]",
);
const updatedFiles = [
...currentFiles.filter((file) => file.Id !== item.Id),
item,
];
await AsyncStorage.setItem(
"downloaded_files",
JSON.stringify(updatedFiles),
);
} catch (error) {
console.error("Error updating downloaded files:", error);
}
}

View File

@@ -1,11 +1,10 @@
// hooks/useFileOpener.ts
import { useCallback } from "react";
import { useRouter } from "expo-router";
import * as FileSystem from "expo-file-system";
import { BaseItemDto } from "@jellyfin/sdk/lib/generated-client";
import { usePlayback } from "@/providers/PlaybackProvider";
import { FFmpegKit, ReturnCode } from "ffmpeg-kit-react-native";
import { BaseItemDto } from "@jellyfin/sdk/lib/generated-client";
import * as FileSystem from "expo-file-system";
import { useRouter } from "expo-router";
import { useCallback } from "react";
export const useFileOpener = () => {
const router = useRouter();
@@ -13,77 +12,17 @@ export const useFileOpener = () => {
const openFile = useCallback(
async (item: BaseItemDto) => {
const m3u8File = `${FileSystem.documentDirectory}${item.Id}/local.m3u8`;
const outputFile = `${FileSystem.documentDirectory}${item.Id}/output.mp4`;
const directory = FileSystem.documentDirectory;
const url = `${directory}/${item.Id}.mp4`;
console.log("Checking for output file:", outputFile);
const outputFileInfo = await FileSystem.getInfoAsync(outputFile);
if (outputFileInfo.exists) {
console.log("Output MP4 file already exists. Playing directly.");
startDownloadedFilePlayback({
item,
url: outputFile,
});
router.push("/play");
return;
}
console.log("Output MP4 file does not exist. Converting from M3U8.");
const m3u8FileInfo = await FileSystem.getInfoAsync(m3u8File);
if (!m3u8FileInfo.exists) {
console.warn("m3u8 file does not exist:", m3u8File);
return;
}
const conversionSuccess = await convertM3U8ToMP4(m3u8File, outputFile);
if (conversionSuccess) {
startDownloadedFilePlayback({
item,
url: outputFile,
});
router.push("/play");
} else {
console.error("Failed to convert M3U8 to MP4");
// Handle conversion failure (e.g., show an error message to the user)
}
startDownloadedFilePlayback({
item,
url,
});
router.push("/play");
},
[startDownloadedFilePlayback]
);
return { openFile };
};
export async function convertM3U8ToMP4(
inputM3U8: string,
outputMP4: string
): Promise<boolean> {
console.log("Converting M3U8 to MP4");
console.log("Input M3U8:", inputM3U8);
console.log("Output MP4:", outputMP4);
try {
const command = `-i ${inputM3U8} -c copy ${outputMP4}`;
console.log("Executing FFmpeg command:", command);
const session = await FFmpegKit.execute(command);
const returnCode = await session.getReturnCode();
if (ReturnCode.isSuccess(returnCode)) {
console.log("Conversion completed successfully");
return true;
} else {
console.error("Conversion failed. Return code:", returnCode);
const output = await session.getOutput();
console.error("FFmpeg output:", output);
return false;
}
} catch (error) {
console.error("Error during conversion:", error);
return false;
}
}

View File

@@ -1,96 +0,0 @@
import { BaseItemDto } from "@jellyfin/sdk/lib/generated-client/models";
import AsyncStorage from "@react-native-async-storage/async-storage";
import { useQueryClient } from "@tanstack/react-query";
import * as FileSystem from "expo-file-system";
/**
* Custom hook for managing downloaded files.
* @returns An object with functions to delete individual files and all files.
*/
export const useFiles = () => {
const queryClient = useQueryClient();
/**
* Deletes all downloaded files and clears the download record.
*/
const deleteAllFiles = async (): Promise<void> => {
try {
// Get all downloaded items
const downloadedItems = await AsyncStorage.getItem("downloadedItems");
if (downloadedItems) {
const items = JSON.parse(downloadedItems);
// Delete each item's folder
for (const item of items) {
const folderPath = `${FileSystem.documentDirectory}${item.Id}`;
await FileSystem.deleteAsync(folderPath, { idempotent: true });
}
}
// Clear the downloadedItems in AsyncStorage
await AsyncStorage.removeItem("downloadedItems");
// Invalidate the query to refresh the UI
queryClient.invalidateQueries({ queryKey: ["downloaded_files"] });
console.log(
"Successfully deleted all downloaded files and cleared AsyncStorage"
);
} catch (error) {
console.error("Failed to delete all files:", error);
}
};
/**
* Deletes a specific file and updates the download record.
* @param id - The ID of the file to delete.
*/
const deleteFile = async (id: string): Promise<void> => {
if (!id) {
console.error("Invalid file ID");
return;
}
try {
// Delete the entire folder
const folderPath = `${FileSystem.documentDirectory}${id}`;
await FileSystem.deleteAsync(folderPath, { idempotent: true });
// Remove the item from AsyncStorage
const downloadedItems = await AsyncStorage.getItem("downloadedItems");
if (downloadedItems) {
let items = JSON.parse(downloadedItems);
items = items.filter((item: any) => item.Id !== id);
await AsyncStorage.setItem("downloadedItems", JSON.stringify(items));
}
// Invalidate the query to refresh the UI
queryClient.invalidateQueries({ queryKey: ["downloaded_files"] });
console.log(
`Successfully deleted folder and AsyncStorage entry for ID ${id}`
);
} catch (error) {
console.error(
`Failed to delete folder and AsyncStorage entry for ID ${id}:`,
error
);
}
};
return { deleteFile, deleteAllFiles };
};
/**
* Retrieves the list of downloaded files from AsyncStorage.
* @returns An array of BaseItemDto objects representing downloaded files.
*/
async function getDownloadedFiles(): Promise<BaseItemDto[]> {
try {
const filesJson = await AsyncStorage.getItem("downloaded_files");
return filesJson ? JSON.parse(filesJson) : [];
} catch (error) {
console.error("Failed to retrieve downloaded files:", error);
return [];
}
}

View File

@@ -4,10 +4,10 @@ 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 { runningProcesses } from "@/utils/atoms/downloads";
import { writeToLog } from "@/utils/log";
import { useQueryClient } from "@tanstack/react-query";
import { toast } from "sonner-native";
import { useDownload } from "@/providers/DownloadProvider";
/**
* Custom hook for remuxing HLS to MP4 using FFmpeg.
@@ -17,8 +17,9 @@ import { toast } from "sonner-native";
* @returns An object with remuxing-related functions
*/
export const useRemuxHlsToMp4 = (item: BaseItemDto) => {
const [_, setProgress] = useAtom(runningProcesses);
const queryClient = useQueryClient();
const { process, updateProcess, clearProcess, saveDownloadedItemInfo } =
useDownload();
if (!item.Id || !item.Name) {
writeToLog("ERROR", "useRemuxHlsToMp4 ~ missing arguments");
@@ -29,9 +30,7 @@ export const useRemuxHlsToMp4 = (item: BaseItemDto) => {
const startRemuxing = useCallback(
async (url: string) => {
toast.success("Download started", {
invert: true,
});
toast.success("Download started");
const command = `-y -loglevel quiet -thread_queue_size 512 -protocol_whitelist file,http,https,tcp,tls,crypto -multiple_requests 1 -tcp_nodelay 1 -fflags +genpts -i ${url} -c copy -bufsize 50M -max_muxing_queue_size 4096 ${output}`;
@@ -41,7 +40,12 @@ export const useRemuxHlsToMp4 = (item: BaseItemDto) => {
);
try {
setProgress({ item, progress: 0, startTime: new Date(), speed: 0 });
updateProcess({
id: item.Id!,
item,
progress: 0,
state: "downloading",
});
FFmpegKitConfig.enableStatisticsCallback((statistics) => {
const videoLength =
@@ -56,11 +60,13 @@ export const useRemuxHlsToMp4 = (item: BaseItemDto) => {
? Math.floor((processedFrames / totalFrames) * 100)
: 0;
setProgress((prev) =>
prev?.item.Id === item.Id!
? { ...prev, progress: percentage, speed }
: prev
);
updateProcess((prev) => {
if (!prev) return null;
return {
...prev,
progress: percentage,
};
});
});
// Await the execution of the FFmpeg command and ensure that the callback is awaited properly.
@@ -70,19 +76,25 @@ export const useRemuxHlsToMp4 = (item: BaseItemDto) => {
const returnCode = await session.getReturnCode();
if (returnCode.isValueSuccess()) {
await updateDownloadedFiles(item);
await saveDownloadedItemInfo(item);
toast.success("Download completed");
writeToLog(
"INFO",
`useRemuxHlsToMp4 ~ remuxing completed successfully for item: ${item.Name}`
);
await queryClient.invalidateQueries({
queryKey: ["downloadedItems"],
});
resolve();
} else if (returnCode.isValueError()) {
toast.success("Download failed");
writeToLog(
"ERROR",
`useRemuxHlsToMp4 ~ remuxing failed for item: ${item.Name}`
);
reject(new Error("Remuxing failed")); // Reject the promise on error
} else if (returnCode.isValueCancel()) {
toast.success("Download canceled");
writeToLog(
"INFO",
`useRemuxHlsToMp4 ~ remuxing was canceled for item: ${item.Name}`
@@ -90,63 +102,33 @@ export const useRemuxHlsToMp4 = (item: BaseItemDto) => {
resolve();
}
setProgress(null);
clearProcess();
} catch (error) {
reject(error);
}
});
});
await queryClient.invalidateQueries({ queryKey: ["downloaded_files"] });
await queryClient.invalidateQueries({ queryKey: ["downloaded"] });
} catch (error) {
console.error("Failed to remux:", error);
writeToLog(
"ERROR",
`useRemuxHlsToMp4 ~ remuxing failed for item: ${item.Name}`
);
setProgress(null);
clearProcess();
throw error; // Re-throw the error to propagate it to the caller
}
},
[output, item, setProgress]
[output, item, clearProcess]
);
const cancelRemuxing = useCallback(() => {
FFmpegKit.cancel();
setProgress(null);
clearProcess();
writeToLog(
"INFO",
`useRemuxHlsToMp4 ~ remuxing cancelled for item: ${item.Name}`
);
}, [item.Name, setProgress]);
}, [item.Name, clearProcess]);
return { startRemuxing, cancelRemuxing };
};
/**
* Updates the list of downloaded files in AsyncStorage.
*
* @param item - The item to add to the downloaded files list
*/
async function updateDownloadedFiles(item: BaseItemDto): Promise<void> {
try {
const currentFiles: BaseItemDto[] = JSON.parse(
(await AsyncStorage.getItem("downloaded_files")) || "[]"
);
const updatedFiles = [
...currentFiles.filter((i) => i.Id !== item.Id),
item,
];
await AsyncStorage.setItem(
"downloaded_files",
JSON.stringify(updatedFiles)
);
} catch (error) {
console.error("Error updating downloaded files:", error);
writeToLog(
"ERROR",
`Failed to update downloaded files for item: ${item.Name}`
);
}
}