public VideoFile( VideoFile other, int imageCount = 0) { if (other is null) { throw new ArgumentNullException(nameof(other)); } ImageCount = imageCount; // We have to do a bit more work here since we can't use // the other ctors because we don't want to eagerly load // the images when we can copy them from the other file. filePath = other.filePath; fileSize = other.fileSize; duration = other.duration; codecInfo = other.codecInfo; if (ImageCount == 0) { return; } if (other.ImageCount == ImageCount) { imageStreams = other.imageStreams .Select(image => { var ms = new MemoryStream(); image.Position = 0; image.CopyTo(ms); return(ms); }) .ToList(); return; } using (var mpv = new MpvWrapper(FilePath, imageCount, Duration)) { imageStreams = mpv.GetImages(0, imageCount).ToList(); } }
private void FindDuplicatesOf( IEnumerable <VideoFile> videoFiles, VideoFile refFile, CancellationToken cancelToken) { OnLogged(string.Format(LogCheckingFile, refFile.FilePath, refFile.Duration.ToPrettyString())); foreach (var file in videoFiles .Where(f => f != refFile) .Where(f => f.IsDurationEqual(refFile, CurrentState.Settings))) { if (cancelToken.IsCancellationRequested) { return; } OnLogged(string.Format(LogCompareFile, file.FilePath)); try { var comparer = new VideoComparer { LeftVideoFile = file, RightVideoFile = refFile, Settings = CurrentState.Settings, }; if (comparer.Compare(cancelToken) == ComparisonResult.Duplicate) { OnLogged($"Found duplicate of {refFile.FilePath} and" + $" {file.FilePath}"); OnDuplicateFound(refFile, file); } } catch (AggregateException exc) when(exc.InnerException is MpvLib.MpvException) { OnLogged(exc.InnerException.Message); } } }
private (MemoryStream ImageStream, int LoadLevel) LoadImage( VideoFile videoFile, int index, IImageComparisonSettings settings) { if (index >= settings.MaxImageCompares || index < 0) { throw new ArgumentOutOfRangeException(nameof(index), "Index out of range."); } if (videoFile.ImageCount != 0 && videoFile.ImageCount != settings.MaxImageCompares) { videoFile.DisposeImages(); } videoFile.ImageCount = settings.MaxImageCompares; if (index < videoFile.ImageStreams.Count()) { return(videoFile.ImageStreams[index], 0); } using (var mpv = new MpvWrapper( videoFile.FilePath, settings.MaxImageCompares, videoFile.Duration)) { // First loading step, only the minimum (when index == 0) var imagesToLoad = settings.MaxDifferentImages + 1; var loadLevel = 1; // Second loading step if (index == settings.MaxDifferentImages + 1) { imagesToLoad = settings.MaxImageCompares - index - settings.MaxDifferentImages; loadLevel = 2; } // Third loading step else if (index == settings.MaxImageCompares - settings.MaxDifferentImages) { imagesToLoad = settings.MaxDifferentImages; loadLevel = 3; } // To make sure we never load more than we initially wanted. imagesToLoad = Math.Min( imagesToLoad, settings.MaxImageCompares - index); foreach (var image in mpv.GetImages(index, imagesToLoad)) { videoFile.ImageStreams.Add(image); } if (index >= videoFile.ImageStreams.Count()) { return(null, loadLevel); } return(videoFile.ImageStreams[index], loadLevel); } }