void Main(object value) //主下載功能 不要用主線程來執行 { Model.BahaModel Baha = (Model.BahaModel)value; Baha.IsIng = true; if (Local.ProxyIP != "" && Local.ProxyPort != 0) //如果有設定Proxy { WebRequest.Proxy = new System.Net.WebProxy(Local.ProxyIP, Local.ProxyPort); if (Local.ProxyUser != "") { System.Net.ServicePointManager.Expect100Continue = false; WebRequest.Proxy.UseDefaultCredentials = true; WebRequest.Proxy.Credentials = new System.Net.NetworkCredential(Local.ProxyUser, Local.ProxyPass); } } try { Baha.DeviceId = WebRequest.GetDeviceId(Baha.SN); if (Baha.DeviceId == "") { Baha.Status = "無法取得DeviceId"; Baha.IsIng = false; Baha.IsStop = true; 處理下載(); return; } if (!WebRequest.GainAccess(Baha.DeviceId, Baha.SN)) { Baha.Status = "無法取得GainAccess"; Baha.IsIng = false; Baha.IsStop = true; 處理下載(); return; } WebRequest.Unlock(Baha.SN); WebRequest.CheckLock(Baha.DeviceId, Baha.SN); WebRequest.Unlock(Baha.SN); WebRequest.Unlock(Baha.SN); WebRequest.StartAd(Baha.SN); for (int i = 8; i > 0; i--) { Baha.Status = "等待" + i.ToString() + "秒跳過廣告..."; if (!VideoList.Contains(Baha)) { 處理下載(); return; } Thread.Sleep(1000); } WebRequest.SkipAd(Baha.SN); Baha.Status = "解析中"; WebRequest.VideoStart(Baha.SN); WebRequest.CheckNoAd(Baha.DeviceId, Baha.SN); Baha.Url = WebRequest.GetM3U8(Baha.DeviceId, Baha.SN); Baha.Res = WebRequest.ParseMasterList(Baha.Url, Baha.SN, Baha.Quality); if (Baha.Res == "") { this.Dispatcher.BeginInvoke(new Action(() => { WPFMessageBox.Show("資源清單裡找不到" + Baha.Quality + "P 畫質的影片"); })); Baha.IsIng = false; Baha.IsStop = true; 處理下載(); return; } Baha.Tmp = "Tmp\\tmp" + Baha.SN; String Path = AppDomain.CurrentDomain.BaseDirectory + Baha.Tmp; if (!Directory.Exists(Path)) { Directory.CreateDirectory(Path); } if (Baha.ChuckList == null || !File.Exists(Path + "\\" + Baha.Res)) // 如果己經有Chuck 就不用再次下載 { FileStream file = new FileStream(Path + "\\" + Baha.Res, FileMode.Create); Baha.ChuckList = new List <string>(); string KeyUri = WebRequest.DownloadM3U8(Baha.Url.Replace("playlist.m3u8", Baha.Res), Baha.SN, file, Baha.ChuckList); FileStream fileKey = new FileStream(Path + "\\" + Baha.Res + "key", FileMode.Create); WebRequest.Download(KeyUri, Baha.SN, fileKey); } String prefix = Baha.Url.Remove(Baha.Url.IndexOf("playlist.m3u8")); Baha.BarMax = Baha.ChuckList.Count; Baha.Bar = 0; String[] Files = Directory.GetFiles(Path); if (Files.Length > 3) //如果有暫存檔案 , Chuck移除減少下載 { Baha.ChuckList.Count(); foreach (String file in Files) { if (!file.EndsWith(".ts")) { continue; } var q = Baha.ChuckList.Where(I => I.Split('?')[0] == System.IO.Path.GetFileName(file)); if (q.Count() > 0) { Baha.ChuckList.Remove(q.First()); Baha.Bar++; } } } foreach (string Chuck in Baha.ChuckList) { Baha.Bar++; Baha.Status = string.Format("已下載 ({0}/{1})", Baha.Bar, Baha.BarMax); FileStream ChuckFile = new FileStream(Path + "\\" + Chuck.Remove(Chuck.IndexOf("?token=")), FileMode.Create); if (!WebRequest.Download(prefix + Chuck, Baha.SN, ChuckFile)) { Baha.IsIng = false; Baha.IsStop = true; this.Dispatcher.BeginInvoke(new Action(() => { WPFMessageBox.Show("網路異常..."); })); return; } if (!VideoList.Contains(Baha)) { DeleteSrcFolder(Path); 處理下載(); return; } } Baha.Status = "轉檔中"; System.Diagnostics.Process process = new System.Diagnostics.Process(); System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo(); startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden; startInfo.FileName = "ffmpeg.exe"; startInfo.Arguments = " -allowed_extensions ALL -y -i " + Baha.Tmp + "/" + Baha.Res + " -c copy " + Local.AniDir + "\\" + Baha.Name + ".mp4"; process.StartInfo = startInfo; process.Start(); process.WaitForExit(); if (process != null) { process.Close(); } Baha.Status = "下載完成"; Baha.IsIng = false; Baha.IsOk = true; DeleteSrcFolder(Path); 處理下載(); } catch { Baha.IsIng = false; Baha.IsStop = true; Baha.Status = "下載中出現錯誤..."; return; } }