Beispiel #1
0
 private void EndpointVideoStreamAdded(IEndpoint sender, EndpointRemoteVideoStreamAddedEventArgs e)
 {
     _remoteVideoStream = e.VideoStream;
     Debug.Log($"Remote added: {_remoteVideoStream.StreamId}");
     _remoteVideoStream.VideoStreamChanged += OnVideoStreamChanged;
     _remoteVideoStream.AddRenderer(_remote.material);
 }
Beispiel #2
0
        // identifies the resolution of a video using `ffmpeg`
        // @param video_path (String) the path to the video file
        // @return [Array] the resolution of the video
        public static int[] VideoResolution(string videoPath)
        {
            IMediaInfo   mediaInfo   = Task.Run(async() => await MediaInfo.Get(videoPath)).Result;
            IVideoStream videoStream = mediaInfo.VideoStreams.FirstOrDefault();

            return(new int[] { videoStream.Width, videoStream.Height });
        }
Beispiel #3
0
 private void CallVideoStreamAdded(ICall sender, CallLocalVideoStreamAddedEventArgs e)
 {
     _localVideoStream = e.VideoStream;
     Debug.Log($"Local added: {_localVideoStream.StreamId}");
     _localVideoStream.VideoStreamChanged += OnVideoStreamChanged;
     _localVideoStream.AddRenderer(_local.material);
 }
        public async Task MultipleWatermarkTest()
        {
            IMediaInfo inputFile = await MediaInfo.Get(Resources.MkvWithAudio).ConfigureAwait(false);

            string       outputPath = Path.ChangeExtension(Path.GetTempFileName(), FileExtensions.Mp4);
            IVideoStream stream     = inputFile.VideoStreams.First()
                                      .SetWatermark(Resources.PngSample, Position.UpperRight, new TimeSpan(), TimeSpan.FromSeconds(3))
                                      .SetWatermark(Resources.PngSample, Position.Center, TimeSpan.FromSeconds(3), TimeSpan.FromSeconds(5))
                                      .SetWatermark(Resources.PngSample, Position.BottomLeft, TimeSpan.FromSeconds(5), inputFile.Duration);


            IConversionResult conversionResult = await Conversion.New()
                                                 .SetPreset(ConversionPreset.UltraFast)
                                                 .AddStream(stream)
                                                 .SetOutput(outputPath)
                                                 .Start().ConfigureAwait(false);

            Assert.True(conversionResult.Success);
            Assert.True(3 == conversionResult.Arguments.Split(' ').Where(x => x.Contains("overlay")).Count());
            Assert.Contains(Resources.PngSample, conversionResult.Arguments);
            IMediaInfo mediaInfo = await MediaInfo.Get(outputPath).ConfigureAwait(false);

            Assert.Equal(TimeSpan.FromSeconds(9), mediaInfo.Duration);
            Assert.Equal("h264", mediaInfo.VideoStreams.First().Format);
            Assert.False(mediaInfo.AudioStreams.Any());
        }
        public async Task PropertiesTest()
        {
            IMediaInfo mediaInfo = await MediaInfo.Get(Resources.Mp4WithAudio).ConfigureAwait(false);

            Assert.True(File.Exists(mediaInfo.FileInfo.FullName));
            Assert.Equal(FileExtensions.Mp4, mediaInfo.FileInfo.Extension);
            Assert.Equal("input.mp4", mediaInfo.FileInfo.Name);

            Assert.Single(mediaInfo.AudioStreams);
            IAudioStream audioStream = mediaInfo.AudioStreams.First();

            Assert.NotNull(audioStream);
            Assert.Equal("aac", audioStream.Format);
            Assert.Equal(TimeSpan.FromSeconds(13), audioStream.Duration);

            Assert.Single(mediaInfo.VideoStreams);
            IVideoStream videoStream = mediaInfo.VideoStreams.First();

            Assert.NotNull(videoStream);
            Assert.Equal(25, videoStream.FrameRate);
            Assert.Equal(720, videoStream.Height);
            Assert.Equal(1280, videoStream.Width);
            Assert.Equal("16:9", videoStream.Ratio);
            Assert.Equal("h264", videoStream.Format);
            Assert.Equal(TimeSpan.FromSeconds(13), videoStream.Duration);

            Assert.Equal(TimeSpan.FromSeconds(13), mediaInfo.Duration);
            Assert.Equal(2107842, mediaInfo.Size);
        }
