Beispiel #1
0
        /// <summary>
        ///     Get proporties prom media file
        /// </summary>
        /// <param name="cancellationToken">Cancellation token</param>
        /// <param name="mediaInfo">Empty media info</param>
        /// <returns>Properties</returns>
        public async Task <MediaInfo> SetProperties(MediaInfo mediaInfo, CancellationToken cancellationToken)
        {
            var path = mediaInfo.Path;

            ProbeModel.Stream[] streams = await GetStreams(path, cancellationToken);

            if (!streams.Any())
            {
                throw new ArgumentException($"Invalid file. Cannot load file {path}");
            }

            FormatModel.Format format = await GetFormat(path, cancellationToken);

            if (format.size != null)
            {
                mediaInfo.Size = long.Parse(format.size);
            }

            mediaInfo.VideoStreams    = PrepareVideoStreams(path, streams.Where(x => x.codec_type == "video"), format);
            mediaInfo.AudioStreams    = PrepareAudioStreams(path, streams.Where(x => x.codec_type == "audio"));
            mediaInfo.SubtitleStreams = PrepareSubtitleStreams(path, streams.Where(x => x.codec_type == "subtitle"));

            mediaInfo.Duration = CalculateDuration(mediaInfo.VideoStreams, mediaInfo.AudioStreams);
            return(mediaInfo);
        }
Beispiel #2
0
        public MediaProperties GetProperties(string videoPath)
        {
            var videoProperties = new MediaProperties();

            ProbeModel.Stream[] streams     = GetStream(videoPath);
            ProbeModel.Stream   videoStream = streams[0];
            ProbeModel.Stream   audioStream = streams[1];
            FormatModel.Format  format      = GetFormat(videoPath);
            videoProperties.Size = long.Parse(format.size);

            if (videoStream != null)
            {
                videoProperties.VideoFormat   = videoStream.codec_name;
                videoProperties.VideoDuration = GetVideoDuration(format, videoStream);
                videoProperties.Width         = videoStream.width;
                videoProperties.Height        = videoStream.height;
                videoProperties.FrameRate     = GetVideoFramerate(videoStream);
                videoProperties.Ratio         = GetVideoAspectRatio(videoProperties.Width, videoProperties.Height);
            }
            if (audioStream != null)
            {
                videoProperties.AudioFormat   = audioStream.codec_name;
                videoProperties.AudioDuration = GetAudioDuration(audioStream);
            }

            videoProperties.Duration = TimeSpan.FromSeconds(Math.Max(videoProperties.VideoDuration.TotalSeconds, videoProperties.AudioDuration.TotalSeconds));
            return(videoProperties);
        }
        private TimeSpan GetVideoDuration(ProbeModel.Stream video, FormatModel.Format format)
        {
            double   duration      = video.duration > 0.01 ? video.duration : format.duration;
            TimeSpan videoDuration = TimeSpan.FromSeconds(duration);

            return(videoDuration);
        }
Beispiel #4
0
        private FormatModel.Format GetFormat(string videoPath)
        {
            string jsonFormat =
                RunProcess($"-v quiet -print_format json -show_format \"{videoPath}\"");

            FormatModel.Format format = JsonConvert.DeserializeObject <FormatModel.Root>(jsonFormat)
                                        .format;
            return(format);
        }
Beispiel #5
0
        private TimeSpan GetVideoDuration(FormatModel.Format format, ProbeModel.Stream video)
        {
            video.duration = format.duration;
            // ReSharper disable once CompareOfFloatsByEqualityOperator
            video.bit_rate = format.bitRate == 0 ? video.bit_rate : format.bitRate;

            double   duration      = video.duration;
            TimeSpan videoDuration = TimeSpan.FromSeconds(duration);

            videoDuration = videoDuration.Subtract(TimeSpan.FromMilliseconds(videoDuration.Milliseconds));

            return(videoDuration);
        }
Beispiel #6
0
        /// <summary>
        ///     Get proporties prom media file
        /// </summary>
        /// <param name="fileInfo">Media file info</param>
        /// <param name="mediaInfo">Empty media info</param>
        /// <returns>Properties</returns>
        public async Task <MediaInfo> GetProperties(System.IO.FileInfo fileInfo, MediaInfo mediaInfo)
        {
            ProbeModel.Stream[] streams = await GetStream(fileInfo.FullName).ConfigureAwait(false);

            if (!streams.Any())
            {
                throw new ArgumentException($"Invalid file. Cannot load file {fileInfo.Name}");
            }

            FormatModel.Format format = await GetFormat(fileInfo.FullName).ConfigureAwait(false);

            mediaInfo.Size = long.Parse(format.size);

            mediaInfo.VideoStreams    = PrepareVideoStreams(fileInfo, streams.Where(x => x.codec_type == "video"), format);
            mediaInfo.AudioStreams    = PrepareAudioStreams(fileInfo, streams.Where(x => x.codec_type == "audio"));
            mediaInfo.SubtitleStreams = PrepareSubtitleStreams(fileInfo, streams.Where(x => x.codec_type == "subtitle"));

            mediaInfo.Duration = CalculateDuration(mediaInfo.VideoStreams, mediaInfo.AudioStreams);
            return(mediaInfo);
        }
Beispiel #7
0
 private IEnumerable <IVideoStream> PrepareVideoStreams(System.IO.FileInfo fileInfo, IEnumerable <ProbeModel.Stream> videoStreamModels, FormatModel.Format format)
 {
     foreach (ProbeModel.Stream model in videoStreamModels)
     {
         var stream = new VideoStream
         {
             Format      = model.codec_name,
             Duration    = GetVideoDuration(model, format),
             Width       = model.width,
             Height      = model.height,
             FrameRate   = GetVideoFramerate(model),
             Ratio       = GetVideoAspectRatio(model.width, model.height),
             Source      = fileInfo,
             Index       = model.index,
             Bitrate     = Math.Abs(model.bit_rate) > 0.01 ? model.bit_rate : format.bit_Rate,
             PixelFormat = model.pix_fmt,
             Default     = model.disposition?._default,
             Forced      = model.disposition?.forced
         };
         yield return(stream);
     }
 }
Beispiel #8
0
 private IEnumerable <IVideoStream> PrepareVideoStreams(string path, IEnumerable <ProbeModel.Stream> videoStreamModels, FormatModel.Format format)
 {
     return(videoStreamModels.Select(model => new VideoStream()
     {
         Codec = model.codec_name,
         Duration = GetVideoDuration(model, format),
         Width = model.width,
         Height = model.height,
         Framerate = GetVideoFramerate(model),
         Ratio = GetVideoAspectRatio(model.width, model.height),
         Path = path,
         Index = model.index,
         Bitrate = Math.Abs(model.bit_rate) > 0.01 ? model.bit_rate : format.bit_Rate,
         PixelFormat = model.pix_fmt,
         Default = model.disposition?._default,
         Forced = model.disposition?.forced,
         Rotation = model.tags?.rotate
     }));
 }