Exemple #1
0
        public async Task ExecuteInstructionAsync()
        {
            var instruction = GetInstruction();

            var ffmpeg = new FFmpeg();

            ffmpeg.Progress += e => { Debug.WriteLine(e); };
            await ffmpeg.ExecuteAsync(instruction);
        }
Exemple #2
0
        public async Task ConvertFromAacToMp3Async()
        {
            var ffmpeg = new FFmpeg();

            ffmpeg.Progress += e => { Debug.WriteLine(e); };

            var files = Directory.EnumerateFiles(@"", "*.aac").Select(x => new FileInfo(x));

            foreach (var file in files)
            {
                var instruction = new InstructionRoot()
                                  .AddInput(new Input(file.FullName))
                                  .AddOutput(new Output(file.FullName.Replace(".aac", ".mp3")))
                                  .GetInstruction();

                await ffmpeg.ExecuteAsync(instruction);
            }
        }
Exemple #3
0
    private async ValueTask ProcessAsync(
        string filePath,
        Container container,
        IReadOnlyList <StreamInput> streamInputs,
        IReadOnlyList <SubtitleInput> subtitleInputs,
        IProgress <double>?progress         = null,
        CancellationToken cancellationToken = default)
    {
        var arguments = new ArgumentsBuilder();

        // Stream inputs
        foreach (var streamInput in streamInputs)
        {
            arguments.Add("-i").Add(streamInput.FilePath);
        }

        // Subtitle inputs
        foreach (var subtitleInput in subtitleInputs)
        {
            arguments.Add("-i").Add(subtitleInput.FilePath);
        }

        // Format
        arguments.Add("-f").Add(container.Name);

        // Preset
        arguments.Add("-preset").Add(_preset);

        // Mapping
        for (var i = 0; i < streamInputs.Count + subtitleInputs.Count; i++)
        {
            arguments.Add("-map").Add(i);
        }

        // Avoid transcoding if possible
        if (streamInputs.All(s => s.Info.Container == container))
        {
            arguments
            .Add("-c:a").Add("copy")
            .Add("-c:v").Add("copy");
        }

        // MP4: specify subtitle codec manually, otherwise they're not injected
        if (container == Container.Mp4 && subtitleInputs.Any())
        {
            arguments.Add("-c:s").Add("mov_text");
        }

        // MP3: specify bitrate manually, otherwise the metadata will contain wrong duration
        // https://superuser.com/questions/892996/ffmpeg-is-doubling-audio-length-when-extracting-from-video
        if (container == Container.Mp3)
        {
            arguments.Add("-b:a").Add("165k");
        }

        // Inject language metadata for subtitles
        for (var i = 0; i < subtitleInputs.Count; i++)
        {
            arguments
            .Add($"-metadata:s:s:{i}")
            .Add($"language={subtitleInputs[i].Info.Language.Code}")
            .Add($"-metadata:s:s:{i}")
            .Add($"title={subtitleInputs[i].Info.Language.Name}");
        }

        // Misc settings
        arguments
        .Add("-threads").Add(Environment.ProcessorCount)
        .Add("-nostdin")
        .Add("-y");

        // Output
        arguments.Add(filePath);

        // Run FFmpeg
        await _ffmpeg.ExecuteAsync(arguments.Build(), progress, cancellationToken);
    }