public IFile Save(string url) { if (url == null) { throw new ArgumentNullException(nameof(url)); } url = url.Trim(); if (!new Regex(YoutuberRegexp).Match(url).Success) { throw new Exception("Link is not valid for youtube video"); } url = UrlFormatter.RemoveParametersFromUrl(url); IEnumerable <VideoInfo> videoInfos = DownloadUrlResolver.GetDownloadUrls(url); VideoInfo videoInfo = videoInfos.First(info => info.VideoType == VideoType.Mp4 && info.Resolution == 360); if (videoInfo.RequiresDecryption) { DownloadUrlResolver.DecryptDownloadUrl(videoInfo); } string videoFolderPath = _fileResolver.GetVideoFolderPath(StorageType.FileSystem); string fileName = UrlFormatter.GetYoutubeVideoIdentifier(url); string fullPath = Path.Combine(videoFolderPath, fileName + videoInfo.VideoExtension); if (File.Exists(fullPath)) { File.Delete(fullPath); } var videoDownloader = new VideoDownloader(videoInfo, fullPath); videoDownloader.Execute(); return(new PCFile() { Extension = videoInfo.VideoExtension, Filename = fileName, StorageType = StorageType.FileSystem, }); }
public async Task <IFile> SaveAsync(string url) { if (url == null) { throw new ArgumentNullException(nameof(url)); } url = url.Trim(); if (!new Regex(YoutuberRegexp).Match(url).Success) { throw new Exception("Link is not valid for youtube video"); } url = UrlFormatter.RemoveParametersFromUrl(url); var videoInfos = await _yotubeLinkerCore.GetDownloadUrlsAsync(url); var videoInfo = videoInfos.OrderByDescending(x => x.AudioBitrate).First(info => info.VideoType == VideoType.Mp4); var videoFolderPath = _fileResolver.GetVideoFolderPath(StorageType.FileSystem); var fileName = UrlFormatter.GetYoutubeVideoIdentifier(url); var fullPath = Path.Combine(videoFolderPath, fileName + videoInfo.VideoExtension); if (File.Exists(fullPath)) { File.Delete(fullPath); } await HttpHelper.DownloadAsync(videoInfo.DownloadUrl, fullPath); return(new PCFile { Extension = videoInfo.VideoExtension, Filename = fileName, StorageType = StorageType.FileSystem }); }