Beispiel #6
0
        // Outputs a video file as images of every individual frame in the specified directory
        public static async Task <IConversionResult> ConvertToImageSequence(string inputPath, string outputPath, TimeSpan startTime,
                                                                            TimeSpan endTime, int frameWidth, int frameHeight, double fps, CancellationTokenSource cts, Action <double> onProgress)
        {
            // Reads in the video file
            IMediaInfo info = await FFmpeg.GetMediaInfo(inputPath).ConfigureAwait(false);

            IVideoStream videoStream = info.VideoStreams.First();

            // Prepares for converting every video frame to an image
            IConversion conversion = FFmpeg.Conversions.New()
                                     .AddStream(videoStream)
                                     .AddParameter($"-ss {startTime}", ParameterPosition.PreInput)
                                     .AddParameter($"-to {endTime}")
                                     .AddParameter($"-s {frameWidth}x{frameHeight}")
                                     .AddParameter("-q:v 3")
                                     .SetFrameRate(fps)
                                     .SetVideoSyncMethod(VideoSyncMethod.cfr)
                                     .SetOutput(Path.Join(outputPath, "%d.jpg"));

            // Notifies the calling location on the progress of converting
            conversion.OnProgress += (sender, args) =>
            {
                double percent = Math.Round(args.Duration.TotalSeconds / endTime.TotalSeconds * 100, 2);
                onProgress(percent);
            };

            // Starts and awaits the conversion while giving it a cancellation token so it can be stopped at will
            return(await conversion.Start(cts.Token));
        }
Beispiel #7
0
        public async Task SetHashFormatTest(Hash hashFormat, long expectedLength)
        {
            string fileExtension = FileExtensions.Txt;

            if (hashFormat == Hash.SHA256)
            {
                fileExtension = FileExtensions.Sha256;
            }

            string     output = Path.Combine(Path.GetTempPath(), Guid.NewGuid() + fileExtension);
            IMediaInfo info   = await FFmpeg.GetMediaInfo(Resources.MkvWithAudio);

            IVideoStream videoStream = info.VideoStreams.First()?.SetCodec(VideoCodec.copy);
            IAudioStream audioStream = info.AudioStreams.First()?.SetCodec(AudioCodec.copy);

            IConversionResult conversionResult = await FFmpeg.Conversions.New()
                                                 .AddStream(videoStream)
                                                 .AddStream(audioStream)
                                                 .SetOutputFormat(Format.hash)
                                                 .SetHashFormat(hashFormat)
                                                 .SetOutput(output)
                                                 .Start();


            FileInfo fi = new FileInfo(output);

            Assert.Equal(fileExtension, fi.Extension);
            Assert.Equal(expectedLength, fi.Length);
        }
Beispiel #8
0
        public async Task PropertiesTest()
        {
            IMediaInfo mediaInfo = await FFmpeg.GetMediaInfo(Resources.Mp4WithAudio);

            Assert.True(File.Exists(mediaInfo.Path));
            Assert.Equal(FileExtensions.Mp4, Path.GetExtension(mediaInfo.Path));
            Assert.EndsWith("input.mp4", mediaInfo.Path);

            Assert.Single(mediaInfo.AudioStreams);
            IAudioStream audioStream = mediaInfo.AudioStreams.First();

            Assert.NotNull(audioStream);
            Assert.Equal("aac", audioStream.Codec);
            Assert.Equal(TimeSpan.FromSeconds(13), audioStream.Duration);

            Assert.Single(mediaInfo.VideoStreams);
            IVideoStream videoStream = mediaInfo.VideoStreams.First();

            Assert.NotNull(videoStream);
            Assert.Equal(25, videoStream.Framerate);
            Assert.Equal(720, videoStream.Height);
            Assert.Equal(1280, videoStream.Width);
            Assert.Equal("16:9", videoStream.Ratio);
            Assert.Equal("h264", videoStream.Codec);
            Assert.Equal(TimeSpan.FromSeconds(13), videoStream.Duration);

            Assert.Equal(TimeSpan.FromSeconds(13), mediaInfo.Duration);
            Assert.Equal(2107842, mediaInfo.Size);
        }
