// Load Video datas from disk private static VideoDatas LoadVideos(string jsonPath, IPreviewBeatmapLevel level) { var infoText = File.ReadAllText(jsonPath); VideoDatas vids; try { try { vids = JsonConvert.DeserializeObject <VideoDatas>(infoText); } catch { VideoData vid = JsonConvert.DeserializeObject <VideoData>(infoText); vid.level = level; vids = new VideoDatas { videos = new List <VideoData> { vid }, level = level }; } } catch (Exception e) { Plugin.logger.Warn("Error parsing video json: " + jsonPath); Plugin.logger.Error(e.GetType().ToString()); Plugin.logger.Error(e.StackTrace); return(null); } foreach (VideoData vid in vids.videos) { vid.level = level; // if (File.Exists(GetVideoPath(vid))) // { // vid.downloadState = DownloadState.Downloaded; // } vid.UpdateDownloadState(); } return(vids); }
private IEnumerator DownloadVideo() { downloading = true; IncrementDownloadCount(); if (!updated) { yield return(new WaitUntil(() => updated)); } VideoDownload download = videoQueue.Peek(); VideoData video = download.video; // Plugin.logger.Debug($"Starting Download with {video.title}"); if (video.downloadState == DownloadState.Cancelled || download.downloadAttempts > MaxRetries) { // skip videoQueue.Dequeue(); if (videoQueue.Count > 0) { // Plugin.logger.Debug($"Starting Next Download"); // Start next download DownloadVideo(); } else { // Plugin.logger.Debug($"Done Download"); // queue empty downloading = false; yield break; } } Plugin.logger.Info("Downloading: " + video.title); IEnumerator countdown = Countdown(download); StopCoroutine(countdown); video.downloadState = DownloadState.Downloading; downloadProgress?.Invoke(video); download.Update(); ydl = MakeYoutubeProcessAndReturnIt(video); Plugin.logger.Info($"yt command: \"{ydl.StartInfo.FileName}\" {ydl.StartInfo.Arguments}"); ydl.Start(); // Hook up our output to console ydl.BeginOutputReadLine(); ydl.BeginErrorReadLine(); int logCount = 0; ydl.OutputDataReceived += (sender, e) => { if (e.Data != null) { Regex rx = new Regex(@"(\d*).\d%+"); Match match = rx.Match(e.Data); if (match.Success) { video.downloadProgress = Single.Parse(match.Value.Substring(0, match.Value.Length - 1)) / 100; downloadProgress?.Invoke(video); download.Update(); if (video.downloadState == DownloadState.Cancelled) { DownloadCancelled((Process)sender, video); } } if (++logCount % 10 == 0 || video.downloadProgress > .95) { Plugin.logger.Info(e.Data); } } }; ydl.ErrorDataReceived += (sender, e) => { if (e.Data.Length < 3) { return; } Plugin.logger.Error(e.Data); //TODO: check these errors are problems - re-download or skip file when an error occurs //video.downloadState = DownloadState.Cancelled; downloadProgress?.Invoke(video); download.Update(); if (video.downloadState == DownloadState.Cancelled || e.Data.Contains("Unable to extract video data")) { DownloadCancelled((Process)sender, video); } }; ydl.Exited += (sender, e) => { StopCoroutine(countdown); if (video.downloadState == DownloadState.Cancelled) { Plugin.logger.Info("Cancelled"); VideoLoader.DeleteVideo(video); } else { // video.downloadState = DownloadState.Downloaded; video.UpdateDownloadState(); VideoLoader.SaveVideoToDisk(video); StartCoroutine(VerifyDownload(video)); } videoQueue.Dequeue(); if (videoQueue.Count > 0) { // Start next download // Plugin.logger.Debug("Starting Next Download"); DownloadVideo(); } else { // queue empty downloading = false; } try { ydl?.Dispose(); } catch { } DecrementDownloadCount(); }; }