private void Downloader (YouTubeVideo video, MainProgramElements mainWindow, bool isAudio) { string temporaryDownloadPath = Path.Combine(this.UserSettings.TemporarySaveLocation, video.FullName); string movingPath = Path.Combine(this.UserSettings.MainSaveLocation, video.FullName); if (this.UserSettings.ValidationLocations.All(path => !File.Exists(Path.Combine(path, video.FullName)) && !File.Exists(movingPath)) { if(isAudio) { var audioDownloader = new AudioDownloader (video, temporaryDownloadPath);; audioDownloader.AudioExtractionProgressChanged += (sender, args) => mainWindow.CurrentDownloadProgress = (int)(85 + args.ProgressPercentage * 0.15); audioDownloader.Execute(); } else { var videoDownloader = new VideoDownloader (video, temporaryDownloadPath); videoDownloader.DownloadProgressChanged += ((sender, args) => mainWindow.CurrentDownloadProgress = (int)args.ProgressPercentage); videoDownloader.Execute(); } if (!temporaryDownloadPath.Equals(movingPath, StringComparison.OrdinalIgnoreCase)) File.Move(temporaryDownloadPath, movingPath); } else { throw new DownloadCanceledException(string.Format(CultureInfo.CurrentCulture, "The download of #{0} '{1}({2})' has been canceled because it already existed.", videoToUse.Position, RemoveIllegalPathCharacters(video.Title).Truncate(10), videoToUse.Location.Truncate(100))); } }
private bool LoadNextSong() { if (SongQueue.Count == 0) { CurrentSong = null; return false; } CurrentSong = SongQueue[0]; SongQueue.RemoveAt(0); return true; }
public void ProcessVideosToMp3(List <VideoObject> allVideoIds, string pathMp3Files, string playlistTile) { try { foreach (VideoObject videoObject in allVideoIds) { Stopwatch stopWatchFile = new Stopwatch(); stopWatchFile.Start(); _log.Debug($"Thread : {Thread.CurrentThread.ManagedThreadId} => Processing: " + videoObject.Title); var youtube = YouTube.Default; try { // Get all different videos var videos = YouTube.Default.GetAllVideos("http://www.youtube.com/watch?v=" + videoObject.Id); // Create object to store highest quality VideoLibrary.YouTubeVideo videoHighRes = null; int maxAudioBitrate = 0; foreach (var video in videos) { if (video.AudioBitrate > maxAudioBitrate && (video.FileExtension == ".mp4" || video.FileExtension == ".webm")) { maxAudioBitrate = video.AudioBitrate; videoHighRes = video; } } _log.Debug($"Thread : {Thread.CurrentThread.ManagedThreadId} => Audio bitrate = " + videoHighRes.AudioBitrate + " => " + videoHighRes.FullName); // Write video to file if mp3 version doesn't exist yet if (!File.Exists(pathMp3Files + videoHighRes.FullName + ".mp3")) { string test = pathMp3Files + videoHighRes.FullName + ".mp3"; for (int attempts = 0; attempts < 5; attempts++) // if you really want to keep going until it works, use for(;;) { try { _log.Debug($"Thread : {Thread.CurrentThread.ManagedThreadId} => Attempt number : " + attempts + " of file " + videoHighRes.Title); //content = videoHighRes.GetBytes(); GlobalMp3Path = pathMp3Files; DownloadYoutubeVideo(videoHighRes.Uri, videoHighRes.FullName); break; } catch (Exception x) { _log.Debug($"Thread : {Thread.CurrentThread.ManagedThreadId} => Error in retry " + attempts + " with message : " + x.Message); } System.Threading.Thread.Sleep(1000); // Possibly a good idea to pause here } } else { _log.Debug($"Thread : {Thread.CurrentThread.ManagedThreadId} => File already exists."); } } catch (Exception ex) { _log.Error($"Thread : {Thread.CurrentThread.ManagedThreadId} => Error during retrieving or writing video : " + videoObject.Title + " with error message: " + ex.Message); TelegramBotSendError($"Thread : {Thread.CurrentThread.ManagedThreadId} => Error during retrieving or writing video : " + videoObject.Title + " with error message: " + ex.Message); } stopWatchFile.Stop(); _log.Debug($"Thread : {Thread.CurrentThread.ManagedThreadId} => File downloaded in : " + stopWatchFile.Elapsed + " seconds."); _log.Info($"Thread : {Thread.CurrentThread.ManagedThreadId} => Downloaded : " + videoObject.Title); TelegramBotSendInfo("Processed: " + videoObject.Title + " in " + stopWatchFile.Elapsed + " seconds."); } } catch (Exception e) { throw e; } }
internal VideoInfo(YouTubeVideo video) { this.video = video; }
private void SendVoice(string file, DiscordClient client, YouTubeVideo video) { DiscordVoiceClient vc = client.GetVoiceClient(); try { int ms = vc.VoiceConfig.FrameLengthMs; int channels = vc.VoiceConfig.Channels; int sampleRate = 48000; int blockSize = 48 * 2 * channels * ms; //sample rate * 2 * channels * milliseconds byte[] buffer = new byte[blockSize]; var outFormat = new WaveFormat(sampleRate, 16, channels); vc.SetSpeaking(true); if(video.AudioFormat == AudioFormat.Mp3) { using (var mp3Reader = new Mp3FileReader(video.Stream())) { using (var resampler = new MediaFoundationResampler(mp3Reader, outFormat) { ResamplerQuality = 60 }) { //resampler.ResamplerQuality = 60; int byteCount; while ((byteCount = resampler.Read(buffer, 0, blockSize)) > 0) { if (vc.Connected) { vc.SendVoice(buffer); } else break; } Console.ForegroundColor = ConsoleColor.Yellow; Console.WriteLine("Voice finished enqueuing"); Console.ForegroundColor = ConsoleColor.White; resampler.Dispose(); mp3Reader.Close(); } } } else if(video.AudioFormat == AudioFormat.Vorbis) { using (var vorbis = new NAudio.Vorbis.VorbisWaveReader(video.Stream())) { using (var resampler = new MediaFoundationResampler(vorbis, outFormat) { ResamplerQuality = 60 }) { int byteCount; while ((byteCount = resampler.Read(buffer, 0, blockSize)) > 0) { if (vc.Connected) { vc.SendVoice(buffer); } else break; } Console.ForegroundColor = ConsoleColor.Yellow; Console.WriteLine("Voice finished enqueuing"); Console.ForegroundColor = ConsoleColor.White; resampler.Dispose(); vorbis.Close(); } } } } catch (Exception ex) { try { MainEntry.owner.SendMessage("Exception during voice: `" + ex.Message + "`\n\n```" + ex.StackTrace + "\n```"); } catch { } } }