Beispiel #9
0
        public async Task MkvPropertiesTest()
        {
            IMediaInfo mediaInfo = await FFmpeg.GetMediaInfo(Resources.MkvWithAudio);

            Assert.True(File.Exists(mediaInfo.Path));
            Assert.Equal(FileExtensions.Mkv, Path.GetExtension(mediaInfo.Path));
            Assert.EndsWith("SampleVideo_360x240_1mb.mkv", mediaInfo.Path);

            Assert.Single(mediaInfo.AudioStreams);
            IAudioStream audioStream = mediaInfo.AudioStreams.First();

            Assert.NotNull(audioStream);
            Assert.Equal("aac", audioStream.Codec);
            Assert.Equal(1, audioStream.Index);
            Assert.Equal(TimeSpan.FromSeconds(9), audioStream.Duration);

            Assert.Single(mediaInfo.VideoStreams);
            IVideoStream videoStream = mediaInfo.VideoStreams.First();

            Assert.NotNull(videoStream);
            Assert.Equal(0, videoStream.Index);
            Assert.Equal(25, videoStream.Framerate);
            Assert.Equal(240, videoStream.Height);
            Assert.Equal(320, videoStream.Width);
            Assert.Equal("4:3", videoStream.Ratio);
            Assert.Equal("h264", videoStream.Codec);
            Assert.Equal(TimeSpan.FromSeconds(9), videoStream.Duration);

            Assert.Equal(TimeSpan.FromSeconds(9), mediaInfo.Duration);
            Assert.Equal(1055721, mediaInfo.Size);
        }
Beispiel #10
0
        public async Task SetHashFormatTest(string hashFormat, long expectedLength)
        {
            string fileExtension = string.Empty;

            if (hashFormat == "SHA256")
            {
                fileExtension = FileExtensions.Sha256;
            }
            else
            {
                fileExtension = FileExtensions.Txt;
            }

            string     output = Path.Combine(Path.GetTempPath(), Guid.NewGuid() + fileExtension);
            IMediaInfo info   = await MediaInfo.Get(Resources.MkvWithAudio).ConfigureAwait(false);

            IVideoStream videoStream = info.VideoStreams.First()?.SetCodec(VideoCodec.Copy);
            IAudioStream audioStream = info.AudioStreams.First()?.SetCodec(AudioCodec.Copy);

            IConversionResult conversionResult = await Conversion.New()
                                                 .AddStream(videoStream)
                                                 .AddStream(audioStream)
                                                 .SetOutputFormat(MediaFormat.Hash)
                                                 .SetHashFormat(new HashFormat(hashFormat))
                                                 .SetOutput(output)
                                                 .Start().ConfigureAwait(false);

            Assert.True(conversionResult.Success);
            System.IO.FileInfo fi = new System.IO.FileInfo(output);

            Assert.Equal(fileExtension, fi.Extension);
            Assert.Equal(expectedLength, fi.Length);
        }
