/// <summary> /// 获取所有的下载中数据 /// </summary> /// <returns></returns> public List <DownloadingItem> GetDownloading() { // 从数据库获取数据 DownloadingDb downloadingDb = new DownloadingDb(); Dictionary <string, object> dic = downloadingDb.QueryAll(); //downloadingDb.Close(); // 遍历 List <DownloadingItem> list = new List <DownloadingItem>(); foreach (KeyValuePair <string, object> item in dic) { if (item.Value is Downloading downloading) { DownloadingItem downloadingItem = new DownloadingItem { DownloadBase = GetDownloadBase(item.Key), Downloading = downloading }; if (downloadingItem.DownloadBase == null) { continue; } list.Add(downloadingItem); } } return(list); }
/// <summary> /// 是否存在于下载列表中 /// </summary> /// <param name="downloading"></param> /// <returns></returns> private async Task <bool> IsExist(DownloadingItem downloading) { bool isExist = downloadingList.Contains(downloading); if (isExist) { return(true); } else { // 先恢复为waiting状态,暂停状态下Remove会导致文件重新下载,原因暂不清楚 await AriaClient.UnpauseAsync(downloading.Downloading.Gid); // 移除下载项 var ariaRemove = await AriaClient.RemoveAsync(downloading.Downloading.Gid); if (ariaRemove == null || ariaRemove.Result == downloading.Downloading.Gid) { // 从内存中删除下载项 await AriaClient.RemoveDownloadResultAsync(downloading.Downloading.Gid); } return(false); } }
/// <summary> /// 混流音频和视频 /// </summary> /// <param name="downloading"></param> /// <param name="audioUid"></param> /// <param name="videoUid"></param> /// <returns></returns> public override string MixedFlow(DownloadingItem downloading, string audioUid, string videoUid) { if (videoUid == nullMark) { return(null); } return(BaseMixedFlow(downloading, audioUid, videoUid)); }
protected string BaseDownloadDanmaku(DownloadingItem downloading) { // 更新状态显示 downloading.DownloadStatusTitle = DictionaryResource.GetString("WhileDownloading"); downloading.DownloadContent = DictionaryResource.GetString("DownloadingDanmaku"); // 下载大小 downloading.DownloadingFileSize = string.Empty; // 下载速度 downloading.SpeedDisplay = string.Empty; string title = $"{downloading.Name}"; string assFile = $"{downloading.DownloadBase.FilePath}.ass"; // 记录本次下载的文件 if (!downloading.Downloading.DownloadFiles.ContainsKey("danmaku")) { downloading.Downloading.DownloadFiles.Add("danmaku", assFile); } int screenWidth = SettingsManager.GetInstance().GetDanmakuScreenWidth(); int screenHeight = SettingsManager.GetInstance().GetDanmakuScreenHeight(); //if (SettingsManager.GetInstance().IsCustomDanmakuResolution() != AllowStatus.YES) //{ // if (downloadingEntity.Width > 0 && downloadingEntity.Height > 0) // { // screenWidth = downloadingEntity.Width; // screenHeight = downloadingEntity.Height; // } //} // 字幕配置 Config subtitleConfig = new Config { Title = title, ScreenWidth = screenWidth, ScreenHeight = screenHeight, FontName = SettingsManager.GetInstance().GetDanmakuFontName(), BaseFontSize = SettingsManager.GetInstance().GetDanmakuFontSize(), LineCount = SettingsManager.GetInstance().GetDanmakuLineCount(), LayoutAlgorithm = SettingsManager.GetInstance().GetDanmakuLayoutAlgorithm().ToString("G").ToLower(), // async/sync TuneDuration = 0, DropOffset = 0, BottomMargin = 0, CustomOffset = 0 }; Core.Danmaku2Ass.Bilibili.GetInstance() .SetTopFilter(SettingsManager.GetInstance().GetDanmakuTopFilter() == AllowStatus.YES) .SetBottomFilter(SettingsManager.GetInstance().GetDanmakuBottomFilter() == AllowStatus.YES) .SetScrollFilter(SettingsManager.GetInstance().GetDanmakuScrollFilter() == AllowStatus.YES) .Create(downloading.DownloadBase.Avid, downloading.DownloadBase.Cid, subtitleConfig, assFile); return(assFile); }
/// <summary> /// 下载失败后的处理 /// </summary> /// <param name="downloading"></param> private void DownloadFailed(DownloadingItem downloading) { downloading.DownloadStatusTitle = DictionaryResource.GetString("DownloadFailed"); downloading.DownloadContent = string.Empty; downloading.DownloadingFileSize = string.Empty; downloading.SpeedDisplay = string.Empty; downloading.Downloading.DownloadStatus = DownloadStatus.DOWNLOAD_FAILED; downloading.StartOrPause = ButtonIcon.Instance().Retry; downloading.StartOrPause.Fill = DictionaryResource.GetColor("ColorPrimary"); }
protected PlayUrlDashVideo BaseDownloadAudio(DownloadingItem downloading) { // 更新状态显示 downloading.DownloadStatusTitle = DictionaryResource.GetString("WhileDownloading"); downloading.DownloadContent = DictionaryResource.GetString("DownloadingAudio"); // 下载大小 downloading.DownloadingFileSize = string.Empty; downloading.Progress = 0; // 下载速度 downloading.SpeedDisplay = string.Empty; // 如果没有Dash,返回null if (downloading.PlayUrl == null || downloading.PlayUrl.Dash == null) { return(null); } // 如果audio列表没有内容,则返回null if (downloading.PlayUrl.Dash.Audio == null) { return(null); } else if (downloading.PlayUrl.Dash.Audio.Count == 0) { return(null); } // 根据音频id匹配 PlayUrlDashVideo downloadAudio = null; foreach (PlayUrlDashVideo audio in downloading.PlayUrl.Dash.Audio) { if (audio.Id == downloading.AudioCodec.Id) { downloadAudio = audio; break; } } // 避免Dolby==null及其它未知情况,直接使用异常捕获 try { // Dolby Atmos if (downloading.AudioCodec.Id == 30250) { downloadAudio = downloading.PlayUrl.Dash.Dolby.Audio[0]; } } catch (Exception) { } return(downloadAudio); }
/// <summary> /// 是否存在于下载列表中 /// </summary> /// <param name="downloading"></param> /// <returns></returns> private bool IsExist(DownloadingItem downloading) { bool isExist = downloadingList.Contains(downloading); if (isExist) { return(true); } else { return(false); } }
/// <summary> /// 删除下载中数据 /// </summary> /// <param name="downloadingItem"></param> public void RemoveDownloading(DownloadingItem downloadingItem) { if (downloadingItem == null || downloadingItem.DownloadBase == null) { return; } RemoveDownloadBase(downloadingItem.DownloadBase.Uuid); DownloadingDb downloadingDb = new DownloadingDb(); downloadingDb.Delete(downloadingItem.DownloadBase.Uuid); //downloadingDb.Close(); }
/// <summary> /// 修改下载中数据 /// </summary> /// <param name="downloadingItem"></param> public void UpdateDownloading(DownloadingItem downloadingItem) { if (downloadingItem == null || downloadingItem.DownloadBase == null) { return; } UpdateDownloadBase(downloadingItem.DownloadBase); DownloadingDb downloadingDb = new DownloadingDb(); downloadingDb.Update(downloadingItem.DownloadBase.Uuid, downloadingItem.Downloading); //downloadingDb.Close(); }
private void AriaTellStatus(long totalLength, long completedLength, long speed, string gid) { // 当前的下载视频 DownloadingItem video = null; try { video = downloadingList.FirstOrDefault(it => it.Downloading.Gid == gid); } catch (InvalidOperationException e) { Core.Utils.Debugging.Console.PrintLine("AriaTellStatus()发生异常: {0}", e); LogManager.Error("AriaTellStatus()", e); } if (video == null) { return; } // 下载进度百分比 float percent = 0; if (totalLength != 0) { percent = (float)completedLength / totalLength * 100; } // 根据进度判断本次是否需要更新UI if (Math.Abs(percent - video.Progress) < 0.01) { return; } // 下载进度 video.Progress = percent; // 下载大小 video.DownloadingFileSize = Format.FormatFileSize(completedLength) + "/" + Format.FormatFileSize(totalLength); // 下载速度 video.SpeedDisplay = Format.FormatSpeed(speed); // 最大下载速度 if (video.Downloading.MaxSpeed < speed) { video.Downloading.MaxSpeed = speed; } }
/// <summary> /// 下载音频,返回下载文件路径 /// </summary> /// <param name="downloading"></param> /// <returns></returns> public string DownloadAudio(DownloadingItem downloading) { // 更新状态显示 downloading.DownloadStatusTitle = DictionaryResource.GetString("WhileDownloading"); downloading.DownloadContent = DictionaryResource.GetString("DownloadingAudio"); // 如果没有Dash,返回null if (downloading.PlayUrl == null || downloading.PlayUrl.Dash == null) { return(null); } // 如果audio列表没有内容,则返回null if (downloading.PlayUrl.Dash.Audio == null) { return(null); } else if (downloading.PlayUrl.Dash.Audio.Count == 0) { return(null); } // 根据音频id匹配 PlayUrlDashVideo downloadAudio = null; foreach (PlayUrlDashVideo audio in downloading.PlayUrl.Dash.Audio) { if (audio.Id == downloading.AudioCodec.Id) { downloadAudio = audio; break; } } // 避免Dolby==null及其它未知情况,直接使用异常捕获 try { // Dolby Atmos if (downloading.AudioCodec.Id == 30250) { downloadAudio = downloading.PlayUrl.Dash.Dolby.Audio[0]; } } catch (Exception) { } return(DownloadVideo(downloading, downloadAudio)); }
/// <summary> /// 强制暂停 /// </summary> /// <param name="downloading"></param> /// <exception cref="OperationCanceledException"></exception> protected override void Pause(DownloadingItem downloading) { cancellationToken.ThrowIfCancellationRequested(); downloading.DownloadStatusTitle = DictionaryResource.GetString("Pausing"); if (downloading.Downloading.DownloadStatus == DownloadStatus.PAUSE) { throw new OperationCanceledException("Stop thread by pause"); } // 是否存在 var isExist = IsExist(downloading); if (!isExist.Result) { throw new OperationCanceledException("Task is deleted"); } }
/// <summary> /// 添加下载中数据 /// </summary> /// <param name="downloadingItem"></param> public void AddDownloading(DownloadingItem downloadingItem) { if (downloadingItem == null || downloadingItem.DownloadBase == null) { return; } AddDownloadBase(downloadingItem.DownloadBase); DownloadingDb downloadingDb = new DownloadingDb(); object obj = downloadingDb.QueryById(downloadingItem.DownloadBase.Uuid); if (obj == null) { downloadingDb.Insert(downloadingItem.DownloadBase.Uuid, downloadingItem.Downloading); } //downloadingDb.Close(); }
protected List <string> BaseDownloadSubtitle(DownloadingItem downloading) { // 更新状态显示 downloading.DownloadStatusTitle = DictionaryResource.GetString("WhileDownloading"); downloading.DownloadContent = DictionaryResource.GetString("DownloadingSubtitle"); // 下载大小 downloading.DownloadingFileSize = string.Empty; // 下载速度 downloading.SpeedDisplay = string.Empty; List <string> srtFiles = new List <string>(); var subRipTexts = VideoStream.GetSubtitle(downloading.DownloadBase.Avid, downloading.DownloadBase.Bvid, downloading.DownloadBase.Cid); if (subRipTexts == null) { return(null); } foreach (var subRip in subRipTexts) { string srtFile = $"{downloading.DownloadBase.FilePath}_{subRip.LanDoc}.srt"; try { File.WriteAllText(srtFile, subRip.SrtString); // 记录本次下载的文件 if (!downloading.Downloading.DownloadFiles.ContainsKey("subtitle")) { downloading.Downloading.DownloadFiles.Add("subtitle", srtFile); } srtFiles.Add(srtFile); } catch (Exception e) { Core.Utils.Debugging.Console.PrintLine($"{Tag}.DownloadSubtitle()发生异常: {0}", e); LogManager.Error($"{Tag}.DownloadSubtitle()", e); } } return(srtFiles); }
protected PlayUrlDashVideo BaseDownloadVideo(DownloadingItem downloading) { // 更新状态显示 downloading.DownloadStatusTitle = DictionaryResource.GetString("WhileDownloading"); downloading.DownloadContent = DictionaryResource.GetString("DownloadingVideo"); // 下载大小 downloading.DownloadingFileSize = string.Empty; downloading.Progress = 0; // 下载速度 downloading.SpeedDisplay = string.Empty; // 如果没有Dash,返回null if (downloading.PlayUrl == null || downloading.PlayUrl.Dash == null) { return(null); } // 如果Video列表没有内容,则返回null if (downloading.PlayUrl.Dash.Video == null) { return(null); } else if (downloading.PlayUrl.Dash.Video.Count == 0) { return(null); } // 根据视频编码匹配 PlayUrlDashVideo downloadVideo = null; foreach (PlayUrlDashVideo video in downloading.PlayUrl.Dash.Video) { if (video.Id == downloading.Resolution.Id && Utils.GetVideoCodecName(video.Codecs) == downloading.VideoCodecName) { downloadVideo = video; break; } } return(downloadVideo); }
protected void BaseParse(DownloadingItem downloading) { // 更新状态显示 downloading.DownloadStatusTitle = DictionaryResource.GetString("Parsing"); downloading.DownloadContent = string.Empty; // 下载大小 downloading.DownloadingFileSize = string.Empty; downloading.Progress = 0; // 下载速度 downloading.SpeedDisplay = string.Empty; if (downloading.PlayUrl != null && downloading.Downloading.DownloadStatus == DownloadStatus.NOT_STARTED) { // 设置下载状态 downloading.Downloading.DownloadStatus = DownloadStatus.DOWNLOADING; return; } // 设置下载状态 downloading.Downloading.DownloadStatus = DownloadStatus.DOWNLOADING; // 解析 switch (downloading.Downloading.PlayStreamType) { case PlayStreamType.VIDEO: downloading.PlayUrl = VideoStream.GetVideoPlayUrl(downloading.DownloadBase.Avid, downloading.DownloadBase.Bvid, downloading.DownloadBase.Cid); break; case PlayStreamType.BANGUMI: downloading.PlayUrl = VideoStream.GetBangumiPlayUrl(downloading.DownloadBase.Avid, downloading.DownloadBase.Bvid, downloading.DownloadBase.Cid); break; case PlayStreamType.CHEESE: downloading.PlayUrl = VideoStream.GetCheesePlayUrl(downloading.DownloadBase.Avid, downloading.DownloadBase.Bvid, downloading.DownloadBase.Cid, downloading.DownloadBase.EpisodeId); break; default: break; } }
protected string BaseDownloadCover(DownloadingItem downloading, string coverUrl, string fileName) { // 更新状态显示 downloading.DownloadStatusTitle = DictionaryResource.GetString("WhileDownloading"); downloading.DownloadContent = DictionaryResource.GetString("DownloadingCover"); // 下载大小 downloading.DownloadingFileSize = string.Empty; // 下载速度 downloading.SpeedDisplay = string.Empty; // 查询、保存封面 StorageCover storageCover = new StorageCover(); string cover = storageCover.GetCover(downloading.DownloadBase.Avid, downloading.DownloadBase.Bvid, downloading.DownloadBase.Cid, coverUrl); if (cover == null) { return(null); } // 复制图片到指定位置 try { File.Copy(cover, fileName, true); // 记录本次下载的文件 if (!downloading.Downloading.DownloadFiles.ContainsKey(coverUrl)) { downloading.Downloading.DownloadFiles.Add(coverUrl, fileName); } return(fileName); } catch (Exception e) { Core.Utils.Debugging.Console.PrintLine($"{Tag}.DownloadCover()发生异常: {0}", e); LogManager.Error($"{Tag}.DownloadCover()", e); } return(null); }
protected string BaseMixedFlow(DownloadingItem downloading, string audioUid, string videoUid) { // 更新状态显示 downloading.DownloadStatusTitle = DictionaryResource.GetString("MixedFlow"); downloading.DownloadContent = DictionaryResource.GetString("DownloadingVideo"); // 下载大小 downloading.DownloadingFileSize = string.Empty; // 下载速度 downloading.SpeedDisplay = string.Empty; //if (videoUid == nullMark) //{ // return null; //} string finalFile = $"{downloading.DownloadBase.FilePath}.mp4"; if (videoUid == null) { finalFile = $"{downloading.DownloadBase.FilePath}.aac"; } // 合并音视频 FFmpegHelper.MergeVideo(audioUid, videoUid, finalFile); // 获取文件大小 if (File.Exists(finalFile)) { FileInfo info = new FileInfo(finalFile); downloading.FileSize = Format.FormatFileSize(info.Length); } else { downloading.FileSize = Format.FormatFileSize(0); } return(finalFile); }
/// <summary> /// 下载视频,返回下载文件路径 /// </summary> /// <param name="downloading"></param> /// <returns></returns> public string DownloadVideo(DownloadingItem downloading) { // 更新状态显示 downloading.DownloadStatusTitle = DictionaryResource.GetString("WhileDownloading"); downloading.DownloadContent = DictionaryResource.GetString("DownloadingVideo"); // 如果没有Dash,返回null if (downloading.PlayUrl == null || downloading.PlayUrl.Dash == null) { return(null); } // 如果Video列表没有内容,则返回null if (downloading.PlayUrl.Dash.Video == null) { return(null); } else if (downloading.PlayUrl.Dash.Video.Count == 0) { return(null); } // 根据视频编码匹配 PlayUrlDashVideo downloadVideo = null; foreach (PlayUrlDashVideo video in downloading.PlayUrl.Dash.Video) { if (video.Id == downloading.Resolution.Id && Utils.GetVideoCodecName(video.Codecs) == downloading.VideoCodecName) { downloadVideo = video; break; } } return(DownloadVideo(downloading, downloadVideo)); }
public abstract string MixedFlow(DownloadingItem downloading, string audioUid, string videoUid);
protected abstract void Pause(DownloadingItem downloading);
public abstract List <string> DownloadSubtitle(DownloadingItem downloading);
public abstract string DownloadCover(DownloadingItem downloading, string coverUrl, string fileName);
public abstract string DownloadVideo(DownloadingItem downloading);
public abstract string DownloadDanmaku(DownloadingItem downloading);
public abstract string DownloadAudio(DownloadingItem downloading);
public abstract void Parse(DownloadingItem downloading);
/// <summary> /// 下载视频,返回下载文件路径 /// </summary> /// <param name="downloading"></param> /// <returns></returns> public override string DownloadVideo(DownloadingItem downloading) { PlayUrlDashVideo downloadVideo = BaseDownloadVideo(downloading); return(DownloadVideo(downloading, downloadVideo)); }
/// <summary> /// 下载一个视频 /// </summary> /// <param name="downloading"></param> /// <returns></returns> private async Task SingleDownload(DownloadingItem downloading) { // 路径 downloading.DownloadBase.FilePath = downloading.DownloadBase.FilePath.Replace("\\", "/"); string[] temp = downloading.DownloadBase.FilePath.Split('/'); //string path = downloading.DownloadBase.FilePath.Replace(temp[temp.Length - 1], ""); string path = downloading.DownloadBase.FilePath.TrimEnd(temp[temp.Length - 1].ToCharArray()); // 路径不存在则创建 if (!Directory.Exists(path)) { Directory.CreateDirectory(path); } try { await Task.Run(new Action(() => { // 初始化 downloading.DownloadStatusTitle = string.Empty; downloading.DownloadContent = string.Empty; //downloading.Downloading.DownloadFiles.Clear(); // 解析并依次下载音频、视频、弹幕、字幕、封面等内容 Parse(downloading); // 暂停 Pause(downloading); string audioUid = null; // 如果需要下载音频 if (downloading.DownloadBase.NeedDownloadContent["downloadAudio"]) { //audioUid = DownloadAudio(downloading); for (int i = 0; i < retry; i++) { audioUid = DownloadAudio(downloading); if (audioUid != null && audioUid != nullMark) { break; } } } if (audioUid == nullMark) { DownloadFailed(downloading); return; } // 暂停 Pause(downloading); string videoUid = null; // 如果需要下载视频 if (downloading.DownloadBase.NeedDownloadContent["downloadVideo"]) { //videoUid = DownloadVideo(downloading); for (int i = 0; i < retry; i++) { videoUid = DownloadVideo(downloading); if (videoUid != null && videoUid != nullMark) { break; } } } if (videoUid == nullMark) { DownloadFailed(downloading); return; } // 暂停 Pause(downloading); string outputDanmaku = null; // 如果需要下载弹幕 if (downloading.DownloadBase.NeedDownloadContent["downloadDanmaku"]) { outputDanmaku = DownloadDanmaku(downloading); } // 暂停 Pause(downloading); List <string> outputSubtitles = null; // 如果需要下载字幕 if (downloading.DownloadBase.NeedDownloadContent["downloadSubtitle"]) { outputSubtitles = DownloadSubtitle(downloading); } // 暂停 Pause(downloading); string outputCover = null; string outputPageCover = null; // 如果需要下载封面 if (downloading.DownloadBase.NeedDownloadContent["downloadCover"]) { // page的封面 string pageCoverFileName = $"{downloading.DownloadBase.FilePath}.{GetImageExtension(downloading.DownloadBase.PageCoverUrl)}"; outputPageCover = DownloadCover(downloading, downloading.DownloadBase.PageCoverUrl, pageCoverFileName); string coverFileName = $"{downloading.DownloadBase.FilePath}.Cover.{GetImageExtension(downloading.DownloadBase.CoverUrl)}"; // 封面 //outputCover = DownloadCover(downloading, downloading.DownloadBase.CoverUrl, $"{path}/Cover.{GetImageExtension(downloading.DownloadBase.CoverUrl)}"); outputCover = DownloadCover(downloading, downloading.DownloadBase.CoverUrl, coverFileName); } // 暂停 Pause(downloading); // 混流 string outputMedia = string.Empty; if (downloading.DownloadBase.NeedDownloadContent["downloadAudio"] || downloading.DownloadBase.NeedDownloadContent["downloadVideo"]) { outputMedia = MixedFlow(downloading, audioUid, videoUid); } // 这里本来只有IsExist,没有pause,不知道怎么处理 // 是否存在 //isExist = IsExist(downloading); //if (!isExist.Result) //{ // return; //} // 检测音频、视频是否下载成功 bool isMediaSuccess = true; if (downloading.DownloadBase.NeedDownloadContent["downloadAudio"] || downloading.DownloadBase.NeedDownloadContent["downloadVideo"]) { // 只有下载音频不下载视频时才输出aac // 只要下载视频就输出mp4 if (File.Exists(outputMedia)) { // 成功 isMediaSuccess = true; } else { isMediaSuccess = false; } } // 检测弹幕是否下载成功 bool isDanmakuSuccess = true; if (downloading.DownloadBase.NeedDownloadContent["downloadDanmaku"]) { if (File.Exists(outputDanmaku)) { // 成功 isDanmakuSuccess = true; } else { isDanmakuSuccess = false; } } // 检测字幕是否下载成功 bool isSubtitleSuccess = true; if (downloading.DownloadBase.NeedDownloadContent["downloadSubtitle"]) { if (outputSubtitles == null) { // 为null时表示不存在字幕 } else { foreach (string subtitle in outputSubtitles) { if (!File.Exists(subtitle)) { // 如果有一个不存在则失败 isSubtitleSuccess = false; } } } } // 检测封面是否下载成功 bool isCover = true; if (downloading.DownloadBase.NeedDownloadContent["downloadCover"]) { if (File.Exists(outputCover) || File.Exists(outputPageCover)) { // 成功 isCover = true; } else { isCover = false; } } if (!isMediaSuccess || !isDanmakuSuccess || !isSubtitleSuccess || !isCover) { DownloadFailed(downloading); return; } // 下载完成后处理 Downloaded downloaded = new Downloaded { MaxSpeedDisplay = Format.FormatSpeed(downloading.Downloading.MaxSpeed), }; // 设置完成时间 downloaded.SetFinishedTimestamp(new DateTimeOffset(DateTime.UtcNow).ToUnixTimeSeconds()); DownloadedItem downloadedItem = new DownloadedItem { DownloadBase = downloading.DownloadBase, Downloaded = downloaded }; App.PropertyChangeAsync(new Action(() => { // 加入到下载完成list中,并从下载中list去除 downloadedList.Add(downloadedItem); downloadingList.Remove(downloading); // 下载完成列表排序 DownloadFinishedSort finishedSort = SettingsManager.GetInstance().GetDownloadFinishedSort(); App.SortDownloadedList(finishedSort); })); })); } catch (OperationCanceledException e) { Core.Utils.Debugging.Console.PrintLine(Tag, e.ToString()); LogManager.Debug(Tag, e.Message); } }
/// <summary> /// 将下载音频和视频的函数中相同代码抽象出来 /// </summary> /// <param name="downloading"></param> /// <param name="downloadVideo"></param> /// <returns></returns> private string DownloadVideo(DownloadingItem downloading, PlayUrlDashVideo downloadVideo) { // 如果为空,说明没有匹配到可下载的音频视频 if (downloadVideo == null) { return(null); } // 下载链接 List <string> urls = new List <string>(); if (downloadVideo.BaseUrl != null) { urls.Add(downloadVideo.BaseUrl); } if (downloadVideo.BackupUrl != null) { urls.AddRange(downloadVideo.BackupUrl); } // 路径 downloading.DownloadBase.FilePath = downloading.DownloadBase.FilePath.Replace("\\", "/"); string[] temp = downloading.DownloadBase.FilePath.Split('/'); //string path = downloading.DownloadBase.FilePath.Replace(temp[temp.Length - 1], ""); string path = downloading.DownloadBase.FilePath.TrimEnd(temp[temp.Length - 1].ToCharArray()); // 下载文件名 string fileName = Guid.NewGuid().ToString("N"); string key = $"{downloadVideo.Id}_{downloadVideo.Codecs}"; // 老版本数据库没有这一项,会变成null if (downloading.Downloading.DownloadedFiles == null) { downloading.Downloading.DownloadedFiles = new List <string>(); } if (downloading.Downloading.DownloadFiles.ContainsKey(key)) { // 如果存在,表示下载过, // 则继续使用上次下载的文件名 fileName = downloading.Downloading.DownloadFiles[key]; // 还要检查一下文件有没有被人删掉,删掉的话重新下载 // 如果下载视频之后音频文件被人删了。此时gid还是视频的,会下错文件 if (downloading.Downloading.DownloadedFiles.Contains(key) && File.Exists(Path.Combine(path, fileName))) { return(Path.Combine(path, fileName)); } } else { // 记录本次下载的文件 try { downloading.Downloading.DownloadFiles.Add(key, fileName); } catch (ArgumentException) { } // Gid最好能是每个文件单独存储,现在复用有可能会混 // 不过好消息是下载是按固定顺序的,而且下载了两个音频会混流不过 downloading.Downloading.Gid = null; } // 启用https AllowStatus useSSL = SettingsManager.GetInstance().UseSSL(); if (useSSL == AllowStatus.YES) { for (int i = 0; i < urls.Count; i++) { string url = urls[i]; if (url.StartsWith("http://")) { urls[i] = url.Replace("http://", "https://"); } } } else { for (int i = 0; i < urls.Count; i++) { string url = urls[i]; if (url.StartsWith("https://")) { urls[i] = url.Replace("https://", "http://"); } } } // 开始下载 DownloadResult downloadStatus = DownloadByAria(downloading, urls, path, fileName); switch (downloadStatus) { case DownloadResult.SUCCESS: downloading.Downloading.DownloadedFiles.Add(key); downloading.Downloading.Gid = null; return(Path.Combine(path, fileName)); case DownloadResult.FAILED: case DownloadResult.ABORT: default: return(nullMark); } }