add external chapter file scanning (#2317)

* add external chapter file scanning

Support Matroska chapter xml files next to media file with extension .xml or .chapters

* only update chapters in db

---------

Co-authored-by: Jeff Slutter <MrMustard@gmail.com>
Co-authored-by: Jason Dove <1695733+jasongdove@users.noreply.github.com>
This commit is contained in:
midnite8177
2025-08-14 03:34:47 +00:00
committed by GitHub
co-authored by Jeff Slutter Jason Dove
parent 62e140ec98
commit f626954eb7
21 changed files with 510 additions and 6 deletions
@@ -537,6 +537,47 @@ public class MetadataRepository : IMetadataRepository
return await UpdateSubtitles(dbContext, metadata, subtitles);
}
public async Task<bool> UpdateChapters(MediaVersion version, List<MediaChapter> chapters)
{
await using TvContext dbContext = await _dbContextFactory.CreateDbContextAsync();
var maybeExisting = await dbContext.MediaVersions
.Include(mv => mv.Chapters)
.SelectOneAsync(mv => mv.Id, mv => mv.Id == version.Id);
foreach (MediaVersion existing in maybeExisting)
{
var chaptersToAdd = chapters
.Filter(s => existing.Chapters.All(es => es.ChapterId != s.ChapterId))
.ToList();
var chaptersToRemove = existing.Chapters
.Filter(es => chapters.All(s => s.ChapterId != es.ChapterId))
.ToList();
var chaptersToUpdate = chapters.Except(chaptersToAdd).ToList();
// add
existing.Chapters.AddRange(chaptersToAdd);
// remove
existing.Chapters.RemoveAll(chaptersToRemove.Contains);
// update
foreach (MediaChapter incomingChapter in chaptersToUpdate)
{
MediaChapter existingChapter = existing.Chapters
.First(s => s.ChapterId == incomingChapter.ChapterId);
existingChapter.StartTime = incomingChapter.StartTime;
existingChapter.EndTime = incomingChapter.EndTime;
existingChapter.Title = incomingChapter.Title;
}
return await dbContext.SaveChangesAsync() > 0;
}
return false;
}
public async Task<bool> RemoveGenre(Genre genre)
{
await using TvContext dbContext = await _dbContextFactory.CreateDbContextAsync();