< Summary - Jellyfin

Information
Class: MediaBrowser.Providers.MediaInfo.VideoImageProvider
Assembly: MediaBrowser.Providers
File(s): /srv/git/jellyfin/MediaBrowser.Providers/MediaInfo/VideoImageProvider.cs
Line coverage
73%
Covered lines: 14
Uncovered lines: 5
Coverable lines: 19
Total lines: 134
Line coverage: 73.6%
Branch coverage
75%
Covered branches: 12
Total branches: 16
Branch coverage: 75%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Coverage history

Coverage history 0 25 50 75 100

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)100%11100%
get_Name()100%210%
get_Order()100%210%
GetSupportedImages(...)100%210%
GetImage(...)100%88100%
Supports(...)50%12860%

File(s)

/srv/git/jellyfin/MediaBrowser.Providers/MediaInfo/VideoImageProvider.cs

#LineLine coverage
 1#pragma warning disable CA1826 // CA1826 Do not use Enumerable methods on Indexable collections.
 2
 3using System;
 4using System.Collections.Generic;
 5using System.Linq;
 6using System.Threading;
 7using System.Threading.Tasks;
 8using MediaBrowser.Controller.Entities;
 9using MediaBrowser.Controller.Library;
 10using MediaBrowser.Controller.MediaEncoding;
 11using MediaBrowser.Controller.Persistence;
 12using MediaBrowser.Controller.Providers;
 13using MediaBrowser.Model.Drawing;
 14using MediaBrowser.Model.Dto;
 15using MediaBrowser.Model.Entities;
 16using MediaBrowser.Model.MediaInfo;
 17using Microsoft.Extensions.Logging;
 18
 19namespace MediaBrowser.Providers.MediaInfo
 20{
 21    /// <summary>
 22    /// Uses <see cref="IMediaEncoder"/> to create still images from the main video.
 23    /// </summary>
 24    public class VideoImageProvider : IDynamicImageProvider, IHasOrder
 25    {
 26        private readonly IMediaSourceManager _mediaSourceManager;
 27        private readonly IMediaEncoder _mediaEncoder;
 28        private readonly ILogger<VideoImageProvider> _logger;
 29
 30        /// <summary>
 31        /// Initializes a new instance of the <see cref="VideoImageProvider"/> class.
 32        /// </summary>
 33        /// <param name="mediaSourceManager">The media source manager for fetching item streams.</param>
 34        /// <param name="mediaEncoder">The media encoder for capturing images.</param>
 35        /// <param name="logger">The logger.</param>
 36        public VideoImageProvider(IMediaSourceManager mediaSourceManager, IMediaEncoder mediaEncoder, ILogger<VideoImage
 37        {
 2838            _mediaSourceManager = mediaSourceManager;
 2839            _mediaEncoder = mediaEncoder;
 2840            _logger = logger;
 2841        }
 42
 43        /// <inheritdoc />
 044        public string Name => "Screen Grabber";
 45
 46        /// <inheritdoc />
 47        // Make sure this comes after internet image providers
 048        public int Order => 100;
 49
 50        /// <inheritdoc />
 51        public IEnumerable<ImageType> GetSupportedImages(BaseItem item)
 52        {
 053            return new[] { ImageType.Primary };
 54        }
 55
 56        /// <inheritdoc />
 57        public Task<DynamicImageResponse> GetImage(BaseItem item, ImageType type, CancellationToken cancellationToken)
 58        {
 759            var video = (Video)item;
 60
 61            // No support for these
 762            if (video.IsPlaceHolder || video.VideoType == VideoType.Dvd)
 63            {
 164                return Task.FromResult(new DynamicImageResponse { HasImage = false });
 65            }
 66
 67            // Can't extract if we didn't find a video stream in the file
 668            if (!video.DefaultVideoStreamIndex.HasValue)
 69            {
 170                _logger.LogInformation("Skipping image extraction due to missing DefaultVideoStreamIndex for {Path}.", v
 171                return Task.FromResult(new DynamicImageResponse { HasImage = false });
 72            }
 73
 574            return GetVideoImage(video, cancellationToken);
 75        }
 76
 77        private async Task<DynamicImageResponse> GetVideoImage(Video item, CancellationToken cancellationToken)
 78        {
 79            MediaSourceInfo mediaSource = new MediaSourceInfo
 80            {
 81                VideoType = item.VideoType,
 82                IsoType = item.IsoType,
 83                Protocol = item.PathProtocol ?? MediaProtocol.File,
 84            };
 85
 86            // If we know the duration, grab it from 10% into the video. Otherwise just 10 seconds in.
 87            // Always use 10 seconds for dvd because our duration could be out of whack
 88            var imageOffset = item.VideoType != VideoType.Dvd && item.RunTimeTicks > 0
 89                                  ? TimeSpan.FromTicks(item.RunTimeTicks.Value / 10)
 90                                  : TimeSpan.FromSeconds(10);
 91
 92            var query = new MediaStreamQuery { ItemId = item.Id, Index = item.DefaultVideoStreamIndex };
 93            var videoStream = _mediaSourceManager.GetMediaStreams(query).FirstOrDefault();
 94            if (videoStream is null)
 95            {
 96                query.Type = MediaStreamType.Video;
 97                query.Index = null;
 98                videoStream = _mediaSourceManager.GetMediaStreams(query).FirstOrDefault();
 99            }
 100
 101            if (videoStream is null)
 102            {
 103                _logger.LogInformation("Skipping image extraction: no video stream found for {Path}.", item.Path ?? stri
 104                return new DynamicImageResponse { HasImage = false };
 105            }
 106
 107            string extractedImagePath = await _mediaEncoder.ExtractVideoImage(item.Path, item.Container, mediaSource, vi
 108
 109            return new DynamicImageResponse
 110            {
 111                Format = ImageFormat.Jpg,
 112                HasImage = true,
 113                Path = extractedImagePath,
 114                Protocol = MediaProtocol.File
 115            };
 116        }
 117
 118        /// <inheritdoc />
 119        public bool Supports(BaseItem item)
 120        {
 53121            if (item.IsShortcut)
 122            {
 0123                return false;
 124            }
 125
 53126            if (!item.IsFileProtocol)
 127            {
 0128                return false;
 129            }
 130
 53131            return item is Video video && !video.IsPlaceHolder && video.IsCompleteMedia;
 132        }
 133    }
 134}