Skip to content

Commit

Permalink
Refactor file info, add codec & version (#918)
Browse files Browse the repository at this point in the history
* Make file search details UX better

* Refactor mediainfo hook, add codec & version to file info

* Fix mediainfo hook
  • Loading branch information
harshithmohan authored May 28, 2024
1 parent a584bd0 commit 5843fe8
Show file tree
Hide file tree
Showing 3 changed files with 172 additions and 143 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ const QuickSelectModal = ({ onClose, seriesId, show }: Props) => {
const fileSummaryQuery = useSeriesFileSummaryQuery(
seriesId,
{
groupBy: 'GroupName,FileSource,VideoResolution,AudioLanguages,SubtitleLanguages,VideoHasChapters',
groupBy:
'GroupName,FileSource,FileVersion,VideoCodecs,VideoResolution,AudioLanguages,SubtitleLanguages,VideoHasChapters',
includeEpisodeDetails: true,
},
show,
Expand Down Expand Up @@ -85,10 +86,14 @@ const QuickSelectModal = ({ onClose, seriesId, show }: Props) => {
)
</>
)}
&nbsp;-&nbsp;
{`v${group.FileVersion}`}
</div>
<div className="flex flex-wrap text-sm opacity-65">
{group.FileSource}
&nbsp;|&nbsp;
{group.VideoCodecs?.toUpperCase()}
&nbsp;|&nbsp;
{group.VideoResolution}
{group.AudioLanguages && (
<>
Expand Down
145 changes: 64 additions & 81 deletions src/hooks/useMediaInfo.ts
Original file line number Diff line number Diff line change
@@ -1,99 +1,82 @@
import { useMemo } from 'react';
import { get, map, toNumber } from 'lodash';
import { map, toNumber } from 'lodash';

import type { FileType } from '@/core/types/api/file';
import type { FileInfo } from '@/core/types/models/file';

export const useVideoInfo = <T extends FileType>(file?: T) => {
const videoInfo: string[] = useMemo(() => {
if (!file) return [];

const info: string[] = [];
const VideoSource = get(file, 'AniDB.Source');
if (VideoSource) {
info.push(VideoSource);
}

const VideoBitDepth = file?.MediaInfo?.Video?.[0]?.BitDepth;
if (VideoBitDepth) {
info.push(`${VideoBitDepth}-bit`);
}

const VideoBitRate = file?.MediaInfo?.Video?.[0]?.BitRate;
if (VideoBitRate) {
info.push(`${Math.round(toNumber(VideoBitRate) / 1024)} kb/s`);
}

const VideoResolution = file?.MediaInfo?.Video?.[0]?.Resolution;
if (VideoResolution) {
info.push(VideoResolution);
}

const VideoWidth = file?.MediaInfo?.Video?.[0]?.Width;
const VideoHeight = file?.MediaInfo?.Video?.[0]?.Height;
if (VideoWidth && VideoHeight) {
info.push(`${VideoWidth}x${VideoHeight}`);
}

return info;
}, [file]);
return videoInfo;
};
const getVideoInfo = (file: FileType) => {
const videoInfo = file.MediaInfo?.Video?.[0];
if (!videoInfo) return [];

export const useAudioInfo = <T extends FileType>(file?: T) => {
const audioInfo: string[] = useMemo(() => {
if (!file) return [];
const info = [
videoInfo.Codec.Simplified.toUpperCase(),
videoInfo.Resolution,
];

const info: string[] = [];
const AudioFormat = file?.MediaInfo?.Audio?.[0]?.Format.Name;
const AudioLanguages = map(file?.MediaInfo?.Audio, item => item.LanguageCode).filter(item => !!item);
const VideoBitDepth = file.MediaInfo?.Video?.[0]?.BitDepth;
if (VideoBitDepth) {
info.push(`${VideoBitDepth}-bit`);
}

if (AudioFormat) {
info.push(AudioFormat);
}
if (videoInfo.BitRate) {
info.push(`${Math.round(toNumber(videoInfo.BitRate) / 1024)} kb/s`);
}

if (AudioLanguages && AudioLanguages.length > 0) {
info.push(`${AudioLanguages.length > 1 ? 'Multi Audio' : 'Audio'} (${AudioLanguages.join(',')})`);
}
if (videoInfo.Width && videoInfo.Height) {
info.push(`${videoInfo.Width}x${videoInfo.Height}`);
}

const SubtitleLanguages = map(file?.MediaInfo?.Subtitles, item => item.LanguageCode).filter(item => !!item);
if (SubtitleLanguages && SubtitleLanguages.length > 0) {
info.push(`${SubtitleLanguages.length > 1 ? 'Multi Subs' : 'Subs'} (${SubtitleLanguages.join(',')})`);
}
return info;
};

return info;
}, [file]);
return audioInfo;
const getAudioInfo = (file: FileType) => {
const info: string[] = [];

if (file.MediaInfo?.Audio?.[0]?.Format?.Name) {
info.push(file.MediaInfo.Audio[0].Format.Name);
}

const audioLanguages = map(file.MediaInfo?.Audio, item => item.LanguageCode).filter(item => !!item);
if (audioLanguages && audioLanguages.length > 0) {
info.push(`${audioLanguages.length > 1 ? 'Multi Audio' : 'Audio'} (${audioLanguages.join(', ')})`);
}

const subtitleLanguages = map(file.MediaInfo?.Subtitles, item => item.LanguageCode).filter(item => !!item);
if (subtitleLanguages && subtitleLanguages.length > 0) {
info.push(`${subtitleLanguages.length > 1 ? 'Multi Subs' : 'Subs'} (${subtitleLanguages.join(',')})`);
}

return info;
};

const useMediaInfo = <T extends FileType>(file?: T): FileInfo => {
const videoInfo = useVideoInfo(file);
const audioInfo = useAudioInfo(file);
const { fileName, folderPath } = useMemo(() => {
const absolutePath = file?.Locations?.[0]?.AbsolutePath ?? '??';
const useMediaInfo = (file: FileType): FileInfo =>
useMemo(() => {
const videoInfo = getVideoInfo(file);
const audioInfo = getAudioInfo(file);

const absolutePath = file.Locations?.[0]?.AbsolutePath ?? '??';
const fileName = absolutePath.split(/[/\\]+/).pop();
const folderPath = absolutePath.slice(0, absolutePath.replaceAll('\\', '/').lastIndexOf('/') + 1);

const groupInfo = [file.AniDB?.ReleaseGroup?.Name ?? 'Unknown'];
if (file.AniDB?.Source) groupInfo.push(file.AniDB.Source);
if (file.AniDB?.Version) groupInfo.push(`v${file.AniDB.Version}`);

return {
fileName: absolutePath.split(/[/\\]+/).pop(),
folderPath: absolutePath.slice(0, absolutePath.replaceAll('\\', '/').lastIndexOf('/') + 1),
Name: fileName ?? '',
Location: folderPath ?? '',
Size: file.Size ?? 0,
Group: groupInfo.join(' | '),
Hashes: {
ED2K: file.Hashes?.ED2K ?? '',
SHA1: file.Hashes?.SHA1 ?? '',
CRC32: file.Hashes?.CRC32 ?? '',
MD5: file.Hashes?.MD5 ?? '',
},
VideoInfo: videoInfo,
AudioInfo: audioInfo,
Chapters: file.AniDB?.Chaptered ?? false,
};
}, [file]);

return {
Name: fileName ?? '',
Location: folderPath ?? '',
Size: file?.Size ?? 0,
Group: `${file?.AniDB?.ReleaseGroup?.Name ?? 'Unknown'}${
file?.AniDB?.Version ? (` | v${file?.AniDB?.Version}`) : ''
}`,
Hashes: {
ED2K: file?.Hashes?.ED2K ?? '',
SHA1: file?.Hashes?.SHA1 ?? '',
CRC32: file?.Hashes?.CRC32 ?? '',
MD5: file?.Hashes?.MD5 ?? '',
},
VideoInfo: videoInfo,
AudioInfo: audioInfo,
Chapters: file?.AniDB?.Chaptered ?? false,
};
};

export default useMediaInfo;
Loading

0 comments on commit 5843fe8

Please sign in to comment.