Beispiel #11
0
        public VideoScalingStream(IVideoStream source, int width, int height, PixelFormat pixelFormat)
        {
            if (source == null)
            {
                throw new ArgumentNullException("source");
            }

            m_source      = source;
            m_width       = width;
            m_height      = height;
            m_pixelFormat = pixelFormat;

            m_scalingContext = FFmpeg.sws_getContext(source.Width, source.Height, source.PixelFormat,
                                                     width, height, pixelFormat, SwsFlags.Bicubic, null, null, null);
            if (m_scalingContext == null)
            {
                throw new DecoderException("Error getting scaling context");
            }

            if (FFmpeg.avpicture_alloc(out m_outPict, this.PixelFormat, this.Width, this.Height) != 0)
            {
                throw new DecoderException("Error allocating AVPicture");
            }

            m_outPictAllocated = true;
        }
Beispiel #12
0
 public StreamCapture(CancellationToken camToken)
 {
     LogUtil.WriteInc("Initializing stream capture.");
     _targets = new List <VectorOfPoint>();
     SetCapVars();
     _vc = GetStream();
     _vc.Start(camToken);
 }
Beispiel #13
0
        public async Task GetVideoBitrateTest()
        {
            IMediaInfo info = await FFmpeg.GetMediaInfo(Resources.MkvWithAudio);

            IVideoStream videoStream = info.VideoStreams.First();

            Assert.Equal(860233, videoStream.Bitrate);
        }
        public async Task GetVideoBitrateTest()
        {
            IMediaInfo info = await MediaInfo.Get(Resources.MkvWithAudio).ConfigureAwait(false);

            IVideoStream videoStream = info.VideoStreams.First();

            Assert.Equal(860233, videoStream.Bitrate);
        }
        private static string GetOutputArgs(IVideoStream codecToAdd, IAudioStream audio,
                                            string suffix, string destDir, int threadsPerStream)
        {
            string newFileName = $"{suffix}.mp4";
            string outPath     = Path.Combine(destDir, newFileName);

            return($" {codecToAdd.Build()} {audio?.Build()} -threads {threadsPerStream} \"{outPath}\" ");
        }
Beispiel #16
0
        public async Task CalculateFramerate_SloMoVideo_CorrectFramerateIsReturned()
        {
            IMediaInfo info = await FFmpeg.GetMediaInfo(Resources.SloMoMp4);

            IVideoStream videoStream = info.VideoStreams.First();

            Assert.Equal(116.244, videoStream.Framerate);
            Assert.Equal(TimeSpan.FromSeconds(3), videoStream.Duration);
        }
        public async Task CalculateFramerate_SloMoVideo_CorrectFramerateIsReturned()
        {
            IMediaInfo info = await FFmpeg.GetMediaInfo(Resources.SloMoMp4);

            IVideoStream videoStream = info.VideoStreams.First();

            // It does not has to be the same
            Assert.Equal(116, (int)videoStream.Framerate);
            Assert.Equal(3, videoStream.Duration.Seconds);
        }
Beispiel #18
0
        public static IConversion ExtractVideo(string inputPath, string outputPath)
        {
            IMediaInfo info = FFmpeg.GetMediaInfo(inputPath).GetAwaiter().GetResult();

            IVideoStream videoStream = info.VideoStreams.FirstOrDefault();

            return(New()
                   .AddStream(videoStream)
                   .SetOutput(outputPath));
        }
Beispiel #19
0
        /// <summary>
        ///     Extract video from file
        /// </summary>
        /// <param name="inputPath">Input path</param>
        /// <param name="outputPath">Output audio stream</param>
        /// <returns>Conversion result</returns>
        public static IConversion ExtractVideo(string inputPath, string outputPath)
        {
            IMediaInfo info = AsyncHelper.RunSync(() => MediaInfo.Get(inputPath));

            IVideoStream videoStream = info.VideoStreams.FirstOrDefault();

            return(New()
                   .AddStream(videoStream)
                   .SetOutput(outputPath));
        }
