refactor: use MediaSegments api for intro/outro skipping
This commit is contained in:
4 files changed
+76
-56
No files matched your search
@@ -23,15 +23,14 @@ import {
|
|||||||
SortOrder,
|
SortOrder,
|
||||||
type UserItemDataDto,
|
type UserItemDataDto,
|
||||||
} from "@jellyfin/sdk/lib/generated-client";
|
} from "@jellyfin/sdk/lib/generated-client";
|
||||||
import { getDlnaApi } from "@jellyfin/sdk/lib/utils/api/dlna-api";
|
|
||||||
import { getItemsApi } from "@jellyfin/sdk/lib/utils/api/items-api";
|
import { getItemsApi } from "@jellyfin/sdk/lib/utils/api/items-api";
|
||||||
import { getPlaylistsApi } from "@jellyfin/sdk/lib/utils/api/playlists-api";
|
import { getPlaylistsApi } from "@jellyfin/sdk/lib/utils/api/playlists-api";
|
||||||
import { getTvShowsApi } from "@jellyfin/sdk/lib/utils/api/tv-shows-api";
|
import { getTvShowsApi } from "@jellyfin/sdk/lib/utils/api/tv-shows-api";
|
||||||
import { useMutation } from "@tanstack/react-query";
|
import { useMutation } from "@tanstack/react-query";
|
||||||
import { useNavigate, useRouteContext } from "@tanstack/react-router";
|
import { useNavigate } from "@tanstack/react-router";
|
||||||
import { useSnackbar } from "notistack";
|
import { useSnackbar } from "notistack";
|
||||||
|
|
||||||
import useQueue, { setQueue } from "@/utils/store/queue";
|
import { setQueue } from "@/utils/store/queue";
|
||||||
|
|
||||||
import { playItem, usePlaybackDataLoadStore } from "@/utils/store/playback";
|
import { playItem, usePlaybackDataLoadStore } from "@/utils/store/playback";
|
||||||
|
|
||||||
@@ -39,12 +38,10 @@ import type PlayResult from "@//utils/types/playResult";
|
|||||||
import { getRuntimeCompact } from "@/utils/date/time";
|
import { getRuntimeCompact } from "@/utils/date/time";
|
||||||
import getSubtitle from "@/utils/methods/getSubtitles";
|
import getSubtitle from "@/utils/methods/getSubtitles";
|
||||||
import playbackProfile from "@/utils/playback-profiles";
|
import playbackProfile from "@/utils/playback-profiles";
|
||||||
import { axiosClient, useApiInContext } from "@/utils/store/api";
|
import { useApiInContext } from "@/utils/store/api";
|
||||||
import { usePhotosPlayback } from "@/utils/store/photosPlayback";
|
import { usePhotosPlayback } from "@/utils/store/photosPlayback";
|
||||||
import type IntroMediaInfo from "@/utils/types/introMediaInfo";
|
|
||||||
import { getMediaInfoApi } from "@jellyfin/sdk/lib/utils/api/media-info-api";
|
import { getMediaInfoApi } from "@jellyfin/sdk/lib/utils/api/media-info-api";
|
||||||
import { getMediaSegmentsApi } from "@jellyfin/sdk/lib/utils/api/media-segments-api";
|
import { getMediaSegmentsApi } from "@jellyfin/sdk/lib/utils/api/media-segments-api";
|
||||||
import { getUserApi } from "@jellyfin/sdk/lib/utils/api/user-api";
|
|
||||||
import { getUserLibraryApi } from "@jellyfin/sdk/lib/utils/api/user-library-api";
|
import { getUserLibraryApi } from "@jellyfin/sdk/lib/utils/api/user-library-api";
|
||||||
import type { SxProps } from "@mui/material";
|
import type { SxProps } from "@mui/material";
|
||||||
import type { AxiosResponse } from "axios";
|
import type { AxiosResponse } from "axios";
|
||||||
@@ -92,13 +89,13 @@ const MemoizedLinearProgress = memo(({ value }: { value: number }) => (
|
|||||||
zIndex: 0,
|
zIndex: 0,
|
||||||
mixBlendMode: "difference",
|
mixBlendMode: "difference",
|
||||||
}}
|
}}
|
||||||
|
//@ts-ignore
|
||||||
color="white"
|
color="white"
|
||||||
/>
|
/>
|
||||||
));
|
));
|
||||||
|
|
||||||
const PlayButton = ({
|
const PlayButton = ({
|
||||||
item,
|
item,
|
||||||
itemId,
|
|
||||||
itemUserData,
|
itemUserData,
|
||||||
userId,
|
userId,
|
||||||
itemType,
|
itemType,
|
||||||
@@ -136,6 +133,9 @@ const PlayButton = ({
|
|||||||
if (!api) {
|
if (!api) {
|
||||||
throw new Error("API is not available");
|
throw new Error("API is not available");
|
||||||
}
|
}
|
||||||
|
if (!item.Id) {
|
||||||
|
throw new Error("Item ID is not available");
|
||||||
|
}
|
||||||
if (playlistItem) {
|
if (playlistItem) {
|
||||||
result = await getPlaylistsApi(api).getPlaylistItems({
|
result = await getPlaylistsApi(api).getPlaylistItems({
|
||||||
userId: userId,
|
userId: userId,
|
||||||
@@ -163,27 +163,22 @@ const PlayButton = ({
|
|||||||
audioStreamIndex: currentAudioTrack,
|
audioStreamIndex: currentAudioTrack,
|
||||||
subtitleStreamIndex:
|
subtitleStreamIndex:
|
||||||
currentSubTrack === "nosub" ? -1 : currentSubTrack,
|
currentSubTrack === "nosub" ? -1 : currentSubTrack,
|
||||||
itemId: result.data.Items?.[indexNumber].Id,
|
itemId: result.data.Items?.[indexNumber].Id ?? "",
|
||||||
startTimeTicks:
|
startTimeTicks:
|
||||||
result.data.Items?.[indexNumber].UserData
|
result.data.Items?.[indexNumber].UserData
|
||||||
?.PlaybackPositionTicks,
|
?.PlaybackPositionTicks,
|
||||||
userId: userId,
|
userId: userId,
|
||||||
mediaSourceId:
|
mediaSourceId:
|
||||||
result.data.Items?.[indexNumber].MediaSources?.[0].Id,
|
result.data.Items?.[indexNumber].MediaSources?.[0].Id ?? "",
|
||||||
playbackInfoDto: {
|
playbackInfoDto: {
|
||||||
DeviceProfile: playbackProfile,
|
DeviceProfile: playbackProfile,
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
try {
|
introInfo = (
|
||||||
introInfo = (
|
await getMediaSegmentsApi(api).getItemSegments({
|
||||||
await getMediaSegmentsApi(api).getItemSegments({
|
itemId: result.data.Items?.[indexNumber].Id ?? "",
|
||||||
itemId: item.Id,
|
})
|
||||||
includeSegmentTypes: ["Intro"],
|
)?.data;
|
||||||
})
|
|
||||||
)?.data;
|
|
||||||
} catch (error) {
|
|
||||||
console.error(error);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case BaseItemKind.Series:
|
case BaseItemKind.Series:
|
||||||
@@ -202,7 +197,7 @@ const PlayButton = ({
|
|||||||
if (
|
if (
|
||||||
result.data.Items?.[0].Id &&
|
result.data.Items?.[0].Id &&
|
||||||
result.data.Items?.[0].MediaSources?.[0].Id
|
result.data.Items?.[0].MediaSources?.[0].Id
|
||||||
)
|
) {
|
||||||
mediaSource = await getMediaInfoApi(api).getPostedPlaybackInfo({
|
mediaSource = await getMediaInfoApi(api).getPostedPlaybackInfo({
|
||||||
audioStreamIndex: currentAudioTrack,
|
audioStreamIndex: currentAudioTrack,
|
||||||
subtitleStreamIndex:
|
subtitleStreamIndex:
|
||||||
@@ -216,19 +211,11 @@ const PlayButton = ({
|
|||||||
DeviceProfile: playbackProfile,
|
DeviceProfile: playbackProfile,
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
try {
|
|
||||||
introInfo = (
|
introInfo = (
|
||||||
await axiosClient.get(
|
await getMediaSegmentsApi(api).getItemSegments({
|
||||||
`${api.basePath}/Episode/${result.data.Items?.[0]?.Id}/IntroSkipperSegments`,
|
itemId: result.data.Items?.[0].Id,
|
||||||
{
|
})
|
||||||
headers: {
|
|
||||||
Authorization: `MediaBrowser Token=${api.accessToken}`,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
)
|
|
||||||
)?.data;
|
)?.data;
|
||||||
} catch (error) {
|
|
||||||
console.error(error);
|
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case BaseItemKind.Playlist:
|
case BaseItemKind.Playlist:
|
||||||
@@ -323,6 +310,22 @@ const PlayButton = ({
|
|||||||
onSuccess: (result: PlayResult | null) => {
|
onSuccess: (result: PlayResult | null) => {
|
||||||
if (!api) {
|
if (!api) {
|
||||||
console.error("API is not available");
|
console.error("API is not available");
|
||||||
|
enqueueSnackbar("API is not available", { variant: "error" });
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (!userId) {
|
||||||
|
console.error("User ID is not available");
|
||||||
|
enqueueSnackbar("User ID is not available", { variant: "error" });
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (!result?.item?.Items?.length) {
|
||||||
|
console.error("No items found");
|
||||||
|
enqueueSnackbar("No items found", { variant: "error" });
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (!result?.item?.Items?.[0].Id) {
|
||||||
|
console.error("No item ID found");
|
||||||
|
enqueueSnackbar("No item ID found", { variant: "error" });
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (trackIndex) {
|
if (trackIndex) {
|
||||||
@@ -346,6 +349,11 @@ const PlayButton = ({
|
|||||||
navigate({ to: "/player/photos" });
|
navigate({ to: "/player/photos" });
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
if (!result?.mediaSource) {
|
||||||
|
enqueueSnackbar("No media source found", { variant: "error" });
|
||||||
|
console.error("No media source found");
|
||||||
|
return;
|
||||||
|
}
|
||||||
const episodeIndex = item.IndexNumber ? item.IndexNumber - 1 : 0;
|
const episodeIndex = item.IndexNumber ? item.IndexNumber - 1 : 0;
|
||||||
|
|
||||||
// Creates a queue containing all Episodes for a particular season(series) or collection of movies
|
// Creates a queue containing all Episodes for a particular season(series) or collection of movies
|
||||||
@@ -362,10 +370,11 @@ const PlayButton = ({
|
|||||||
// Subtitle
|
// Subtitle
|
||||||
const subtitle = getSubtitle(
|
const subtitle = getSubtitle(
|
||||||
currentSubTrack,
|
currentSubTrack,
|
||||||
result?.mediaSource.MediaSources?.[0].MediaStreams,
|
result?.mediaSource?.MediaSources?.[0].MediaStreams,
|
||||||
);
|
);
|
||||||
// URL generation
|
// URL generation
|
||||||
const urlOptions: URLSearchParams = {
|
const urlOptions: URLSearchParams = {
|
||||||
|
//@ts-ignore
|
||||||
Static: true,
|
Static: true,
|
||||||
tag: result?.mediaSource.MediaSources?.[0].ETag,
|
tag: result?.mediaSource.MediaSources?.[0].ETag,
|
||||||
mediaSourceId: result?.mediaSource.MediaSources?.[0].Id,
|
mediaSourceId: result?.mediaSource.MediaSources?.[0].Id,
|
||||||
@@ -388,14 +397,14 @@ const PlayButton = ({
|
|||||||
playItem(
|
playItem(
|
||||||
itemName,
|
itemName,
|
||||||
episodeTitle,
|
episodeTitle,
|
||||||
currentVideoTrack,
|
currentVideoTrack ?? 0,
|
||||||
currentAudioTrack,
|
currentAudioTrack ?? 0,
|
||||||
result?.mediaSource?.MediaSources?.[0].Container ?? "mkv",
|
result?.mediaSource?.MediaSources?.[0].Container ?? "mkv",
|
||||||
playbackUrl,
|
playbackUrl,
|
||||||
userId,
|
userId ?? "",
|
||||||
item.UserData?.PlaybackPositionTicks,
|
item.UserData?.PlaybackPositionTicks,
|
||||||
item.RunTimeTicks,
|
item.RunTimeTicks,
|
||||||
playItemValue,
|
playItemValue ?? item,
|
||||||
queue,
|
queue,
|
||||||
episodeIndex,
|
episodeIndex,
|
||||||
result?.mediaSource?.MediaSources?.[0]?.Id,
|
result?.mediaSource?.MediaSources?.[0]?.Id,
|
||||||
@@ -484,14 +493,14 @@ const PlayButton = ({
|
|||||||
: "Watch Now"}
|
: "Watch Now"}
|
||||||
<MemoizedLinearProgress
|
<MemoizedLinearProgress
|
||||||
value={
|
value={
|
||||||
100 > itemUserData?.PlayedPercentage &&
|
100 > (itemUserData?.PlayedPercentage ?? 100) &&
|
||||||
itemUserData?.PlayedPercentage > 0
|
(itemUserData?.PlayedPercentage ?? 0) > 0
|
||||||
? itemUserData?.PlayedPercentage
|
? itemUserData?.PlayedPercentage
|
||||||
: 0
|
: 0
|
||||||
}
|
}
|
||||||
/>
|
/>
|
||||||
</Button>
|
</Button>
|
||||||
{itemUserData?.PlaybackPositionTicks > 0 && (
|
{(itemUserData?.PlaybackPositionTicks ?? 0) > 0 && (
|
||||||
<Typography
|
<Typography
|
||||||
sx={{
|
sx={{
|
||||||
opacity: 0.8,
|
opacity: 0.8,
|
||||||
@@ -503,7 +512,7 @@ const PlayButton = ({
|
|||||||
variant="caption"
|
variant="caption"
|
||||||
>
|
>
|
||||||
{getRuntimeCompact(
|
{getRuntimeCompact(
|
||||||
item.RunTimeTicks - itemUserData.PlaybackPositionTicks,
|
item.RunTimeTicks ?? 0 - (itemUserData?.PlaybackPositionTicks ?? 0),
|
||||||
)}{" "}
|
)}{" "}
|
||||||
left
|
left
|
||||||
</Typography>
|
</Typography>
|
||||||
|
|||||||
@@ -141,7 +141,7 @@ function VideoPlayer() {
|
|||||||
episodeTitle,
|
episodeTitle,
|
||||||
mediaSource,
|
mediaSource,
|
||||||
playsessionId,
|
playsessionId,
|
||||||
introInfo,
|
mediaSegments,
|
||||||
] = usePlaybackStore((state) => [
|
] = usePlaybackStore((state) => [
|
||||||
state.playbackStream,
|
state.playbackStream,
|
||||||
state.item,
|
state.item,
|
||||||
@@ -153,6 +153,15 @@ function VideoPlayer() {
|
|||||||
state.playsessionId,
|
state.playsessionId,
|
||||||
state.intro,
|
state.intro,
|
||||||
]);
|
]);
|
||||||
|
|
||||||
|
const introInfo = mediaSegments?.Items?.find(
|
||||||
|
(segment) => segment.Type === "Intro",
|
||||||
|
);
|
||||||
|
|
||||||
|
const creditInfo = mediaSegments?.Items?.find(
|
||||||
|
(segment) => segment.Type === "Outro",
|
||||||
|
);
|
||||||
|
|
||||||
const [currentQueueItemIndex, queue] = useQueue((s) => [
|
const [currentQueueItemIndex, queue] = useQueue((s) => [
|
||||||
s.currentItemIndex,
|
s.currentItemIndex,
|
||||||
s.tracks,
|
s.tracks,
|
||||||
@@ -389,22 +398,23 @@ function VideoPlayer() {
|
|||||||
|
|
||||||
const showSkipIntroButton = useMemo(() => {
|
const showSkipIntroButton = useMemo(() => {
|
||||||
if (
|
if (
|
||||||
ticksToSec(progress) >=
|
progress >= (introInfo?.StartTicks ?? 0) &&
|
||||||
(introInfo?.Introduction?.ShowSkipPromptAt ?? 0) &&
|
progress < (introInfo?.EndTicks ?? 0)
|
||||||
ticksToSec(progress) < (introInfo?.Introduction?.HideSkipPromptAt ?? 0)
|
) {
|
||||||
)
|
console.log("showing skip intro button");
|
||||||
return true;
|
return true;
|
||||||
|
}
|
||||||
return false;
|
return false;
|
||||||
}, [progress]);
|
}, [progress]);
|
||||||
|
|
||||||
const showUpNextCard = useMemo(() => {
|
const showUpNextCard = useMemo(() => {
|
||||||
if (queue[currentQueueItemIndex]?.Id === queue[queue.length - 1]?.Id) {
|
if (queue?.[currentQueueItemIndex]?.Id === queue?.[queue.length - 1]?.Id) {
|
||||||
return false; // Check if the current playing episode is last episode in queue
|
return false; // Check if the current playing episode is last episode in queue
|
||||||
}
|
}
|
||||||
if (introInfo?.Credits) {
|
if (creditInfo) {
|
||||||
if (
|
if (
|
||||||
ticksToSec(progress) >= introInfo?.Credits.ShowSkipPromptAt &&
|
progress >= (creditInfo.StartTicks ?? progress + 1) &&
|
||||||
ticksToSec(progress) < introInfo?.Credits.HideSkipPromptAt
|
progress < (creditInfo.EndTicks ?? 0)
|
||||||
)
|
)
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
@@ -419,7 +429,7 @@ function VideoPlayer() {
|
|||||||
|
|
||||||
const handleSkipIntro = useCallback(() => {
|
const handleSkipIntro = useCallback(() => {
|
||||||
player.current?.seekTo(
|
player.current?.seekTo(
|
||||||
introInfo?.Introduction.IntroEnd ?? player.current?.getCurrentTime(),
|
ticksToSec(introInfo?.EndTicks) ?? player.current?.getCurrentTime(),
|
||||||
);
|
);
|
||||||
}, [item?.Id]);
|
}, [item?.Id]);
|
||||||
|
|
||||||
@@ -466,8 +476,7 @@ function VideoPlayer() {
|
|||||||
let trickplayResolution: TrickplayInfo | undefined;
|
let trickplayResolution: TrickplayInfo | undefined;
|
||||||
const trickplayResolutions = mediaSource.id
|
const trickplayResolutions = mediaSource.id
|
||||||
? item?.Trickplay?.[mediaSource.id]
|
? item?.Trickplay?.[mediaSource.id]
|
||||||
: null;
|
: null;
|
||||||
console.log(item);
|
|
||||||
if (trickplayResolutions) {
|
if (trickplayResolutions) {
|
||||||
let bestWidth: number | undefined;
|
let bestWidth: number | undefined;
|
||||||
const maxWidth = window.screen.width * window.devicePixelRatio * 0.2;
|
const maxWidth = window.screen.width * window.devicePixelRatio * 0.2;
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
import type { Api } from "@jellyfin/sdk";
|
import type { Api } from "@jellyfin/sdk";
|
||||||
import type {
|
import type {
|
||||||
BaseItemDto,
|
BaseItemDto,
|
||||||
|
MediaSegmentDtoQueryResult,
|
||||||
MediaStream,
|
MediaStream,
|
||||||
} from "@jellyfin/sdk/lib/generated-client";
|
} from "@jellyfin/sdk/lib/generated-client";
|
||||||
import { getMediaInfoApi } from "@jellyfin/sdk/lib/utils/api/media-info-api";
|
import { getMediaInfoApi } from "@jellyfin/sdk/lib/utils/api/media-info-api";
|
||||||
@@ -32,7 +33,7 @@ type PlaybackStore = {
|
|||||||
itemDuration: number;
|
itemDuration: number;
|
||||||
item: BaseItemDto | null;
|
item: BaseItemDto | null;
|
||||||
playsessionId: string | undefined | null;
|
playsessionId: string | undefined | null;
|
||||||
intro: IntroMediaInfo | undefined;
|
intro: MediaSegmentDtoQueryResult | undefined;
|
||||||
};
|
};
|
||||||
|
|
||||||
export const usePlaybackStore = createWithEqualityFn<PlaybackStore>(
|
export const usePlaybackStore = createWithEqualityFn<PlaybackStore>(
|
||||||
@@ -79,8 +80,8 @@ export const playItem = (
|
|||||||
queueItemIndex: number,
|
queueItemIndex: number,
|
||||||
mediaSourceId: string | undefined | null,
|
mediaSourceId: string | undefined | null,
|
||||||
playsessionId: string | undefined | null,
|
playsessionId: string | undefined | null,
|
||||||
subtitle: subtitlePlaybackInfo,
|
subtitle: subtitlePlaybackInfo | undefined,
|
||||||
intro: IntroMediaInfo | undefined,
|
intro: MediaSegmentDtoQueryResult | undefined,
|
||||||
) => {
|
) => {
|
||||||
console.log({
|
console.log({
|
||||||
itemName,
|
itemName,
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
import type {
|
import type {
|
||||||
BaseItemDtoQueryResult,
|
BaseItemDtoQueryResult,
|
||||||
|
MediaSegmentDtoQueryResult,
|
||||||
PlaybackInfoResponse,
|
PlaybackInfoResponse,
|
||||||
} from "@jellyfin/sdk/lib/generated-client";
|
} from "@jellyfin/sdk/lib/generated-client";
|
||||||
import type IntroMediaInfo from "./introMediaInfo";
|
import type IntroMediaInfo from "./introMediaInfo";
|
||||||
@@ -7,7 +8,7 @@ import type IntroMediaInfo from "./introMediaInfo";
|
|||||||
interface PlayResult {
|
interface PlayResult {
|
||||||
item: BaseItemDtoQueryResult | undefined;
|
item: BaseItemDtoQueryResult | undefined;
|
||||||
mediaSource: PlaybackInfoResponse | undefined;
|
mediaSource: PlaybackInfoResponse | undefined;
|
||||||
introInfo: IntroMediaInfo | undefined;
|
introInfo: MediaSegmentDtoQueryResult | undefined;
|
||||||
}
|
}
|
||||||
|
|
||||||
export default PlayResult;
|
export default PlayResult;
|
||||||
Reference in new issue
Block a user