public void start() { String filename; if (downloadNo == 0) { //写入m3u8文件 String fileContent = File.ReadAllText(m3u8.file.FullName); //读取所有内容 Regex regFileContent = new Regex(@".*//(.*?.ts).*"); string result = regFileContent.Replace(fileContent, "$1"); regFileContent = new Regex(@"URI="".*"""); result = regFileContent.Replace(result, @"URI=""key.key"""); if (!Directory.Exists(@"Downloads\\" + m3u8.name)) { Directory.CreateDirectory(@"Downloads\\" + m3u8.name); } File.WriteAllText(@".\Downloads\" + m3u8.name + "\\" + m3u8.name + ".m3u8", result); } Regex reg = new Regex(@".*/(.*\.ts).*"); Match m = reg.Match(m3u8.DownloadUrl[downloadNo]); if (m.Success) { filename = m.Result("$1"); } else { filename = m3u8.name + "_" + downloadNo + ".ts"; } Aria2DownloadCallBack cb = new Aria2DownloadCallBack(OnDataReceived); Aria2Download mydownload = new Aria2Download(m3u8.DownloadUrl[downloadNo], "Downloads\\" + m3u8.name + "\\" + filename, cb); mydownload.Start(); Console.WriteLine("Download start"); }
public void OnDataReceived(Aria2Download.Status status, int progress) { // dt.Rows[0][4] = "当前进度: " + progress + "%"; this.status = status; switch (status) { case Aria2Download.Status.Finished: //下载完成 downloadNo++; if (downloadNo < m3u8.DownloadUrl.Count) { //下载下一个 if (callBack != null) { callBack(Aria2Download.Status.Running, id, "下载:" + (downloadNo - 1).ToString() + "/" + m3u8.DownloadUrl.Count + " " + progress + "%"); } start(); } else if (downloadNo == m3u8.DownloadUrl.Count) { if (m3u8.keyPath.StartsWith("http")) { Aria2DownloadCallBack cb = new Aria2DownloadCallBack(OnDataReceived); Aria2Download mydownload = new Aria2Download(m3u8.keyPath, "Downloads\\" + m3u8.name + "\\key.key", cb); mydownload.Start(); Console.WriteLine(m3u8.name + ":开始下载key"); } else { try { File.Copy(m3u8.keyPath, m3u8.name + "\\key.key", true); if (automerge) { this.status = Aria2Download.Status.Running; if (callBack != null) { callBack(status, id, "下载完成 " + m3u8.DownloadUrl.Count + "/" + m3u8.DownloadUrl.Count); } m3u8Merge(); } else { this.status = Aria2Download.Status.Finished; if (callBack != null) { callBack(status, id, "下载完成 " + m3u8.DownloadUrl.Count + "/" + m3u8.DownloadUrl.Count); } } } catch { this.status = Aria2Download.Status.Failed; if (callBack != null) { callBack(status, id, "下载Key失败 " + m3u8.DownloadUrl.Count + "/" + m3u8.DownloadUrl.Count); } } } } else if (downloadNo > m3u8.DownloadUrl.Count) { if (automerge) { this.status = Aria2Download.Status.Running; if (callBack != null) { callBack(status, id, "下载完成 " + m3u8.DownloadUrl.Count + "/" + m3u8.DownloadUrl.Count); } m3u8Merge(); } else { this.status = Aria2Download.Status.Finished; if (callBack != null) { callBack(status, id, "下载完成 " + m3u8.DownloadUrl.Count + "/" + m3u8.DownloadUrl.Count); } } } break; case Aria2Download.Status.Running: if (callBack != null) { callBack(status, id, "下载:" + downloadNo + "/" + m3u8.DownloadUrl.Count + " " + progress + "%"); } break; case Aria2Download.Status.Failed: if (callBack != null) { callBack(status, id, "失败!" + downloadNo + "/" + m3u8.DownloadUrl.Count); } break; case Aria2Download.Status.Paused: if (callBack != null) { callBack(status, id, "下载暂停 " + downloadNo + "/" + m3u8.DownloadUrl.Count + " " + progress + "%"); } break; } Console.WriteLine("当前进度: " + progress + "%"); }
void DownloadNextFile1(object state) { if (System.Threading.Thread.CurrentThread.ManagedThreadId == mainThreadId) { Console.WriteLine("主线程" + partId); } else { Console.WriteLine("子线程" + partId); } if (fileId >= m3u8FileList.Count) { return; } if (partId == 0) { log.AppendText("开始下载文件" + fileId + ":"); } //dataGridView1.Rows[fileId].Cells[4].Value = string.Format("{0:D3}/{1:D3}", partId, m3u8FileList[fileId].DownloadUrl.Count); m3u8FileList[fileId].stateId = partId; dataGridView1.DataSource = new List <m3u8File>(); dataGridView1.DataSource = m3u8FileList; log.AppendText(" 分片" + partId + "\n"); Console.WriteLine("开始下载文件" + m3u8FileList[fileId].name + "*************************************************"); String url = m3u8FileList[fileId].DownloadUrl[partId]; String partName = url.Substring(url.LastIndexOf('/') + 1); DownloadAllDataReceived = new Aria2DownloadCallBack(OnDownloadAllDataReceived); Aria2Download download = new Aria2Download(url, m3u8FileList[fileId].name, DownloadAllDataReceived, new FileId(fileId, partId)); download.Start(); m3u8FileList[fileId].DownloadState[partId] = 0; updateDownloadState(fileId, partId); Downloading++; partId++; if (partId >= m3u8FileList[fileId].DownloadUrl.Count) { partId = 0; Status res = Status.Finished; for (int i = 0; i < m3u8FileList[fileId].DownloadState.Count; i++) { if (m3u8FileList[fileId].DownloadState[i] == (int)Status.Failed) { res = Status.Failed; break; } } try { m3u8FileList[fileId].stateId = (int)res; m3u8FileList[fileId].stateId = 0; } catch (Exception) { //throw; } fileId++; dataGridView1.DataSource = new List <m3u8File>(); dataGridView1.DataSource = m3u8FileList; } }