Beispiel #20
0
        /// <summary>
        ///     Convert image video stream to gif
        /// </summary>
        /// <param name="inputPath">Input path</param>
        /// <param name="outputPath">Output path</param>
        /// <param name="loop">Number of repeats</param>
        /// <param name="delay">Delay between repeats (in seconds)</param>
        /// <returns>Conversion result</returns>
        public static IConversion ToGif(string inputPath, string outputPath, int loop, int delay = 0)
        {
            IMediaInfo info = AsyncHelper.RunSync(() => MediaInfo.Get(inputPath));

            IVideoStream videoStream = info.VideoStreams.FirstOrDefault()
                                       ?.SetLoop(loop, delay);

            return(New()
                   .AddStream(videoStream)
                   .SetOutput(outputPath));
        }
        /// <summary>
        ///     Convert image video stream to gif
        /// </summary>
        /// <param name="inputPath">Input path</param>
        /// <param name="outputPath">Output path</param>
        /// <param name="loop">Number of repeats</param>
        /// <param name="delay">Delay between repeats (in seconds)</param>
        /// <returns>Conversion result</returns>
        internal static IConversion ToGif(string inputPath, string outputPath, int loop, int delay = 0)
        {
            IMediaInfo info = FFmpeg.GetMediaInfo(inputPath).GetAwaiter().GetResult();

            IVideoStream videoStream = info.VideoStreams.FirstOrDefault()
                                       ?.SetLoop(loop, delay);

            return(New()
                   .AddStream(videoStream)
                   .SetOutput(outputPath));
        }
Beispiel #22
0
        private TimeSpan GetSnapshotTime(GeneratePreviewAction action, IVideoStream stream)
        {
            if (action.TimeOfSnapshot.HasValue)
            {
                return(action.TimeOfSnapshot.Value);
            }

            var snapshotTime = stream.Duration.TotalMilliseconds / 5;

            return(TimeSpan.FromMilliseconds(snapshotTime));
        }
Beispiel #23
0
        public async Task UnknownDecoderException()
        {
            string     output = Path.Combine(Path.GetTempPath(), Guid.NewGuid() + FileExtensions.Mp4);
            IMediaInfo info   = await MediaInfo.Get(Resources.MkvWithAudio).ConfigureAwait(false);

            IVideoStream videoStream = info.VideoStreams.First()?.SetCodec(VideoCodec.Mpeg4);

            await Assert.ThrowsAsync <UnknownDecoderException>(async() =>
            {
                await Conversion.Convert(Resources.MkvWithAudio, output).UseHardwareAcceleration(HardwareAccelerator.Auto, VideoCodec.H264_nvenc, VideoCodec.H264_cuvid).Start().ConfigureAwait(false);
            }).ConfigureAwait(false);
        }
Beispiel #24
0
        public static IConversion SetWatermark(string inputPath, string outputPath, string inputImage, Position position)
        {
            IMediaInfo info = FFmpeg.GetMediaInfo(inputPath).GetAwaiter().GetResult();

            IVideoStream videoStream = info.VideoStreams.FirstOrDefault()
                                       .SetWatermark(inputImage, position);

            return(New()
                   .AddStream(videoStream)
                   .AddStream(info.AudioStreams.ToArray())
                   .SetOutput(outputPath));
        }
Beispiel #25
0
        /// <summary>
        ///     Saves snapshot of video
        /// </summary>
        /// <param name="inputPath">Video</param>
        /// <param name="outputPath">Output file</param>
        /// <param name="captureTime">TimeSpan of snapshot</param>
        /// <returns>Conversion result</returns>
        public static IConversion Snapshot(string inputPath, string outputPath, TimeSpan captureTime)
        {
            IMediaInfo info = AsyncHelper.RunSync(() => MediaInfo.Get(inputPath));

            IVideoStream videoStream = info.VideoStreams.FirstOrDefault()
                                       .SetOutputFramesCount(1)
                                       .SetSeek(captureTime);

            return(New()
                   .AddStream(videoStream)
                   .SetOutput(outputPath));
        }
