示例#1
0
    private void InternalImageSharpConvertImage(Stream inStream, Stream outStream, int width, int height, ThumbnailResizeType resizeType, ThumbnailFormatType formatType)
    {
        using var image = SixLabors.ImageSharp.Image.Load(inStream);
        image.Mutate(x =>
        {
            var resizeOptions = new ResizeOptions
            {
                Position = AnchorPositionMode.Center,
                Size     = new SixLabors.ImageSharp.Size(width, height),
                Mode     = resizeType switch
                {
                    ThumbnailResizeType.Pad => ResizeMode.Pad,
                    ThumbnailResizeType.Crop => ResizeMode.Crop,
                    _ => throw new NotSupportedException(),
                },
            };

            x.Resize(resizeOptions);
        });

        if (formatType == ThumbnailFormatType.Png)
        {
            var encoder = new SixLabors.ImageSharp.Formats.Png.PngEncoder();
            image.Save(outStream, encoder);
            return;
        }

        throw new NotSupportedException();
    }
示例#2
0
 private static string ResizeTypeToString(ThumbnailResizeType resizeType)
 {
     return(resizeType switch
     {
         ThumbnailResizeType.Crop => "crop",
         ThumbnailResizeType.Pad => "pad",
         _ => throw new NotSupportedException(nameof(resizeType)),
     });
示例#3
0
 public IEnumerable <Thumbnail> GetThumbnails(string path, int width, int height, ThumbnailFormatType formatType, ThumbnailResizeType resizeType, CancellationToken token = default)
 {
     return(_contentsManager.GetThumnails(path, width, height, formatType, resizeType, token));
 }
示例#4
0
 public IEnumerable <Thumbnail> GetThumnails(string path, int width, int height, ThumbnailFormatType formatType, ThumbnailResizeType resizeType, CancellationToken token = default)
 {
     return(_thumbnailCacheStorage.GetThumnailImages(path, width, height, formatType, resizeType, token));
 }
示例#5
0
    private void ConvertImage(Stream inStream, Stream outStream, int width, int height, ThumbnailResizeType resizeType, ThumbnailFormatType formatType)
    {
        try
        {
            this.InternalImageSharpConvertImage(inStream, outStream, width, height, resizeType, formatType);
        }
        catch (SixLabors.ImageSharp.UnknownImageFormatException)
        {
            using (var bitmapStream = new RecyclableMemoryStream(_bytesPool))
            {
                this.InternalMagickImageConvertImage(inStream, bitmapStream);
                bitmapStream.Seek(0, SeekOrigin.Begin);

                this.InternalImageSharpConvertImage(bitmapStream, outStream, width, height, resizeType, formatType);
            }
        }
    }
示例#6
0
    private async ValueTask <IMemoryOwner <byte> > GetMovieImageAsync(string path, int width, int height, ThumbnailResizeType resizeType, ThumbnailFormatType formatType, CancellationToken cancellationToken = default)
    {
        try
        {
            var arguments = $"-loglevel error -i \"{path}\" -vf thumbnail=30 -frames:v 1 -f image2 pipe:1";

            using var process = Process.Start(new ProcessStartInfo("ffmpeg", arguments)
            {
                CreateNoWindow         = true,
                UseShellExecute        = false,
                RedirectStandardOutput = true,
                RedirectStandardError  = false,
            });

            using var baseStream = process !.StandardOutput.BaseStream;

            using var inStream  = new RecyclableMemoryStream(_bytesPool);
            using var outStream = new RecyclableMemoryStream(_bytesPool);

            await baseStream.CopyToAsync(inStream, cancellationToken);

            inStream.Seek(0, SeekOrigin.Begin);
            await process.WaitForExitAsync(cancellationToken);

            this.ConvertImage(inStream, outStream, width, height, resizeType, formatType);

            return(outStream.ToMemoryOwner());
        }
        catch (Exception e)
        {
            _logger.Warn(e);
            throw;
        }
    }
示例#7
0
    private async ValueTask <IMemoryOwner <byte>[]> GetMovieImagesAsync(NestedPath filePath, TimeSpan minInterval, int maxImageCount, int width, int height, ThumbnailResizeType resizeType, ThumbnailFormatType formatType, CancellationToken cancellationToken = default)
    {
        var resultMap = new ConcurrentDictionary <int, IMemoryOwner <byte> >();

        IExtractedFileOwner?extractedFile = null;

        if (filePath.Values.Count > 1)
        {
            extractedFile = await _fileSystem.TryExtractFileAsync(filePath, cancellationToken);
        }

        try
        {
            var targetFilePath = extractedFile?.Path ?? filePath.Values[0];

            var duration = await GetMovieDurationAsync(targetFilePath, cancellationToken).ConfigureAwait(false);

            int intervalSeconds = (int)Math.Max(minInterval.TotalSeconds, duration.TotalSeconds / maxImageCount);
            int imageCount      = (int)(duration.TotalSeconds / intervalSeconds);

            await Enumerable.Range(1, imageCount)
            .Select(x => x * intervalSeconds)
            .Where(seekSec => (duration.TotalSeconds - seekSec) > 1)     // 残り1秒以下の場合は除外
            .ForEachAsync(
                async seekSec =>
            {
                var ret = await this.GetMovieImagesAsync(targetFilePath, seekSec, width, height, resizeType, formatType, cancellationToken);
                resultMap.TryAdd(ret.SeekSec, ret.MemoryOwner);
            }, (int)_concurrency, cancellationToken).ConfigureAwait(false);

            cancellationToken.ThrowIfCancellationRequested();

            if (!resultMap.IsEmpty)
            {
                var tempList = resultMap.ToList();
                tempList.Sort((x, y) => x.Key.CompareTo(y.Key));

                return(tempList.Select(n => n.Value).ToArray());
            }

            var ret = await this.GetMovieImageAsync(targetFilePath, width, height, resizeType, formatType, cancellationToken);

            return(new[] { ret });
        }
        finally
        {
            if (extractedFile is not null)
            {
                extractedFile.Dispose();
            }
        }
    }
        public async ValueTask <ThumbnailCache?> FindOneAsync(NestedPath filePath, int width, int height, ThumbnailResizeType resizeType, ThumbnailFormatType formatType)
        {
            await Task.Delay(1).ConfigureAwait(false);

            using (await _asyncLock.LockAsync())
            {
                var id = new ThumbnailCacheIdEntity()
                {
                    FilePath            = NestedPathEntity.Import(filePath),
                    ThumbnailWidth      = width,
                    ThumbnailHeight     = height,
                    ThumbnailResizeType = resizeType,
                    ThumbnailFormatType = formatType,
                };
                var storage = this.GetStorage();

                var liteFileInfo = storage.FindById(id);
                if (liteFileInfo is null)
                {
                    return(null);
                }

                using (var inStream = liteFileInfo.OpenRead())
                {
                    return(RocketMessage.FromStream <ThumbnailCache>(inStream));
                }
            }
        }
 public ThumbnailGeneratorGetThumbnailOptions(int width, int height, ThumbnailFormatType formatType, ThumbnailResizeType resizeType, TimeSpan minInterval, int maxImageCount)
 {
     this.Width         = width;
     this.Height        = height;
     this.FormatType    = formatType;
     this.ResizeType    = resizeType;
     this.MinInterval   = minInterval;
     this.MaxImageCount = maxImageCount;
 }