예제 #1
0
        public static async Task <MediaAnalysis> AnalyseAsync(System.IO.Stream stream, int outputCapacity = int.MaxValue)
        {
            var streamPipeSource = new StreamPipeSource(stream);
            var pipeArgument     = new InputPipeArgument(streamPipeSource);

            using var instance = PrepareInstance(pipeArgument.PipePath, outputCapacity);
            pipeArgument.Pre();

            var task = instance.FinishedRunning();

            try
            {
                await pipeArgument.During();
            }
            catch (IOException)
            {
            }
            finally
            {
                pipeArgument.Post();
            }
            var exitCode = await task;

            if (exitCode != 0)
            {
                throw new FFMpegException(FFMpegExceptionType.Process, $"FFProbe process returned exit status {exitCode}: {string.Join("\n", instance.OutputData)} {string.Join("\n", instance.ErrorData)}");
            }

            pipeArgument.Post();
            return(ParseOutput(pipeArgument.PipePath, instance));
        }
예제 #2
0
        public static async Task <IMediaAnalysis> AnalyseAsync(Stream stream, int outputCapacity = int.MaxValue, FFOptions?ffOptions = null)
        {
            var streamPipeSource = new StreamPipeSource(stream);
            var pipeArgument     = new InputPipeArgument(streamPipeSource);

            using var instance = PrepareInstance(pipeArgument.PipePath, outputCapacity, ffOptions ?? GlobalFFOptions.Current);
            pipeArgument.Pre();

            var task = instance.FinishedRunning();

            try
            {
                await pipeArgument.During().ConfigureAwait(false);
            }
            catch (IOException)
            {
            }
            finally
            {
                pipeArgument.Post();
            }
            var exitCode = await task.ConfigureAwait(false);

            if (exitCode != 0)
            {
                throw new FFProbeProcessException($"ffprobe exited with non-zero exit-code ({exitCode} - {string.Join("\n", instance.ErrorData)})", instance.ErrorData);
            }

            pipeArgument.Post();
            return(ParseOutput(instance));
        }
예제 #3
0
        public static IMediaAnalysis?Analyse(Stream stream, int outputCapacity = int.MaxValue)
        {
            var streamPipeSource = new StreamPipeSource(stream);
            var pipeArgument     = new InputPipeArgument(streamPipeSource);

            using var instance = PrepareInstance(pipeArgument.PipePath, outputCapacity);
            pipeArgument.Pre();

            var task = instance.FinishedRunning();

            try
            {
                pipeArgument.During().ConfigureAwait(false).GetAwaiter().GetResult();
            }
            catch (IOException) { }
            finally
            {
                pipeArgument.Post();
            }
            var exitCode = task.ConfigureAwait(false).GetAwaiter().GetResult();

            if (exitCode != 0)
            {
                throw new FFMpegException(FFMpegExceptionType.Process, $"FFProbe process returned exit status {exitCode}", null, string.Join("\n", instance.ErrorData), string.Join("\n", instance.OutputData));
            }

            return(ParseOutput(pipeArgument.PipePath, instance));
        }
예제 #4
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);
                }
            }
        }
        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);
            }
        }