예제 #1
0
        public Task ProcessAsync(IReadOnlyList <string> inputFilePaths,
                                 string outputFilePath, string format, bool transcode, IEnumerable <string> additionalArgs = null,
                                 IProgress <double> progress = null, CancellationToken cancellationToken = default(CancellationToken))
        {
            var args = new List <string>();

            // Set input files
            foreach (var inputFilePath in inputFilePaths)
            {
                args.Add($"-i \"{inputFilePath}\"");
            }

            // Set output format
            args.Add($"-f {format}");

            // Skip transcoding if it's not required
            if (!transcode)
            {
                args.Add("-c copy");
            }

            // Optimize mp4 transcoding
            if (transcode && string.Equals(format, "mp4", StringComparison.OrdinalIgnoreCase))
            {
                args.Add("-preset ultrafast");
            }

            // Set max threads
            args.Add($"-threads {Environment.ProcessorCount}");

            // Disable stdin so that the process will not hang waiting for user input
            args.Add("-nostdin");

            // Trim streams to shortest
            args.Add("-shortest");

            if (additionalArgs != null)
            {
                args.AddRange(additionalArgs);
            }

            // Overwrite files
            args.Add("-y");

            // Set output file
            args.Add($"\"{outputFilePath}\"");

            // Set up progress router
            var progressRouter = new FfmpegProgressRouter(progress);

            // Run CLI
            return(Cli.Wrap(_ffmpegFilePath)
                   .WithWorkingDirectory(Directory.GetCurrentDirectory())
                   .WithArguments(args.JoinToString(" "))
                   .WithStandardErrorPipe(PipeTarget.ToDelegate((line) => progressRouter.ProcessLine(line))) // handle stderr to parse and route progress
                   .WithValidation(CommandResultValidation.None)                                             // disable stderr validation because ffmpeg writes progress there
                   .ExecuteAsync(cancellationToken));
        }
예제 #2
0
        public Task ProcessAsync(IReadOnlyList <string> inputFilePaths,
                                 string outputFilePath, string format, bool transcode,
                                 IProgress <double>?progress = null, CancellationToken cancellationToken = default)
        {
            var args = new List <string>();

            // Set input files
            foreach (var inputFilePath in inputFilePaths)
            {
                args.Add($"-i \"{inputFilePath}\"");
            }

            // Set output format
            args.Add($"-f {format}");

            // Skip transcoding if it's not required
            if (!transcode)
            {
                args.Add("-c copy");
            }

            //args.Add("libx264");

            // Optimize mp4 transcoding
            if (transcode && string.Equals(format, "mp4", StringComparison.OrdinalIgnoreCase))
            {
                args.Add("-preset veryfast");
            }

            // Set max threads
            args.Add($"-threads {Environment.ProcessorCount}");

            // Disable stdin so that the process will not hang waiting for user input
            args.Add("-nostdin");

            // Trim streams to shortest
            args.Add("-shortest");

            // Overwrite files
            args.Add("-y");

            // Set output file
            args.Add($"\"{outputFilePath}\"");

            // Set up progress router
            var progressRouter = new FfmpegProgressRouter(progress);

            // Run CLI
            return(new Cli(_ffmpegFilePath)
                   .SetWorkingDirectory(Directory.GetCurrentDirectory())
                   .SetArguments(args.JoinToString(" "))
                   .SetStandardErrorCallback(progressRouter.ProcessLine) // handle stderr to parse and route progress
                   .SetCancellationToken(cancellationToken)
                   .EnableExitCodeValidation()
                   .EnableStandardErrorValidation(false) // disable stderr validation because ffmpeg writes progress there
                   .ExecuteAsync());
        }