public static bool YouGetDownLoad(string url, string saveFilePath, bool cutEnd = false) { var tempSavePath = Path.Combine(saveFilePath, Guid.NewGuid().ToString()); Directory.CreateDirectory(tempSavePath); try { var arguments = url; var output = StartToolProcess(yougetPath, arguments, tempSavePath); var videoType = "mp4"; var videoPath = FileHandle.GetFirstFileBySuffix(tempSavePath, videoType); if (string.IsNullOrEmpty(videoPath)) { return(false); } var NormalVideoPath = GetNewFilePath(videoPath); File.Move(videoPath, NormalVideoPath); if (cutEnd) { ToolKit.MediaHelper.CutOneSecondForVideo(NormalVideoPath); } var files = Directory.GetFiles(tempSavePath); foreach (var filePath in files) { var fileName = Path.GetFileName(filePath); var newFilePath = Path.Combine(saveFilePath, fileName); File.Move(filePath, newFilePath); } Directory.Delete(tempSavePath, true); return(true); } catch { Directory.Delete(tempSavePath, true); return(false); } }
/// <summary> /// youtube-dl下载 /// </summary> /// <param name="url"></param> /// <param name="saveFilePath"></param> /// <returns></returns> public static bool YoutubedlDownload(string url, string saveFilePath, bool cutEnd = false) { var tempSavePath = Path.Combine(saveFilePath, Guid.NewGuid().ToString()); Directory.CreateDirectory(tempSavePath); //检测下载链接 Console.WriteLine("开始下载:" + url); var checkArguments = string.Format("\"{0}\" -j --write-thumbnail", url); //.Replace("&from=ted","") var checkOutput = StartToolProcess(YoutubeDlPath, checkArguments, tempSavePath); VideoInfo checkVideoInfo = null; try { checkVideoInfo = JsonConvert.DeserializeObject <VideoInfo>(checkOutput); } catch (Exception ex) { if (url.Contains("youtube.com")) { return(false); } } if (checkVideoInfo == null && url.Contains("youtube.com")) { var printInfo = string.Format($"未检测到 {url} 下有可用下载链接"); Console.WriteLine(printInfo); return(false); } var videoType = "mp4"; if (url.Contains("bilibili.com")) { videoType = "flv"; } //开始下载 var arguments = string.Format("\"{0}\" -f \"{1}\" --write-thumbnail --print-json --newline", url, videoType); arguments = string.Format("\"{0}\" -f \"{1}\" --write-thumbnail --print-json --newline", url, videoType); var output = StartToolProcess(YoutubeDlPath, arguments, tempSavePath); var videoPath = FileHandle.GetFirstFileBySuffix(tempSavePath, videoType); if (string.IsNullOrEmpty(videoPath)) { return(false); } var NormalVideoPath = GetNewFilePath(videoPath); File.Move(videoPath, NormalVideoPath); if (cutEnd) { ToolKit.MediaHelper.CutOneSecondForVideo(NormalVideoPath); } var files = Directory.GetFiles(tempSavePath); foreach (var filePath in files) { var fileName = Path.GetFileName(filePath); fileName = ToolKit.StringHelper.TraditionalToSimplifiedChinese(fileName); var newFilePath = Path.Combine(saveFilePath, fileName); File.Move(filePath, newFilePath); } Directory.Delete(tempSavePath); return(true); }