コード例 #1
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;
        }
    }
コード例 #2
0
    private async ValueTask <ThumbnailGeneratorGetThumbnailResult> GetPictureThumbnailAsync(NestedPath filePath, ThumbnailGeneratorGetThumbnailOptions options, CancellationToken cancellationToken = default)
    {
        var ext = filePath.GetExtension().ToLower();

        if (!_pictureTypeExtensionList.Contains(ext))
        {
            return(new ThumbnailGeneratorGetThumbnailResult(ThumbnailGeneratorGetThumbnailResultStatus.Failed));
        }

        try
        {
            var fileLength = await _fileSystem.GetFileSizeAsync(filePath, cancellationToken);

            var fileLastWriteTime = await _fileSystem.GetFileLastWriteTimeAsync(filePath, cancellationToken);

            using (var inStream = await _fileSystem.GetFileStreamAsync(filePath, cancellationToken))
                using (var outStream = new RecyclableMemoryStream(_bytesPool))
                {
                    this.ConvertImage(inStream, outStream, options.Width, options.Height, options.ResizeType, options.FormatType);
                    outStream.Seek(0, SeekOrigin.Begin);

                    var image = outStream.ToMemoryOwner();

                    var fileMeta      = new FileMeta(filePath, (ulong)fileLength, Timestamp.FromDateTime(fileLastWriteTime));
                    var thumbnailMeta = new ThumbnailMeta(options.ResizeType, options.FormatType, (uint)options.Width, (uint)options.Height);
                    var content       = new ThumbnailContent(image);
                    var cache         = new ThumbnailCache(fileMeta, thumbnailMeta, new[] { content });

                    await _thumbnailGeneratorRepository.ThumbnailCaches.InsertAsync(cache);

                    return(new ThumbnailGeneratorGetThumbnailResult(ThumbnailGeneratorGetThumbnailResultStatus.Succeeded, cache.Contents));
                }
        }
        catch (NotSupportedException e)
        {
            _logger.Warn(e);
        }
        catch (OperationCanceledException e)
        {
            _logger.Debug(e);
        }
        catch (Exception e)
        {
            _logger.Error(e);
            throw;
        }

        return(new ThumbnailGeneratorGetThumbnailResult(ThumbnailGeneratorGetThumbnailResultStatus.Failed));
    }