public static YoutubeVideoInfo GetVideoInfo(string vid) { if (_videoInfoCache.TryGetValue(vid, out YoutubeVideoInfo cachedInfo)) { return(cachedInfo); } var cmd = @$ "youtube-dl -s --get-filename --get-duration --no-check-certificate " "https://youtu.be/{vid}" ""; var shellResult = ShellHelper.Execute(cmd); if (shellResult.ExitCode != 0) { throw new Exception($"youtube-dl -s exited with code {shellResult.ExitCode}.\n{shellResult.Output}"); } var dataArray = shellResult.Output.ToString().Split(Environment.NewLine, StringSplitOptions.RemoveEmptyEntries); if (dataArray.Length < 2) { throw new Exception($"youtube-dl -s returned unformatted data {shellResult.ExitCode}.\n{shellResult.Output}"); } var info = new YoutubeVideoInfo() { Filename = dataArray[0].Trim(), Duration = dataArray[1] }; if (info.Filename.Contains('.')) { info.Filename = ShellHelper.SanitizeFilename(info.Filename.Substring(0, info.Filename.LastIndexOf('.'))); } var dur = info.Duration.Split(':'); if (dur.Length == 1) { info.DurationSeconds = int.Parse(dur[0]); } else if (dur.Length == 2) { info.DurationSeconds = int.Parse(dur[0]) * 60 + int.Parse(dur[1]); } else if (dur.Length == 3) { info.DurationSeconds = int.Parse(dur[0]) * 3600 + int.Parse(dur[1]) * 60 + int.Parse(dur[2]); } _videoInfoCache[vid] = info; return(info); }
private void LogStart(YoutubeProcessRequest request, YoutubeVideoInfo info) { Startup.EphemeralLog($"=== YouTube Process ===", true); Startup.EphemeralLog($"Request: {JsonConvert.SerializeObject(request)} - Title: {info.Filename} - Duration: {info.Duration}", true); }