Using parallel task library (C # .NET 4.0) with external exe (ffmpeg) without conflicts

I tried to solve the problem for several days. I am new to multithreading. My goal is to simultaneously run several tasks using video encoding ffmpeg.exeand use the full power of the server.

I have a C # shell that starts the process ffmpeg.exeand works without streaming (or only with internal ffmpeg threading (not available for flv coding)), which looks like this:

using (Process process = new Process())
{
    process.StartInfo.FileName = encoderPath + "ffmpeg.exe";

    process.StartInfo.UseShellExecute = false;
    process.StartInfo.RedirectStandardOutput = true;
    process.StartInfo.RedirectStandardError = true;
    process.StartInfo.CreateNoWindow = false;

    string arguments = "-y -i " + filenameInput + " -f " + 
        GetVideoFormatName(format) + " -vcodec " + GetVideoCodecName(codec);

    // (most argument setup has been omitted for brevity)
    arguments += " " + filenameOutput + " ";

    process.StartInfo.Arguments = arguments;
    process.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
    process.Start();

    bool succes = LireSortie(process);
    process.WaitForExit();
    process.Close();
    return succes;
}

The code below calls the shell. The second parameter for each method Encodeis the number of threads used for ffmpeg internal threading. When I turn it off, it does not work.

var fm = new FFMpegWrapper();

fm.FilenameInput = "test.mp4";
//VideoInfo videoinfo = fm.GetVideoInfo();
Task[] tasks = {
    Task.Factory.StartNew(
        new Action(()=>{ fm.Encodeto200p("test200p.mp4", 4); })),
    Task.Factory.StartNew(
        new Action(()=>{ fm.EncodetoFlash200p("test200p.flv"); })),
    // ... (calls to other Encode methods ommitted) ...
    Task.Factory.StartNew(
        new Action(()=>{ fm.Encodeto404p("test404p.mp4", 4); })),
    Task.Factory.StartNew(
        new Action(()=>{ fm.EncodetoFlash404p("test404p.flv"); })),
    Task.Factory.StartNew(
        new Action(()=>{ fm.Encodeto720p("test720p.mp4", 4); }))
};

Task.WaitAll(tasks, 5000);

, , 5000 WaitAll(). , , TPL . ffmpeg.exe "" 0% CPU.

, TPL Process . TPL, "Running" . ffmpeg TPL ( - ), .

+3
1

, TPL , , . , , , - .

, , , , , IO . , ( ), , .

, , , , , . , (, ), .

0

Source: https://habr.com/ru/post/1781265/


All Articles