コード例 #1
0
        //[Singleton]
        public async Task Run([QueueTrigger("video-%Meeemories:ContainerName%")] string id, ILogger log)
        {
            var media = await _service.FindAsync(id);

            if (media == null)
            {
                log.LogInformation($"NotFound {id}");
                return;
            }

            media.Status = MediaStatus.Converting;
            await _service.UpdateAsync(media);

            var rawPath     = Path.GetTempFileName();
            var jpgPath     = $"{rawPath}.jpg";
            var thumbPath   = $"{rawPath}_thumb.jpg";
            var resizedPath = $"{rawPath}_resized.mp4";

            try
            {
                var raw = _service.OpenBlob(media);
                await raw.DownloadToFileAsync(rawPath, FileMode.OpenOrCreate);

                var ffmpeg      = new Engine(StaticFiles.Path("ffmpeg.exe"));
                var inputFile   = new MediaFile(rawPath);
                var jpgFile     = new MediaFile(jpgPath);
                var thumbFile   = new MediaFile(thumbPath);
                var resizedFile = new MediaFile(resizedPath);

                await ffmpeg.GetThumbnailAsync(inputFile, jpgFile, new ConversionOptions
                {
                    Seek = TimeSpan.FromMilliseconds(10)
                });

                double aspect;
                using (var image = new MagickImage(jpgPath))
                {
                    aspect = (double)image.Height / image.Width;
                }

                int width = 400, height = (int)(aspect * width);

                if (height % 2 == 1)
                {
                    height += 1;
                }

                await ffmpeg.ConvertAsync(inputFile, resizedFile, new ConversionOptions
                {
                    CustomWidth  = width,
                    CustomHeight = height,
                    VideoSize    = FFmpeg.NET.Enums.VideoSize.Custom,
                });

                await ffmpeg.GetThumbnailAsync(inputFile, thumbFile, new ConversionOptions
                {
                    Seek = TimeSpan.FromSeconds(3)
                });

                var sources = new List <MediaSource>();

                var thumbBlob   = _service.CreateBlob($"{width:000}w/{raw.Name}.jpg");
                var resizedBlob = _service.CreateBlob($"{width:000}w/{raw.Name}.mp4");

                await thumbBlob.UploadFromFileAsync(thumbPath);

                thumbBlob.Properties.ContentType = "image/jpeg";
                await thumbBlob.SetPropertiesAsync();

                sources.Add(new MediaSource
                {
                    Url      = thumbBlob.Uri.ToString(),
                    Width    = width,
                    Height   = height,
                    MimeType = "image/jpeg"
                });

                await resizedBlob.UploadFromFileAsync(resizedPath);

                resizedBlob.Properties.ContentType = "video/mp4";
                await resizedBlob.SetPropertiesAsync();

                sources.Add(new MediaSource
                {
                    Url      = resizedBlob.Uri.ToString(),
                    Width    = width,
                    Height   = height,
                    MimeType = "video/mp4"
                });

                media.Sources = sources;
                media.Status  = MediaStatus.Complete;
                await _service.UpdateAsync(media);
            }
            catch (Exception e)
            {
                log.LogError(e.Message);
                media.Status = MediaStatus.Fail;
                await _service.UpdateAsync(media);
            }
            finally
            {
                if (File.Exists(rawPath))
                {
                    File.Delete(rawPath);
                }

                if (File.Exists(jpgPath))
                {
                    File.Delete(jpgPath);
                }

                if (File.Exists(thumbPath))
                {
                    File.Delete(thumbPath);
                }

                if (File.Exists(resizedPath))
                {
                    File.Delete(resizedPath);
                }
            }
        }
コード例 #2
0
        public async Task Run([QueueTrigger("image-%Meeemories:ContainerName%")] string id, ILogger log)
        {
            var media = await _service.FindAsync(id);

            if (media == null)
            {
                log.LogInformation($"NotFound {id})");
                return;
            }

            media.Status = MediaStatus.Converting;
            await _service.UpdateAsync(media);

            var raw     = _service.OpenBlob(media);
            var rawPath = Path.GetTempFileName();
            await raw.DownloadToFileAsync(rawPath, FileMode.OpenOrCreate);

            async Task <MediaSource> Convert(Media media, int width)
            {
                var blob = _service.CreateBlob($"{width:000}w/{raw.Name}.jpg");

                using var stream = File.OpenRead(rawPath);
                using var image  = new MagickImage(stream);

                var aspect = (double)image.Height / image.Width;
                var height = (int)(aspect * width);

                image.Resize(width, height);
                image.Format  = MagickFormat.Jpeg;
                image.Quality = 85;

                var binary = image.ToByteArray();
                await blob.UploadFromByteArrayAsync(binary, 0, binary.Length);

                blob.Properties.ContentType = "image/jpeg";
                await blob.SetPropertiesAsync();

                return(new MediaSource
                {
                    Url = blob.Uri.ToString(),
                    Width = width,
                    Height = height,
                    MimeType = "image/jpeg"
                });
            }

            try
            {
                var sources = new List <MediaSource>();
                sources.Add(await Convert(media, 20));
                sources.Add(await Convert(media, 200));
                sources.Add(await Convert(media, 400));
                sources.Add(await Convert(media, 800));

                media.Sources = sources;
                media.Status  = MediaStatus.Complete;
                await _service.UpdateAsync(media);
            }
            catch (Exception e)
            {
                log.LogError(e.Message);
                media.Status = MediaStatus.Fail;
                await _service.UpdateAsync(media);
            }
        }