示例#1
0
        private void ConvertFromStreamPipe(ContainerFormat type, params IArgument[] inputArguments)
        {
            var output = Input.OutputLocation(type);

            try
            {
                var input = FFProbe.Analyse(VideoLibrary.LocalVideoWebm.FullName);
                using (var inputStream = File.OpenRead(input.Path))
                {
                    var pipeSource = new StreamPipeSource(inputStream);
                    var arguments  = FFMpegArguments.FromPipe(pipeSource);
                    foreach (var arg in inputArguments)
                    {
                        arguments.WithArgument(arg);
                    }
                    var processor = arguments.OutputToFile(output);

                    var scaling = arguments.Find <ScaleArgument>();

                    var success = processor.ProcessSynchronously();

                    var outputVideo = FFProbe.Analyse(output);

                    Assert.IsTrue(success);
                    Assert.IsTrue(File.Exists(output));
                    Assert.IsTrue(Math.Abs((outputVideo.Duration - input.Duration).TotalMilliseconds) < 1000.0 / input.PrimaryVideoStream.FrameRate);

                    if (scaling?.Size == null)
                    {
                        Assert.AreEqual(outputVideo.PrimaryVideoStream.Width, input.PrimaryVideoStream.Width);
                        Assert.AreEqual(outputVideo.PrimaryVideoStream.Height, input.PrimaryVideoStream.Height);
                    }
                    else
                    {
                        if (scaling.Size.Value.Width != -1)
                        {
                            Assert.AreEqual(outputVideo.PrimaryVideoStream.Width, scaling.Size.Value.Width);
                        }

                        if (scaling.Size.Value.Height != -1)
                        {
                            Assert.AreEqual(outputVideo.PrimaryVideoStream.Height, scaling.Size.Value.Height);
                        }

                        Assert.AreNotEqual(outputVideo.PrimaryVideoStream.Width, input.PrimaryVideoStream.Width);
                        Assert.AreNotEqual(outputVideo.PrimaryVideoStream.Height, input.PrimaryVideoStream.Height);
                    }
                }
            }
            finally
            {
                if (File.Exists(output))
                {
                    File.Delete(output);
                }
            }
        }
示例#2
0
        public void ConvertFromPipe(ContainerFormat type, System.Drawing.Imaging.PixelFormat fmt, params IArgument[] inputArguments)
        {
            var output = Input.OutputLocation(type);

            try
            {
                var videoFramesSource = new RawVideoPipeSource(BitmapSource.CreateBitmaps(128, fmt, 256, 256));
                var arguments         = FFMpegArguments.FromPipe(videoFramesSource);
                foreach (var arg in inputArguments)
                {
                    arguments.WithArgument(arg);
                }
                var processor = arguments.OutputToFile(output);

                var scaling = arguments.Find <ScaleArgument>();
                processor.ProcessSynchronously();

                var outputVideo = FFProbe.Analyse(output);

                Assert.IsTrue(File.Exists(output));

                if (scaling?.Size == null)
                {
                    Assert.AreEqual(outputVideo.PrimaryVideoStream.Width, videoFramesSource.Width);
                    Assert.AreEqual(outputVideo.PrimaryVideoStream.Height, videoFramesSource.Height);
                }
                else
                {
                    if (scaling.Size.Value.Width != -1)
                    {
                        Assert.AreEqual(outputVideo.PrimaryVideoStream.Width, scaling.Size.Value.Width);
                    }

                    if (scaling.Size.Value.Height != -1)
                    {
                        Assert.AreEqual(outputVideo.PrimaryVideoStream.Height, scaling.Size.Value.Height);
                    }

                    Assert.AreNotEqual(outputVideo.PrimaryVideoStream.Width, videoFramesSource.Width);
                    Assert.AreNotEqual(outputVideo.PrimaryVideoStream.Height, videoFramesSource.Height);
                }
            }
            finally
            {
                if (File.Exists(output))
                {
                    File.Delete(output);
                }
            }
        }
示例#3
0
        public void Video_TranscodeInMemory()
        {
            using var resStream = new MemoryStream();
            var reader = new StreamPipeSink(resStream);
            var writer = new RawVideoPipeSource(BitmapSource.CreateBitmaps(128, System.Drawing.Imaging.PixelFormat.Format24bppRgb, 128, 128));

            FFMpegArguments
            .FromPipe(writer)
            .WithVideoCodec("vp9")
            .ForceFormat("webm")
            .OutputToPipe(reader)
            .ProcessSynchronously();

            resStream.Position = 0;
            var vi = FFProbe.Analyse(resStream);

            Assert.AreEqual(vi.PrimaryVideoStream.Width, 128);
            Assert.AreEqual(vi.PrimaryVideoStream.Height, 128);
        }
        public async Task <bool> WriteVideo()
        {
            if (_outputFileName == string.Empty)
            {
                throw new Exception($"OutputFileName was {_outputFileName}");
            }

            try
            {
                {
                    var pipe      = new StreamPipeSource(await _inputVideo.ToMemoryStreamAsync());
                    var stopWatch = new Stopwatch();
                    Log.Information($"Starting writing input video to {this._outputFileName}");

                    stopWatch.Start();
                    await FFMpegArguments
                    .FromPipe(pipe)
                    .WithVideoCodec(VideoCodec.LibX264)
                    .WithConstantRateFactor(21)
                    .WithAudioCodec(AudioCodec.Aac)
                    .WithVariableBitrate(4)
                    .WithFastStart()
                    .Scale(VideoSize.Hd)
                    .OutputToFile(this._outputFileName)
                    .ProcessAsynchronously();

                    Log.Information($"Finished writing video to {this._outputFileName} in " +
                                    $"{stopWatch.ElapsedMilliseconds:n3}ms");
                    stopWatch.Stop();
                    return(true);
                }
            }
            catch (Exception ex)
            {
                Log.Error(ex, "Error in VideoEncoderEngine.WriteVideo()");

                return(false);
            }
        }
示例#5
0
        public async Task Video_Cancel_Async()
        {
            await using var resStream = new MemoryStream();
            var reader = new StreamPipeSink(resStream);
            var writer = new RawVideoPipeSource(BitmapSource.CreateBitmaps(512, System.Drawing.Imaging.PixelFormat.Format24bppRgb, 128, 128));

            var task = FFMpegArguments
                       .FromPipe(writer)
                       .WithVideoCodec("vp9")
                       .ForceFormat("webm")
                       .OutputToPipe(reader)
                       .CancellableThrough(out var cancel)
                       .ProcessAsynchronously(false);

            await Task.Delay(300);

            cancel();

            var result = await task;

            Assert.IsFalse(result);
        }