mirror of https://github.com/ErsatzTV/ErsatzTV.git
10 changed files with 475 additions and 315 deletions
@ -0,0 +1,3 @@ |
|||||||
|
namespace ErsatzTV.Application.Troubleshooting; |
||||||
|
|
||||||
|
public record ArchiveMediaSample(int MediaItemId) : IRequest<Option<string>>; |
||||||
@ -0,0 +1,157 @@ |
|||||||
|
using System.IO.Abstractions; |
||||||
|
using System.IO.Compression; |
||||||
|
using CliWrap; |
||||||
|
using CliWrap.Buffered; |
||||||
|
using ErsatzTV.Core; |
||||||
|
using ErsatzTV.Core.Domain; |
||||||
|
using ErsatzTV.Core.Interfaces.Emby; |
||||||
|
using ErsatzTV.Core.Interfaces.Jellyfin; |
||||||
|
using ErsatzTV.Core.Interfaces.Plex; |
||||||
|
using ErsatzTV.Infrastructure.Data; |
||||||
|
using Microsoft.EntityFrameworkCore; |
||||||
|
using Microsoft.Extensions.Logging; |
||||||
|
|
||||||
|
namespace ErsatzTV.Application.Troubleshooting; |
||||||
|
|
||||||
|
public class ArchiveMediaSampleHandler( |
||||||
|
IDbContextFactory<TvContext> dbContextFactory, |
||||||
|
IPlexPathReplacementService plexPathReplacementService, |
||||||
|
IJellyfinPathReplacementService jellyfinPathReplacementService, |
||||||
|
IEmbyPathReplacementService embyPathReplacementService, |
||||||
|
IFileSystem fileSystem, |
||||||
|
ILogger<ArchiveMediaSampleHandler> logger) |
||||||
|
: TroubleshootingHandlerBase( |
||||||
|
plexPathReplacementService, |
||||||
|
jellyfinPathReplacementService, |
||||||
|
embyPathReplacementService, |
||||||
|
fileSystem), IRequestHandler<ArchiveMediaSample, Option<string>> |
||||||
|
{ |
||||||
|
public async Task<Option<string>> Handle(ArchiveMediaSample request, CancellationToken cancellationToken) |
||||||
|
{ |
||||||
|
await using TvContext dbContext = await dbContextFactory.CreateDbContextAsync(cancellationToken); |
||||||
|
Validation<BaseError, Tuple<MediaItem, string>> validation = await Validate( |
||||||
|
dbContext, |
||||||
|
request, |
||||||
|
cancellationToken); |
||||||
|
|
||||||
|
foreach ((MediaItem mediaItem, string ffmpegPath) in validation.SuccessToSeq()) |
||||||
|
{ |
||||||
|
Option<string> maybeMediaSample = await GetMediaSample( |
||||||
|
request, |
||||||
|
dbContext, |
||||||
|
mediaItem, |
||||||
|
ffmpegPath, |
||||||
|
cancellationToken); |
||||||
|
|
||||||
|
foreach (string mediaSample in maybeMediaSample) |
||||||
|
{ |
||||||
|
return await GetArchive(request, mediaSample, cancellationToken); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
return Option<string>.None; |
||||||
|
} |
||||||
|
|
||||||
|
private async Task<Option<string>> GetArchive( |
||||||
|
ArchiveMediaSample request, |
||||||
|
string mediaSample, |
||||||
|
CancellationToken cancellationToken) |
||||||
|
{ |
||||||
|
try |
||||||
|
{ |
||||||
|
string tempFile = Path.GetTempFileName(); |
||||||
|
await using ZipArchive zipArchive = await ZipFile.OpenAsync( |
||||||
|
tempFile, |
||||||
|
ZipArchiveMode.Update, |
||||||
|
cancellationToken); |
||||||
|
|
||||||
|
string fileName = Path.GetFileName(mediaSample); |
||||||
|
await zipArchive.CreateEntryFromFileAsync(mediaSample, fileName, cancellationToken); |
||||||
|
|
||||||
|
return tempFile; |
||||||
|
} |
||||||
|
catch (Exception ex) |
||||||
|
{ |
||||||
|
logger.LogWarning(ex, "Failed to archive media sample for media item {MediaItemId}", request.MediaItemId); |
||||||
|
} |
||||||
|
|
||||||
|
return Option<string>.None; |
||||||
|
} |
||||||
|
|
||||||
|
private async Task<Option<string>> GetMediaSample( |
||||||
|
ArchiveMediaSample request, |
||||||
|
TvContext dbContext, |
||||||
|
MediaItem mediaItem, |
||||||
|
string ffmpegPath, |
||||||
|
CancellationToken cancellationToken) |
||||||
|
{ |
||||||
|
try |
||||||
|
{ |
||||||
|
string mediaItemPath = await GetMediaItemPath(dbContext, mediaItem, cancellationToken); |
||||||
|
|
||||||
|
string extension = Path.GetExtension(mediaItemPath); |
||||||
|
if (string.IsNullOrWhiteSpace(extension)) |
||||||
|
{ |
||||||
|
// this can help with remote servers (e.g. mediaItemPath is http://localhost/whatever)
|
||||||
|
extension = Path.GetExtension(await GetLocalPath(mediaItem, cancellationToken)); |
||||||
|
|
||||||
|
if (string.IsNullOrWhiteSpace(extension)) |
||||||
|
{ |
||||||
|
// fall back to mkv when extension is otherwise unknown
|
||||||
|
extension = "mkv"; |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
string tempPath = Path.GetTempPath(); |
||||||
|
string fileName = Path.ChangeExtension(Guid.NewGuid().ToString(), extension); |
||||||
|
string outputPath = Path.Combine(tempPath, fileName); |
||||||
|
|
||||||
|
List<string> arguments = |
||||||
|
[ |
||||||
|
"-nostdin", |
||||||
|
"-i", mediaItemPath, |
||||||
|
"-t", "30", |
||||||
|
"-map", "0", |
||||||
|
"-c", "copy", |
||||||
|
outputPath |
||||||
|
]; |
||||||
|
|
||||||
|
using var cts = new CancellationTokenSource(TimeSpan.FromMinutes(2)); |
||||||
|
using var linkedTokenSource = |
||||||
|
CancellationTokenSource.CreateLinkedTokenSource(cts.Token, cancellationToken); |
||||||
|
|
||||||
|
logger.LogDebug("media sample arguments {Arguments}", arguments); |
||||||
|
|
||||||
|
BufferedCommandResult result = await Cli.Wrap(ffmpegPath) |
||||||
|
.WithArguments(arguments) |
||||||
|
.WithWorkingDirectory(FileSystemLayout.FontsCacheFolder) |
||||||
|
.WithStandardErrorPipe(PipeTarget.Null) |
||||||
|
.WithValidation(CommandResultValidation.None) |
||||||
|
.ExecuteBufferedAsync(linkedTokenSource.Token); |
||||||
|
|
||||||
|
if (result.IsSuccess) |
||||||
|
{ |
||||||
|
return outputPath; |
||||||
|
} |
||||||
|
|
||||||
|
logger.LogWarning( |
||||||
|
"Failed to extract media sample for media item {MediaItemId} - exit code {ExitCode}", |
||||||
|
request.MediaItemId, |
||||||
|
result.ExitCode); |
||||||
|
} |
||||||
|
catch (Exception ex) |
||||||
|
{ |
||||||
|
logger.LogWarning(ex, "Failed to extract media sample for media item {MediaItemId}", request.MediaItemId); |
||||||
|
} |
||||||
|
|
||||||
|
return Option<string>.None; |
||||||
|
} |
||||||
|
|
||||||
|
private static async Task<Validation<BaseError, Tuple<MediaItem, string>>> Validate( |
||||||
|
TvContext dbContext, |
||||||
|
ArchiveMediaSample request, |
||||||
|
CancellationToken cancellationToken) => |
||||||
|
(await MediaItemMustExist(dbContext, request.MediaItemId, cancellationToken), |
||||||
|
await FFmpegPathMustExist(dbContext, cancellationToken)) |
||||||
|
.Apply((mediaItem, ffmpegPath) => Tuple(mediaItem, ffmpegPath)); |
||||||
|
} |
||||||
@ -0,0 +1,166 @@ |
|||||||
|
using System.IO.Abstractions; |
||||||
|
using Dapper; |
||||||
|
using ErsatzTV.Core; |
||||||
|
using ErsatzTV.Core.Domain; |
||||||
|
using ErsatzTV.Core.Errors; |
||||||
|
using ErsatzTV.Core.Extensions; |
||||||
|
using ErsatzTV.Core.Interfaces.Emby; |
||||||
|
using ErsatzTV.Core.Interfaces.Jellyfin; |
||||||
|
using ErsatzTV.Core.Interfaces.Plex; |
||||||
|
using ErsatzTV.Infrastructure.Data; |
||||||
|
using ErsatzTV.Infrastructure.Extensions; |
||||||
|
using Microsoft.EntityFrameworkCore; |
||||||
|
|
||||||
|
namespace ErsatzTV.Application.Troubleshooting; |
||||||
|
|
||||||
|
public abstract class TroubleshootingHandlerBase( |
||||||
|
IPlexPathReplacementService plexPathReplacementService, |
||||||
|
IJellyfinPathReplacementService jellyfinPathReplacementService, |
||||||
|
IEmbyPathReplacementService embyPathReplacementService, |
||||||
|
IFileSystem fileSystem) |
||||||
|
{ |
||||||
|
protected static async Task<Validation<BaseError, MediaItem>> MediaItemMustExist( |
||||||
|
TvContext dbContext, |
||||||
|
int mediaItemId, |
||||||
|
CancellationToken cancellationToken) => |
||||||
|
await dbContext.MediaItems |
||||||
|
.AsNoTracking() |
||||||
|
.Include(mi => (mi as Episode).EpisodeMetadata) |
||||||
|
.ThenInclude(em => em.Subtitles) |
||||||
|
.Include(mi => (mi as Episode).MediaVersions) |
||||||
|
.ThenInclude(mv => mv.MediaFiles) |
||||||
|
.Include(mi => (mi as Episode).MediaVersions) |
||||||
|
.ThenInclude(mv => mv.Streams) |
||||||
|
.Include(mi => (mi as Episode).Season) |
||||||
|
.ThenInclude(s => s.Show) |
||||||
|
.ThenInclude(s => s.ShowMetadata) |
||||||
|
.Include(mi => (mi as Movie).MovieMetadata) |
||||||
|
.ThenInclude(mm => mm.Subtitles) |
||||||
|
.Include(mi => (mi as Movie).MediaVersions) |
||||||
|
.ThenInclude(mv => mv.MediaFiles) |
||||||
|
.Include(mi => (mi as Movie).MediaVersions) |
||||||
|
.ThenInclude(mv => mv.Streams) |
||||||
|
.Include(mi => (mi as MusicVideo).MusicVideoMetadata) |
||||||
|
.ThenInclude(mvm => mvm.Subtitles) |
||||||
|
.Include(mi => (mi as MusicVideo).MusicVideoMetadata) |
||||||
|
.ThenInclude(mvm => mvm.Artists) |
||||||
|
.Include(mi => (mi as MusicVideo).MusicVideoMetadata) |
||||||
|
.ThenInclude(mvm => mvm.Studios) |
||||||
|
.Include(mi => (mi as MusicVideo).MusicVideoMetadata) |
||||||
|
.ThenInclude(mvm => mvm.Directors) |
||||||
|
.Include(mi => (mi as MusicVideo).MediaVersions) |
||||||
|
.ThenInclude(mv => mv.MediaFiles) |
||||||
|
.Include(mi => (mi as MusicVideo).MediaVersions) |
||||||
|
.ThenInclude(mv => mv.Streams) |
||||||
|
.Include(mi => (mi as MusicVideo).Artist) |
||||||
|
.ThenInclude(mv => mv.ArtistMetadata) |
||||||
|
.Include(mi => (mi as OtherVideo).OtherVideoMetadata) |
||||||
|
.ThenInclude(ovm => ovm.Subtitles) |
||||||
|
.Include(mi => (mi as OtherVideo).MediaVersions) |
||||||
|
.ThenInclude(ov => ov.MediaFiles) |
||||||
|
.Include(mi => (mi as OtherVideo).MediaVersions) |
||||||
|
.ThenInclude(ov => ov.Streams) |
||||||
|
.Include(mi => (mi as Song).MediaVersions) |
||||||
|
.ThenInclude(mv => mv.MediaFiles) |
||||||
|
.Include(mi => (mi as Song).MediaVersions) |
||||||
|
.ThenInclude(mv => mv.Streams) |
||||||
|
.Include(mi => (mi as Song).SongMetadata) |
||||||
|
.ThenInclude(sm => sm.Artwork) |
||||||
|
.Include(mi => (mi as Image).MediaVersions) |
||||||
|
.ThenInclude(mv => mv.MediaFiles) |
||||||
|
.Include(mi => (mi as Image).MediaVersions) |
||||||
|
.ThenInclude(mv => mv.Streams) |
||||||
|
.Include(mi => (mi as Image).ImageMetadata) |
||||||
|
.Include(mi => (mi as RemoteStream).MediaVersions) |
||||||
|
.ThenInclude(mv => mv.MediaFiles) |
||||||
|
.Include(mi => (mi as RemoteStream).MediaVersions) |
||||||
|
.ThenInclude(mv => mv.Streams) |
||||||
|
.Include(mi => (mi as RemoteStream).RemoteStreamMetadata) |
||||||
|
.SingleOrDefaultAsync(mi => mi.Id == mediaItemId, cancellationToken) |
||||||
|
.Map(Optional) |
||||||
|
.Map(o => o.ToValidation<BaseError>(new UnableToLocatePlayoutItem())); |
||||||
|
|
||||||
|
protected static Task<Validation<BaseError, string>> FFmpegPathMustExist( |
||||||
|
TvContext dbContext, |
||||||
|
CancellationToken cancellationToken) => |
||||||
|
dbContext.ConfigElements.GetValue<string>(ConfigElementKey.FFmpegPath, cancellationToken) |
||||||
|
.FilterT(File.Exists) |
||||||
|
.Map(maybePath => maybePath.ToValidation<BaseError>("FFmpeg path does not exist on filesystem")); |
||||||
|
|
||||||
|
protected Task<string> GetLocalPath(MediaItem mediaItem, CancellationToken cancellationToken) => |
||||||
|
mediaItem.GetLocalPath( |
||||||
|
plexPathReplacementService, |
||||||
|
jellyfinPathReplacementService, |
||||||
|
embyPathReplacementService, |
||||||
|
cancellationToken); |
||||||
|
|
||||||
|
protected async Task<string> GetMediaItemPath( |
||||||
|
TvContext dbContext, |
||||||
|
MediaItem mediaItem, |
||||||
|
CancellationToken cancellationToken) |
||||||
|
{ |
||||||
|
string path = await GetLocalPath(mediaItem, cancellationToken); |
||||||
|
|
||||||
|
// check filesystem first
|
||||||
|
if (fileSystem.File.Exists(path)) |
||||||
|
{ |
||||||
|
if (mediaItem is RemoteStream remoteStream) |
||||||
|
{ |
||||||
|
path = !string.IsNullOrWhiteSpace(remoteStream.Url) |
||||||
|
? remoteStream.Url |
||||||
|
: $"http://localhost:{Settings.StreamingPort}/ffmpeg/remote-stream/{remoteStream.Id}"; |
||||||
|
} |
||||||
|
|
||||||
|
return path; |
||||||
|
} |
||||||
|
|
||||||
|
// attempt to remotely stream plex
|
||||||
|
MediaFile file = mediaItem.GetHeadVersion().MediaFiles.Head(); |
||||||
|
switch (file) |
||||||
|
{ |
||||||
|
case PlexMediaFile pmf: |
||||||
|
Option<int> maybeId = await dbContext.Connection.QuerySingleOrDefaultAsync<int>( |
||||||
|
@"SELECT PMS.Id FROM PlexMediaSource PMS
|
||||||
|
INNER JOIN Library L on PMS.Id = L.MediaSourceId |
||||||
|
INNER JOIN LibraryPath LP on L.Id = LP.LibraryId |
||||||
|
WHERE LP.Id = @LibraryPathId",
|
||||||
|
new { mediaItem.LibraryPathId }) |
||||||
|
.Map(Optional); |
||||||
|
|
||||||
|
foreach (int plexMediaSourceId in maybeId) |
||||||
|
{ |
||||||
|
return $"http://localhost:{Settings.StreamingPort}/media/plex/{plexMediaSourceId}/{pmf.Key}"; |
||||||
|
} |
||||||
|
|
||||||
|
break; |
||||||
|
} |
||||||
|
|
||||||
|
// attempt to remotely stream jellyfin
|
||||||
|
Option<string> jellyfinItemId = mediaItem switch |
||||||
|
{ |
||||||
|
JellyfinEpisode e => e.ItemId, |
||||||
|
JellyfinMovie m => m.ItemId, |
||||||
|
_ => None |
||||||
|
}; |
||||||
|
|
||||||
|
foreach (string itemId in jellyfinItemId) |
||||||
|
{ |
||||||
|
return $"http://localhost:{Settings.StreamingPort}/media/jellyfin/{itemId}"; |
||||||
|
} |
||||||
|
|
||||||
|
// attempt to remotely stream emby
|
||||||
|
Option<string> embyItemId = mediaItem switch |
||||||
|
{ |
||||||
|
EmbyEpisode e => e.ItemId, |
||||||
|
EmbyMovie m => m.ItemId, |
||||||
|
_ => None |
||||||
|
}; |
||||||
|
|
||||||
|
foreach (string itemId in embyItemId) |
||||||
|
{ |
||||||
|
return $"http://localhost:{Settings.StreamingPort}/media/emby/{itemId}"; |
||||||
|
} |
||||||
|
|
||||||
|
return null; |
||||||
|
} |
||||||
|
} |
||||||
Loading…
Reference in new issue