Featured post
c# 4.0 - Using Task Parallel Library (C# .NET 4.0) with an external exe (ffmpeg) without conflicts -
i've been trying solve problem several days. beginner in multithreading. goal run several video encoding tasks simultaneously using ffmpeg.exe
, use power of server.
i've got c# wrapper launches ffmpeg.exe
process , works without threading (or ffmpeg internal threading (not available flv encoding)) looks 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 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 wrapper. second parameter of each encode
method number of threads use internal ffmpeg threading. when disable it, doesn't 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 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);
you wondering why put timeout of 5000 waitall()
. it's because calling thread waits indefinitely because tpl doesn't detect end tasks. ffmpeg.exe
processes "stop" in middle of encoding , keep running 0% of cpu.
i think tpl , process
conflicting. when status of each task tpl, remains "running" time. capture real events of ffmpeg processes tpl (or other mechanism) because, want prevent application crashing , want manage successes , failures.
i doubt problem tpl or home-grown mechanism used detect whether process ends. suspect real reason wait not indefinite classic case of thrashing due pressure on particular resource.
given description of happening , cpu appears @ low levels, pressure on io or memory. suggest profiling application using performance counters @ least (and using task manager quick on surface level) find out going on.
as commenter has suggested, makes sense eliminate tasking , determine whether one, 2 or more serial calls encode @ least work. may 1 particular call holding (e.g. encoding bug), rather resource problem.
- Get link
- X
- Other Apps
Comments
Post a Comment