ดักจาก process.Exited แล้วให้รันไฟล์ต่อไป ก็ไม่ยอม ครับ
มันจะรันทีละ หลายๆไฟล์ ทำให้เครื่องหน่วงไปเลย
[Spoil] คลิกเพื่อดูข้อความที่ซ่อนไว้ protected override void OnDoWork(DoWorkEventArgs args)
{
_progress = 0;
//setup cancelation
ParallelOptions po = new ParallelOptions
{
CancellationToken = _cts.Token,
MaxDegreeOfParallelism = Environment.ProcessorCount
};
try
{
// Parallal execution
try
{
Parallel.ForEach(_playlist, po, item =>
{
try
{
po.CancellationToken.ThrowIfCancellationRequested();
string destinationFilePathWithoutExtension = null;
string tempFilePathWithoutExtension = null;
item.DownloadProgress = 5;
if (!string.IsNullOrWhiteSpace(item.Name))
{
YoutubeLink youtubeLink = YoutubeSearcher.GetYoutubeLinks(item.Name).FirstOrDefault();
item.FileName = MakeValidFileName(youtubeLink.Label);
item.FileName = string.Concat(youtubeLink.Label.Split(Path.GetInvalidFileNameChars())).Trim().Replace('–', '-');
var workingFolder = Path.GetTempPath();
tempFilePathWithoutExtension = Path.Combine(Path.GetTempPath(), item.FileName);
destinationFilePathWithoutExtension = Path.Combine(_runSettings.SongsFolder, item.FileName);
item.DownloadProgress = 10;
if (youtubeLink == null)
{
item.SetDownloadStatus(false);
}
else
{
StartProcess(
_runSettings.YoutubeDlPath,
string.Format(" --ffmpeg-location \"{0}\"" +
" --no-part " +
" --ignore-errors " +
" --no-post-overwrites " +
" --output \"{1}\"" +
" {2}", _runSettings.FfmpegPath, _runSettings.SongsFolder + ".%(ext)s", youtubeLink.Url),
item,
ParseYoutubeDlProgress);
}
item.DownloadProgress = 100;
_progress++;
OnProgressChanged(new ProgressChangedEventArgs(_progress * 100 / _totalSongs, null));
}
}
catch (InvalidOperationException) { } //ignore exceptions when aborting download
catch (Win32Exception) { } //ignore process exception if killed during process exiting
});
}
catch (OperationCanceledException) { }
finally
{
if (_cts != null)
{
_cts.Dispose();
_cts = null;
}
}
// Serial execution
//foreach (PlaylistItem item in _playlist)
//{
// try
// {
// DownloadPlaylistItem(item, po);
// //ConvertPlaylistItem(item, po);
// }
// catch (InvalidOperationException) { } //ignore exceptions when aborting download
// catch (Win32Exception) { } //ignore process exception if killed during process exiting
//}
}
catch (OperationCanceledException) { } //ignore exceptions caused by canceling paralel.foreach loop
}
private Task<string> StartProcess(string executablePath, string arguments, PlaylistItem item, Action<string, PlaylistItem> parseProgressFunc)
{
var promise = new TaskCompletionSource<string>);
logger.Info("[RUN CMD] " + executablePath + arguments);
Process process = new Process
{
StartInfo =
{
FileName = executablePath,
Arguments = arguments,
CreateNoWindow = !_runSettings.IsDebug,
WindowStyle = _runSettings.IsDebug ? ProcessWindowStyle.Normal : ProcessWindowStyle.Hidden,
RedirectStandardOutput = true,
RedirectStandardError = true,
UseShellExecute = false
},
EnableRaisingEvents = true
};
// --audio-quality 5 --extract-audio --audio-format mp3 -o "c:\Users\Julian\Music\PlaylistDownloader\\%(title)s.%(ext)s" https://www.youtube.com/watch?v=mDuElaL1dU0
process.OutputDataReceived += (object sender, DataReceivedEventArgs e) =>
{
string consoleLine = e.Data;
if (!string.IsNullOrWhiteSpace(consoleLine))
{
logger.Info(consoleLine);
parseProgressFunc(consoleLine, item);
}
if (CancellationPending)
{
logger.Info("Canceling process because of user: " + executablePath);
process.Close();
promise.SetResult(null);
}
};
process.ErrorDataReceived += (object sender, DataReceivedEventArgs e) =>
{
string consoleLine = e.Data;
if (!string.IsNullOrWhiteSpace(consoleLine))
{
logger.Info("Error: " + consoleLine);
parseProgressFunc(consoleLine, item);
}
if (CancellationPending)
{
logger.Info("Canceling process because of user: " + executablePath);
process.Close();
promise.SetResult(null);
}
};
process.Exited += new EventHandler((object sender, EventArgs e) =>
{
process.Dispose();
logger.Info("Closing process");
promise.SetResult(null);
});
process.Start();
process.BeginOutputReadLine();
process.BeginErrorReadLine();
return promise.Task;
}
C# Process/youtube-dl มันไม่โหลดไปทีละไฟล์ ครับ
มันจะรันทีละ หลายๆไฟล์ ทำให้เครื่องหน่วงไปเลย
[Spoil] คลิกเพื่อดูข้อความที่ซ่อนไว้