mirror of
https://github.com/streamyfin/streamyfin.git
synced 2025-08-20 18:37:18 +02:00
fix
This commit is contained in:
@@ -1,5 +1,8 @@
|
||||
import { Api } from "@jellyfin/sdk";
|
||||
import { BaseItemDto } from "@jellyfin/sdk/lib/generated-client/models";
|
||||
import {
|
||||
BaseItemDto,
|
||||
PlaybackInfoResponse,
|
||||
} from "@jellyfin/sdk/lib/generated-client/models";
|
||||
import {
|
||||
getMediaInfoApi,
|
||||
getUserLibraryApi,
|
||||
@@ -59,14 +62,17 @@ export const useDownloadMedia = (api: Api | null) => {
|
||||
console.log("File downloaded to:", uri);
|
||||
|
||||
const currentFiles: BaseItemDto[] = JSON.parse(
|
||||
(await AsyncStorage.getItem("downloaded_files")) || "[]"
|
||||
(await AsyncStorage.getItem("downloaded_files")) ?? "[]"
|
||||
);
|
||||
|
||||
const otherItems = currentFiles.filter((i) => i.Id !== itemId);
|
||||
const updatedFiles = [
|
||||
...currentFiles.filter((file) => file.Id !== itemId),
|
||||
item,
|
||||
];
|
||||
|
||||
await AsyncStorage.setItem(
|
||||
"downloaded_files",
|
||||
JSON.stringify([...otherItems, item])
|
||||
JSON.stringify(updatedFiles)
|
||||
);
|
||||
|
||||
setIsDownloading(false);
|
||||
@@ -429,6 +435,7 @@ export const getStreamUrl = async ({
|
||||
startTimeTicks = 0,
|
||||
maxStreamingBitrate = 140000000,
|
||||
forceTranscoding = false,
|
||||
sessionData,
|
||||
}: {
|
||||
api: Api | null | undefined;
|
||||
item: BaseItemDto | null | undefined;
|
||||
@@ -436,6 +443,7 @@ export const getStreamUrl = async ({
|
||||
startTimeTicks: number;
|
||||
maxStreamingBitrate?: number;
|
||||
forceTranscoding?: boolean;
|
||||
sessionData: PlaybackInfoResponse;
|
||||
}) => {
|
||||
if (!api || !userId || !item?.Id) {
|
||||
return null;
|
||||
@@ -469,38 +477,72 @@ export const getStreamUrl = async ({
|
||||
);
|
||||
|
||||
const data = response.data;
|
||||
const mediaSource = item.MediaSources?.[0];
|
||||
const sessionId = sessionData.PlaySessionId;
|
||||
|
||||
if (item.MediaSources?.[0].SupportsDirectPlay) {
|
||||
console.log("Direct play supported");
|
||||
}
|
||||
if (!mediaSource) throw new Error("no media source");
|
||||
if (!sessionId) throw new Error("no sessionId");
|
||||
|
||||
if (
|
||||
item.MediaSources?.[0].SupportsTranscoding &&
|
||||
data.MediaSources?.[0].TranscodingUrl
|
||||
) {
|
||||
console.log("Supports transcoding");
|
||||
}
|
||||
const streamParams = new URLSearchParams({
|
||||
Static: "true",
|
||||
api_key: api.accessToken,
|
||||
playSessionId: sessionData.PlaySessionId || "",
|
||||
videoCodec: "hevc,h264",
|
||||
audioCodec: "aac,mp3,ac3,eac3,flac,alac",
|
||||
maxAudioChannels: "6",
|
||||
mediaSourceId: itemId,
|
||||
Tag: mediaSource.ETag || "",
|
||||
VideoBitrate: "324036",
|
||||
TranscodingMaxAudioChannels: "2",
|
||||
RequireAvc: "false",
|
||||
SegmentContainer: "mp4",
|
||||
MinSegments: "2",
|
||||
BreakOnNonKeyFrames: "True",
|
||||
"h264-level": "40",
|
||||
"h264-videobitdepth": "8",
|
||||
"h264-profile": "high",
|
||||
"h264-audiochannels": "2",
|
||||
"aac-profile": "lc",
|
||||
"h264-rangetype": "SDR",
|
||||
"h264-deinterlace": "true",
|
||||
TranscodeReasons: "ContainerBitrateExceedsLimit",
|
||||
});
|
||||
|
||||
if (
|
||||
item.MediaSources?.[0].SupportsTranscoding &&
|
||||
!data.MediaSources?.[0].TranscodingUrl
|
||||
) {
|
||||
console.log("Supports transcoding, but no URL found");
|
||||
}
|
||||
url = `${
|
||||
api.basePath
|
||||
}/Videos/${itemId}/main.m3u8?${streamParams.toString()}`;
|
||||
|
||||
if (data.MediaSources?.[0].TranscodingUrl) {
|
||||
url = api.basePath + data.MediaSources?.[0].TranscodingUrl;
|
||||
} else {
|
||||
url = buildStreamUrl({
|
||||
apiKey: api.accessToken || "",
|
||||
sessionId: "",
|
||||
itemId: itemId,
|
||||
serverUrl: api.basePath || "",
|
||||
deviceId: "unique-device-id",
|
||||
mediaSourceId: data.MediaSources?.[0].Id || "",
|
||||
tag: data.MediaSources?.[0]?.ETag || "",
|
||||
}).toString();
|
||||
}
|
||||
// if (item.MediaSources?.[0].SupportsDirectPlay) {
|
||||
// console.log("Direct play supported");
|
||||
// }
|
||||
|
||||
// if (
|
||||
// item.MediaSources?.[0].SupportsTranscoding &&
|
||||
// data.MediaSources?.[0].TranscodingUrl
|
||||
// ) {
|
||||
// console.log("Supports transcoding");
|
||||
// }
|
||||
|
||||
// if (
|
||||
// item.MediaSources?.[0].SupportsTranscoding &&
|
||||
// !data.MediaSources?.[0].TranscodingUrl
|
||||
// ) {
|
||||
// console.log("Supports transcoding, but no URL found");
|
||||
// }
|
||||
|
||||
// if (data.MediaSources?.[0].TranscodingUrl) {
|
||||
// url = api.basePath + data.MediaSources?.[0].TranscodingUrl;
|
||||
// } else {
|
||||
// url = buildStreamUrl({
|
||||
// apiKey: api.accessToken || "",
|
||||
// sessionId: "",
|
||||
// itemId: itemId,
|
||||
// serverUrl: api.basePath || "",
|
||||
// deviceId: "unique-device-id",
|
||||
// mediaSourceId: data.MediaSources?.[0].Id || "",
|
||||
// tag: data.MediaSources?.[0]?.ETag || "",
|
||||
// }).toString();
|
||||
// }
|
||||
} catch (e) {
|
||||
console.error(e);
|
||||
}
|
||||
|
||||
76
utils/video/createVideoUrl.ts
Normal file
76
utils/video/createVideoUrl.ts
Normal file
@@ -0,0 +1,76 @@
|
||||
import { MediaSourceInfo } from "@jellyfin/sdk/lib/generated-client/models";
|
||||
|
||||
export function createVideoUrl(mediaSource: MediaSourceInfo): string {
|
||||
const baseUrl = `/videos/${mediaSource.Id}/main.m3u8`;
|
||||
const urlParams = new URLSearchParams();
|
||||
|
||||
// Extract query parameters from TranscodingUrl
|
||||
const transcodingUrlParts = mediaSource.TranscodingUrl?.split("?") ?? [];
|
||||
if (transcodingUrlParts.length > 1) {
|
||||
const queryParams = new URLSearchParams(transcodingUrlParts[1]);
|
||||
queryParams.forEach((value, key) => {
|
||||
urlParams.append(key, value);
|
||||
});
|
||||
}
|
||||
|
||||
// Add or update specific parameters based on the mediaSource object
|
||||
if (mediaSource.DefaultAudioStreamIndex !== undefined) {
|
||||
urlParams.set(
|
||||
"AudioStreamIndex",
|
||||
mediaSource.DefaultAudioStreamIndex?.toString() || ""
|
||||
);
|
||||
}
|
||||
|
||||
if (mediaSource.DefaultSubtitleStreamIndex !== undefined) {
|
||||
urlParams.set(
|
||||
"SubtitleStreamIndex",
|
||||
mediaSource.DefaultSubtitleStreamIndex?.toString() || ""
|
||||
);
|
||||
}
|
||||
|
||||
// Add information about available streams
|
||||
if (mediaSource.MediaStreams) {
|
||||
const videoStreams = mediaSource.MediaStreams.filter(
|
||||
(stream) => stream.Type === "Video"
|
||||
);
|
||||
const audioStreams = mediaSource.MediaStreams.filter(
|
||||
(stream) => stream.Type === "Audio"
|
||||
);
|
||||
const subtitleStreams = mediaSource.MediaStreams.filter(
|
||||
(stream) => stream.Type === "Subtitle"
|
||||
);
|
||||
|
||||
if (videoStreams.length > 0) {
|
||||
urlParams.set(
|
||||
"VideoStreamIndex",
|
||||
videoStreams[0].Index?.toString() || ""
|
||||
);
|
||||
}
|
||||
|
||||
if (audioStreams.length > 0) {
|
||||
const defaultAudioStream =
|
||||
audioStreams.find((stream) => stream.IsDefault) || audioStreams[0];
|
||||
urlParams.set(
|
||||
"AudioStreamIndex",
|
||||
defaultAudioStream.Index?.toString() || ""
|
||||
);
|
||||
urlParams.set("AudioCodec", defaultAudioStream.Codec || "");
|
||||
}
|
||||
|
||||
if (subtitleStreams.length > 0) {
|
||||
const defaultSubtitleStream = subtitleStreams.find(
|
||||
(stream) => stream.IsDefault
|
||||
);
|
||||
if (defaultSubtitleStream?.Index) {
|
||||
urlParams.set(
|
||||
"SubtitleStreamIndex",
|
||||
defaultSubtitleStream.Index.toString()
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
console.log("createVideoUrl ~", `${baseUrl}?${urlParams.toString()}`);
|
||||
|
||||
return `${baseUrl}?${urlParams.toString()}`;
|
||||
}
|
||||
Reference in New Issue
Block a user