Beispiel #26
0
        /// <summary>
        ///     Melt watermark into video
        /// </summary>
        /// <param name="inputPath">Input video path</param>
        /// <param name="outputPath">Output file</param>
        /// <param name="inputImage">Watermark</param>
        /// <param name="position">Position of watermark</param>
        /// <returns>Conversion result</returns>
        public static IConversion SetWatermark(string inputPath, string outputPath, string inputImage, Position position)
        {
            IMediaInfo info = AsyncHelper.RunSync(() => MediaInfo.Get(inputPath));

            IVideoStream videoStream = info.VideoStreams.FirstOrDefault()
                                       .SetWatermark(inputImage, position);

            return(New()
                   .AddStream(videoStream)
                   .AddStream(info.AudioStreams.ToArray())
                   .SetOutput(outputPath));
        }
Beispiel #27
0
        /// <summary>
        ///     Add subtitles to video stream
        /// </summary>
        /// <param name="inputPath">Video</param>
        /// <param name="outputPath">Output file</param>
        /// <param name="subtitlesPath">Subtitles</param>
        /// <returns>Conversion result</returns>
        public static IConversion AddSubtitles(string inputPath, string outputPath, string subtitlesPath)
        {
            IMediaInfo info = AsyncHelper.RunSync(() => MediaInfo.Get(inputPath));

            IVideoStream videoStream = info.VideoStreams.FirstOrDefault()
                                       ?.AddSubtitles(subtitlesPath);

            return(New()
                   .AddStream(videoStream)
                   .AddStream(info.AudioStreams.FirstOrDefault())
                   .SetOutput(outputPath));
        }
Beispiel #28
0
        /// <summary>
        ///     Add subtitles to video stream
        /// </summary>
        /// <param name="inputPath">Video</param>
        /// <param name="outputPath">Output file</param>
        /// <param name="subtitlesPath">Subtitles</param>
        /// <returns>Conversion result</returns>
        internal static IConversion AddSubtitles(string inputPath, string outputPath, string subtitlesPath)
        {
            IMediaInfo info = FFmpeg.GetMediaInfo(inputPath).GetAwaiter().GetResult();

            IVideoStream videoStream = info.VideoStreams.FirstOrDefault()
                                       ?.AddSubtitles(subtitlesPath);

            return(New()
                   .AddStream(videoStream)
                   .AddStream(info.AudioStreams.FirstOrDefault())
                   .SetOutput(outputPath));
        }
Beispiel #29
0
        public static IConversion Snapshot(string inputPath, string outputPath, TimeSpan captureTime)
        {
            IMediaInfo info = FFmpeg.GetMediaInfo(inputPath).GetAwaiter().GetResult();

            IVideoStream videoStream = info.VideoStreams.FirstOrDefault()
                                       .SetOutputFramesCount(1)
                                       .SetSeek(captureTime);

            return(New()
                   .AddStream(videoStream)
                   .SetOutput(outputPath));
        }
Beispiel #30
0
        public static IConversion ChangeSize(string inputPath, string outputPath, int width, int height)
        {
            IMediaInfo info = FFmpeg.GetMediaInfo(inputPath).GetAwaiter().GetResult();

            IVideoStream videoStream = info.VideoStreams.FirstOrDefault()
                                       .SetSize(width, height);

            return(New()
                   .AddStream(videoStream)
                   .AddStream(info.AudioStreams.ToArray())
                   .AddStream(info.SubtitleStreams.ToArray())
                   .SetOutput(outputPath));
        }
        public VideoScalingStream(IVideoStream source, int width, int height, PixelFormat pixelFormat)
        {
            if (source == null)
                throw new ArgumentNullException("source");

            m_source = source;
            m_width = width;
            m_height = height;
            m_pixelFormat = pixelFormat;

            m_scalingContext = FFmpeg.sws_getContext(source.Width, source.Height, source.PixelFormat,
                                                     width, height, pixelFormat, SwsFlags.Bicubic, null, null, null);
            if (m_scalingContext == null)
                throw new DecoderException("Error getting scaling context");

            if (FFmpeg.avpicture_alloc(out m_outPict, this.PixelFormat, this.Width, this.Height) !=0)
                throw new DecoderException("Error allocating AVPicture");

            m_outPictAllocated = true;
        }