2017-02-28 1 views
0

youtube-dl python 스크립트를 사용하여 YouTube에서 다운로드하려면 아래 코드를 사용하고 있습니다. 내 클라이언트 측 진행률 표시 줄에 표시를위한 YouTube - DL 스크립트의 비율을 갖고 싶어하기 때문에 프로세스 대기 종료가 작동하지 않습니다.

string pythonPath = @"C:\Python35\python.exe"; 
string ydl = @"C:\Y\ydl\youtube-dl"; 
string tempLocation = Server.MapPath("/ydl/"); 

string Output = ""; 
string Error = ""; 

int numOutputLines = 0; 
int numErrorLines = 0; 

using (Process process = new Process()) 
{ 
    process.EnableRaisingEvents = true; 
    process.StartInfo.ErrorDialog = false; 
    process.StartInfo.RedirectStandardError = true; 
    process.StartInfo.FileName = pythonPath; 
    process.StartInfo.WorkingDirectory = tempLocation; 
    process.StartInfo.Arguments = ydl + " --output test.mp4 --force-ipv4 -f bestvideo[ext=mp4]+bestaudio[ext=m4a] \"" + Url + "\""; 
    process.StartInfo.Verb = "runas"; 
    process.StartInfo.UseShellExecute = false; 
    process.StartInfo.CreateNoWindow = false; 
    process.StartInfo.RedirectStandardOutput = true; 
    process.StartInfo.RedirectStandardError = true; 

    StringBuilder output = new StringBuilder(); 
    StringBuilder error = new StringBuilder(); 

    using (AutoResetEvent outputWaitHandle = new AutoResetEvent(false)) 
    using (AutoResetEvent errorWaitHandle = new AutoResetEvent(false)) 
    { 
     process.OutputDataReceived += (sender, e) => 
     { 
      if (e.Data == null) 
      { 
       outputWaitHandle.Set(); 
      } 
      else 
      { 
       numOutputLines++; 
       this.Context.Response.Write(Environment.NewLine + "[" + numOutputLines.ToString() + "] - " + e.Data); 
       output.AppendLine("[" + numOutputLines.ToString() + "] - " + e.Data); 
      } 
     }; 
     process.ErrorDataReceived += (sender, e) => 
     { 
      if (e.Data == null) 
      { 
       errorWaitHandle.Set(); 
      } 
      else 
      { 
       numErrorLines++; 
       this.Context.Response.Write(Environment.NewLine + "[" + numErrorLines.ToString() + "] - " + e.Data); 
       error.AppendLine("[" + numErrorLines.ToString() + "] - " + e.Data); 
      } 
     }; 

     //process.Exited += (s, a) => 
     //{ 
     // process.Close(); 
     //}; 

     process.Start(); 

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

      //process.WaitForExit(); 
      Process[] curProcess = Process.GetProcessesByName("youtube-dl"); 
      Process youtubeProcess = curProcess.FirstOrDefault(); 

      while (!youtubeProcess.HasExited) 
      { 
       Thread.Sleep(100); 
      } 

     Output = output.ToString(); 
     Error = error.ToString(); 
     process.Close(); 
    } 
} 


나는 이런 식으로 였는지를 사용했다.
하지만 문제가 있으며 WaitForExit이 작동하지 않습니다. 나는이 문제가 자식 프로세스를 위해 작동하지 않는 과정에서 기다리는 것과 관련된 다른 주제를 읽었다. (내 말은, exit는 python이 youtube-dl 스크립트가 아니라는 것을 의미한다)
어떻게해야합니까? 당신이 자식 프로세스에 관심이 있기 때문에

+0

은 "아니다 의미? –

+0

@ RenéVogt 프로세스가 끝날 때까지 기다리지 않습니다. – parsa

답변

1

어쩌면 당신이 방법을 사용하여 유튜브 과정에 폴링하는 시도는이 같은

Process.GetProcessesByName(string processName); 

뭔가 : _exactly_이 무엇

Process[] curProcess = Process.GetProcessesByName("your youtube process name"); 
    Process youtubeProcess = curProcess.FirstOrDefault(); // Get here the right process instance 
    while (!youtubeProcess.HasExited) 
    { 
     Thread.Sleep(100); 
    } 
+0

나는 당신의 언급대로하지만 여전히 작동하지 않습니다. 나는'youtubeProcess' 값을 출력합니다, 그것은 비어 있습니다. 내 코드를 편집하고'process.StartInfo.Arguments'.please를 업데이트하십시오. 만약 당신이 알고 있다면 무엇이 잘못 됐는지 말씀해주십시오. – parsa

+0

프로세스 이름이 'youtube-dl'입니까? (당신은 작업 관리자에 나타나는 이름을 가져 가야합니다.) – TeaHoney

+0

작업 관리자를 보면 프로세스와 관련된 프로세스 이름이 ** conhost.exe **와 ** python.exe **입니다. – parsa

관련 문제