FFMpeg gets stuck while using c# but not shell

I’m working on a C# project where I need to run FFmpeg commands to process videos. The FFmpeg command works perfectly fine when executed directly in the shell, but it hangs when run from my C# application. I suspect the issue is related to the output pipes getting full.

Here is the FFmpeg command I’m using:

shell
C:pathtoffmpeg.exe -f concat -safe 0 -i "C:pathtolist.txt" -vf "scale=1080x1920,setsar=1" -c:v libx264 -pix_fmt yuv420p -c:a aac -ar 44100 -ac 2 "C:pathtooutput.mp4"

And here is my C# code:

private static async Task _ExecuteFfmpegCommand(string arguments) {
    const string EXECUTABLE_NAME = "ffmpeg.exe";
    var executableFile = FfmpegPath.File(EXECUTABLE_NAME);
    await DownloadSercvice.EnsureFileExist(executableFile, FfmpegURI);
    arguments = $"-loglevel quiet {arguments}";

    using var process = new Process {
        StartInfo = new ProcessStartInfo {
            FileName = executableFile.FullName,
            Arguments = arguments,
            UseShellExecute = false,
            CreateNoWindow = true,
            RedirectStandardOutput = true,
            RedirectStandardError = true
        }
    };

    var command = $"{process.StartInfo.FileName} {process.StartInfo.Arguments}";
    Console.WriteLine($"This is my process command: {command}");

    var outputTask = new TaskCompletionSource<string>();
    var errorTask = new TaskCompletionSource<string>();

    process.OutputDataReceived += (sender, e) => {
        if (e.Data == null)            
            outputTask.SetResult(null);            
        else            
            Console.WriteLine(e.Data);            
    };

    process.ErrorDataReceived += (sender, e) => {
        if (e.Data == null)            
            errorTask.SetResult(null);            
        else            
            Console.WriteLine(e.Data);            
    };

    process.Start();
    process.BeginOutputReadLine();
    process.BeginErrorReadLine();

    await process.WaitForExitAsync();

    var output = await outputTask.Task;
    var error = await errorTask.Task;

    if (process.ExitCode != 0 || !string.IsNullOrWhiteSpace(error)) {
        throw new InvalidOperationException($"FFMPEG Error(0x{process.ExitCode:X8}): {error}") {
            Data = {
            { "Output", output },
            { "Error", error },
            { "ExitCode", process.ExitCode }
        }
        };
    }
}

Issue:
The process hangs and does not complete execution. I suspect the FFmpeg process’s output pipes (stdout and stderr) are getting full, causing the hang. I’ve tried setting the log level to error, but it didn’t resolve the issue.
I got that info from here: ffmpeg hangs when run in background
Solutions Tried:

Set -loglevel error in the FFmpeg command to reduce verbosity.
Set -loglever quiet in the FFmpeg command (to test if that may work)

Handled OutputDataReceived and ErrorDataReceived events to read the output asynchronously.
Questions:

How can I prevent the FFmpeg process from hanging due to full output pipes?
Are there better ways to handle the stdout and stderr of the FFmpeg process in C#?
Any help or suggestions on how to resolve this issue would be greatly appreciated!

New contributor

Internal is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật