Ejemplo n.º 1
0
        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();
            }
        }
Ejemplo n.º 2
0
        public VideoFile(
            string filePath,
            int imageCount = 0)
        {
            if (string.IsNullOrWhiteSpace(filePath))
            {
                throw new ArgumentException($"'{nameof(filePath)}' cannot be" +
                                            $" null or whitespace", nameof(filePath));
            }

            this.filePath = filePath;
            ImageCount    = imageCount;

            if (ImageCount == 0)
            {
                return;
            }

            using (var mpv = new MpvWrapper(FilePath, ImageCount, Duration))
            {
                imageStreams = mpv.GetImages(0, ImageCount).ToList();
            }
        }
Ejemplo n.º 3
0
        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);
            }
        }