public async Task<bool> RefreshChapterImages(ChapterImageRefreshOptions options, CancellationToken cancellationToken) { var extractImages = options.ExtractImages; var video = options.Video; var chapters = options.Chapters; var saveChapters = options.SaveChapters; if (!IsEligibleForChapterImageExtraction(video)) { extractImages = false; } var success = true; var changesMade = false; var runtimeTicks = video.RunTimeTicks ?? 0; var currentImages = GetSavedChapterImages(video); foreach (var chapter in chapters) { if (chapter.StartPositionTicks >= runtimeTicks) { _logger.Info("Stopping chapter extraction for {0} because a chapter was found with a position greater than the runtime.", video.Name); break; } var path = GetChapterImagePath(video, chapter.StartPositionTicks); if (!currentImages.Contains(path, StringComparer.OrdinalIgnoreCase)) { if (extractImages) { if (video.VideoType == VideoType.HdDvd || video.VideoType == VideoType.Iso) { continue; } // Add some time for the first chapter to make sure we don't end up with a black image var time = chapter.StartPositionTicks == 0 ? TimeSpan.FromTicks(Math.Min(FirstChapterTicks, video.RunTimeTicks ?? 0)) : TimeSpan.FromTicks(chapter.StartPositionTicks); InputType type; var inputPath = MediaEncoderHelpers.GetInputArgument(video.Path, false, video.VideoType, video.IsoType, null, video.PlayableStreamFileNames, out type); try { Directory.CreateDirectory(Path.GetDirectoryName(path)); using (var stream = await _encoder.ExtractVideoImage(inputPath, type, video.Video3DFormat, time, cancellationToken).ConfigureAwait(false)) { using (var fileStream = _fileSystem.GetFileStream(path, FileMode.Create, FileAccess.Write, FileShare.Read, true)) { await stream.CopyToAsync(fileStream).ConfigureAwait(false); } } chapter.ImagePath = path; changesMade = true; } catch { success = false; break; } } else if (!string.IsNullOrEmpty(chapter.ImagePath)) { chapter.ImagePath = null; changesMade = true; } } else if (!string.Equals(path, chapter.ImagePath, StringComparison.OrdinalIgnoreCase)) { chapter.ImagePath = path; changesMade = true; } } if (saveChapters && changesMade) { await _itemRepo.SaveChapters(video.Id, chapters, cancellationToken).ConfigureAwait(false); } DeleteDeadImages(currentImages, chapters); return success; }
public async Task<bool> RefreshChapterImages(ChapterImageRefreshOptions options, CancellationToken cancellationToken) { var extractImages = options.ExtractImages; var video = options.Video; var chapters = options.Chapters; var saveChapters = options.SaveChapters; if (!IsEligibleForChapterImageExtraction(video)) { extractImages = false; } var success = true; var changesMade = false; var runtimeTicks = video.RunTimeTicks ?? 0; var currentImages = GetSavedChapterImages(video); foreach (var chapter in chapters) { if (chapter.StartPositionTicks >= runtimeTicks) { _logger.Info("Stopping chapter extraction for {0} because a chapter was found with a position greater than the runtime.", video.Name); break; } var path = GetChapterImagePath(video, chapter.StartPositionTicks); if (!currentImages.Contains(path, StringComparer.OrdinalIgnoreCase)) { if (extractImages) { if (video.VideoType == VideoType.HdDvd || video.VideoType == VideoType.Iso || video.VideoType == VideoType.BluRay) { continue; } // Add some time for the first chapter to make sure we don't end up with a black image var time = chapter.StartPositionTicks == 0 ? TimeSpan.FromTicks(Math.Min(FirstChapterTicks, video.RunTimeTicks ?? 0)) : TimeSpan.FromTicks(chapter.StartPositionTicks); var protocol = MediaProtocol.File; var inputPath = MediaEncoderHelpers.GetInputArgument(_fileSystem, video.Path, protocol, null, video.PlayableStreamFileNames); try { _fileSystem.CreateDirectory(Path.GetDirectoryName(path)); var tempFile = await _encoder.ExtractVideoImage(inputPath, protocol, video.Video3DFormat, time, cancellationToken).ConfigureAwait(false); File.Copy(tempFile, path, true); try { File.Delete(tempFile); } catch { } chapter.ImagePath = path; chapter.ImageDateModified = _fileSystem.GetLastWriteTimeUtc(path); changesMade = true; } catch (Exception ex) { _logger.ErrorException("Error extracting chapter images for {0}", ex, string.Join(",", inputPath)); success = false; break; } } else if (!string.IsNullOrEmpty(chapter.ImagePath)) { chapter.ImagePath = null; changesMade = true; } } else if (!string.Equals(path, chapter.ImagePath, StringComparison.OrdinalIgnoreCase)) { chapter.ImagePath = path; chapter.ImageDateModified = _fileSystem.GetLastWriteTimeUtc(path); changesMade = true; } } if (saveChapters && changesMade) { await _chapterManager.SaveChapters(video.Id.ToString(), chapters, cancellationToken).ConfigureAwait(false); } DeleteDeadImages(currentImages, chapters); return success; }