Exemplo n.º 1
0
        private void Play(int id)
        {
            if (AppConfig.CurrentConfig.Buttons.buttons.Count > id)
            {
                if (AppConfig.CurrentConfig.Buttons.buttons[id] != null)
                {
                    var b = AppConfig.CurrentConfig.Buttons.buttons[id];
                    if (b.BindType == "YoutubeMusic" && b.Link != null)
                    {
                        var cl            = new YoutubeExplode.YoutubeClient();
                        var vid           = YoutubeExplode.YoutubeClient.ParseVideoId(b.Link);
                        var streamInfoSet = cl.GetVideoMediaStreamInfosAsync(vid);
                        if (streamInfoSet == null)
                        {
                            return;
                        }
                        var streamInfo =
                            streamInfoSet.Result.Audio.FirstOrDefault(n => n.AudioEncoding == AudioEncoding.Aac);
                        if (streamInfo == null)
                        {
                            return;
                        }
                        if (Instance.SoundOutExtra.PlaybackState == PlaybackState.Paused ||
                            Instance.SoundOutExtra.PlaybackState == PlaybackState.Playing)
                        {
                            Instance.SoundOutExtra.Stop();
                        }

                        Instance.SoundOutExtra.Initialize(
                            new AacDecoder(streamInfo.Url).ToMono());
                        Instance.SoundOutExtra.Play();
                    }

                    if (b.BindType == "LocalMusic" && b.Link != null)
                    {
                        if (File.Exists(AppConfig.CurrentConfig.Buttons.buttons[id].Link))
                        {
                            if (Instance.SoundOutExtra.PlaybackState == PlaybackState.Paused ||
                                Instance.SoundOutExtra.PlaybackState == PlaybackState.Playing)
                            {
                                Instance.SoundOutExtra.Stop();
                            }

                            Instance.SoundOutExtra.Initialize(
                                new Mp3MediafoundationDecoder(AppConfig.CurrentConfig.Buttons.buttons[id].Link).ToMono());
                            Instance.SoundOutExtra.Play();
                        }
                    }

                    if (b.BindType == "Stop")
                    {
                        if (Instance.SoundOutExtra.PlaybackState == PlaybackState.Paused ||
                            Instance.SoundOutExtra.PlaybackState == PlaybackState.Playing)
                        {
                            Instance.SoundOutExtra.Stop();
                        }
                    }
                }
            }
        }
Exemplo n.º 2
0
        private async Task _fetchYoutubeInfo(string youtubeVideoId)
        {
            var client = new YoutubeExplode.YoutubeClient();

            YoutubeExplode.Models.Video video = null;
            try
            {
                video = await client.GetVideoAsync(youtubeVideoId);

                this.Video.VideoInfo = YoutubeVideoInfo.FromVideoObject(video);
            }
            catch (Exception e)
            {
                _mediaInfoBeingFetched = false;
                YTrackLogger.Log("Cannot Fetch Video info for " + youtubeVideoId + " : \n\n" + e.Message);
            }
        }
Exemplo n.º 3
0
        public YoutubeClient(string ffmpegPath, string downloadPath, Progress <double> progress, bool overwriteFiles, CancellationToken cancellationToken)
        {
            if (string.IsNullOrEmpty(ffmpegPath))
            {
                throw new ArgumentNullException(nameof(ffmpegPath));
            }

            if (string.IsNullOrEmpty(downloadPath))
            {
                throw new ArgumentNullException(nameof(downloadPath));
            }

            FFmpegPath        = ffmpegPath;
            DownloadPath      = downloadPath;
            Progress          = progress;
            OverwriteFiles    = overwriteFiles;
            CancellationToken = cancellationToken;

            client = new YoutubeExplode.YoutubeClient();
        }
Exemplo n.º 4
0
        private async void DownloadButton_Click(object sender, RoutedEventArgs e)
        {
            if (_isBeingDownloaded)
            {
                return;
            }
            _isBeingDownloaded = true;

            YoutubeExplode.YoutubeClient client = new YoutubeExplode.YoutubeClient();
            var stream = await client.GetVideoMediaStreamInfosAsync(this.Video.Id);

            var mediaInfo = stream.Video.OrderBy(x => x.Resolution.Height).LastOrDefault();

            if (mediaInfo is null)
            {
                MessageBox.Show("Cannot Download This Video", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
                return;
            }

            var downloadUrl = mediaInfo.Url;

            _downloadVideo(downloadUrl);
        }
Exemplo n.º 5
0
        public async Task DownloadMediaStream(YoutubeExplode.YoutubeClient youtubeClient, MediaStreamInfo mediaStreamInfo, string filePath, IProgress <DownloadProgress> progress, CancellationToken cancellationToken, int bufferSize)
        {
            using (var mediaStream = await youtubeClient.GetMediaStreamAsync(mediaStreamInfo))
            {
                using (var output = File.Create(filePath, bufferSize))
                {
                    var  buffer = new byte[bufferSize];
                    int  bytesRead;
                    long totalBytesRead             = 0;
                    long totalBytesReadNextSecond   = 0;
                    long totalBytesReadInLastSecond = 0;

                    var timer = new System.Timers.Timer(TimeSpan.FromSeconds(1).TotalMilliseconds);
                    timer.AutoReset = true;
                    timer.Elapsed  += (s, e) =>
                    {
                        totalBytesReadInLastSecond = totalBytesRead - totalBytesReadNextSecond;
                        totalBytesReadNextSecond   = totalBytesRead;
                    };
                    timer.Start();

                    do
                    {
                        // Read
                        totalBytesRead += bytesRead = await mediaStream.ReadAsync(buffer, 0, buffer.Length, cancellationToken).ConfigureAwait(false);

                        // Write
                        await output.WriteAsync(buffer, 0, bytesRead, cancellationToken).ConfigureAwait(false);

                        progress?.Report(new DownloadProgress(totalBytesRead, mediaStream.Length, totalBytesReadInLastSecond));
                    }while (bytesRead > 0);

                    timer.Stop();
                }
            }
        }
Exemplo n.º 6
0
        private void Play(int id)
        {
            if (AppConfig.CurrentConfig.Buttons.buttons.Count > id)
            {
                if (AppConfig.CurrentConfig.Buttons.buttons[id] != null)
                {
                    var b = AppConfig.CurrentConfig.Buttons.buttons[id];
                    if (b.BindType == "YoutubeMusic" && b.Link != null)
                    {
                        var client         = new YoutubeExplode.YoutubeClient();
                        var video          = client.Videos.GetAsync(b.Link).Result;
                        var streamManifest = client.Videos.Streams.GetManifestAsync(video.Id).Result;
                        if (streamManifest.Streams.Count == 0)
                        {
                            return;
                        }

                        var streamInfo = streamManifest.GetAudioOnly().Where(n => n.AudioCodec.Contains("mp4")).FirstOrDefault();

                        if (streamInfo == null)
                        {
                            return;
                        }

                        var ext = streamInfo.Url;
                        if (ext == string.Empty)
                        {
                            return;
                        }
                        if (Instance.SoundOutExtra.PlaybackState == PlaybackState.Paused ||
                            Instance.SoundOutExtra.PlaybackState == PlaybackState.Playing)
                        {
                            Instance.SoundOutExtra.Stop();
                        }

                        Instance.SoundOutExtra.Initialize(
                            new AacDecoder(streamInfo.Url).ToMono());
                        Instance.SoundOutExtra.Play();
                    }

                    if (b.BindType == "LocalMusic" && b.Link != null)
                    {
                        if (File.Exists(AppConfig.CurrentConfig.Buttons.buttons[id].Link))
                        {
                            if (Instance.SoundOutExtra.PlaybackState == PlaybackState.Paused ||
                                Instance.SoundOutExtra.PlaybackState == PlaybackState.Playing)
                            {
                                Instance.SoundOutExtra.Stop();
                            }

                            Instance.SoundOutExtra.Initialize(
                                new Mp3MediafoundationDecoder(AppConfig.CurrentConfig.Buttons.buttons[id].Link).ToMono());
                            Instance.SoundOutExtra.Play();
                        }
                    }

                    if (b.BindType == "Stop")
                    {
                        if (Instance.SoundOutExtra.PlaybackState == PlaybackState.Paused ||
                            Instance.SoundOutExtra.PlaybackState == PlaybackState.Playing)
                        {
                            Instance.SoundOutExtra.Stop();
                        }
                    }
                }
            }
        }
Exemplo n.º 7
0
        // MAIN MAGIC HERE
        // Retrieves video-name from URL and attempts to download audio
        // If save-preset is chosen, will move file to designated filepath - if it fails, file will remain in program folder.
        private async void btnDownloadAudio_Click(object sender, EventArgs e)
        {
            lblError.Text = "";
            if (checklistSavepresets.SelectedItem == null && checkSaveUsepreset.Checked)
            {
                lblError.Text = "You need to either choose one of the presets, or choose to leave the file in this program's folder!";
                return;
            }
            if (!checkFormatMP3.Checked && !checkFormatWAV.Checked && !checkFormatOGG.Checked)
            {
                lblError.Text = "Please choose a format for the audio file!";
                return;
            }
            if (txtURL.Text == "")
            {
                txtURL.BackColor = Color.FromArgb(255, 130, 90, 90);
                lblError.Text    = "No URL entered!";
                return;
            }
            var url = txtURL.Text;
            var ID  = "boi";

            try
            {
                ID = YoutubeExplode.YoutubeClient.ParseVideoId(url);
            }
            catch (Exception error)
            {
                lblError.Text = "Invalid URL!\n\n" + error;
                return;
            }

            progBarVisible = true;
            var client = new YoutubeExplode.YoutubeClient();

            YoutubeExplode.Models.MediaStreams.MediaStreamInfoSet streamInfoSet;
            YoutubeExplode.Models.MediaStreams.AudioStreamInfo    streamInfo;
            YoutubeExplode.Models.Video video;
            try
            {
                streamInfoSet = await client.GetVideoMediaStreamInfosAsync(ID);

                streamInfo = streamInfoSet.Audio.First();
                video      = await client.GetVideoAsync(ID);
            }
            catch (Exception error)
            {
                lblError.Text  = "There's a problem with receiving the data!\nPlease make sure that you have a working connection\nand have entered correct values.\n\n" + error;
                progBarVisible = false;
                return;
            }

            var title = "boi";

            if (checkNameoriginal.Checked)
            {
                title = video.Title;
            }
            else if (checkNamecustom.Checked)
            {
                if (txtCustomfilename.Text != "")
                {
                    title = txtCustomfilename.Text;
                }
                else
                {
                    txtCustomfilename.BackColor = Color.FromArgb(255, 130, 90, 90);
                    lblError.Text  = "No file name entered!";
                    progBarVisible = false;
                    return;
                }
            }

            try
            {
                await client.DownloadMediaStreamAsync(streamInfo, title + ".mp3");
            }
            catch (Exception error)
            {
                lblError.Text  = "There's a problem with the download!\nPlease make sure that you have a working connection\nand that you're not spamming the button too much.\n\n" + error;
                progBarVisible = false;
                return;
            }

            if (checkSaveInprogramfolder.Checked)
            {
                if (checkFormatMP3.Checked)
                {
                    progBarVisible = false;
                    return;
                }
                else if (checkFormatWAV.Checked)
                {
                    using (MediaFoundationReader mp3 = new MediaFoundationReader(title + ".mp3"))
                    {
                        using (WaveStream pcm = WaveFormatConversionStream.CreatePcmStream(mp3))
                        {
                            WaveFileWriter.CreateWaveFile(title + ".wav", pcm);
                        }
                    }
                    File.Delete(title + ".mp3");
                }
                else if (checkFormatOGG.Checked)
                {
                    using (MediaFoundationReader mp3 = new MediaFoundationReader(title + ".mp3"))
                    {
                        using (WaveStream pcm = WaveFormatConversionStream.CreatePcmStream(mp3))
                        {
                            WaveFileWriter.CreateWaveFile(title + ".wav", pcm);
                        }
                    }

                    ConvUsespreset = false;
                    ConvTitle      = title;
                    Thread OGGconversionThread = new Thread(OGGConversion);
                    OGGconversionThread.Start();
                }
                progBarVisible = false;
                return;
            }
            else if (checkSaveUsepreset.Checked)
            {
                try
                {
                    File.Move(title + ".mp3", Convert.ToString(checklistSavepresets.SelectedItem) + "\\" + title + ".mp3");
                }
                catch (Exception error)
                {
                    lblError.Text  = "Couldn't move the file to the specified path!\nHave you entered the path correctly?\nFile was saved in this program's folder instead.\n\n" + error;
                    progBarVisible = false;
                    return;
                }

                if (checkFormatMP3.Checked)
                {
                    progBarVisible = false;
                    return;
                }
                else if (checkFormatWAV.Checked)
                {
                    using (MediaFoundationReader mp3 = new MediaFoundationReader(Convert.ToString(checklistSavepresets.SelectedItem) + "\\" + title + ".mp3"))
                    {
                        using (WaveStream pcm = WaveFormatConversionStream.CreatePcmStream(mp3))
                        {
                            WaveFileWriter.CreateWaveFile(Convert.ToString(checklistSavepresets.SelectedItem) + "\\" + title + ".wav", pcm);
                        }
                    }
                    File.Delete(Convert.ToString(checklistSavepresets.SelectedItem) + "\\" + title + ".mp3");
                }
                else if (checkFormatOGG.Checked)
                {
                    using (MediaFoundationReader mp3 = new MediaFoundationReader(Convert.ToString(checklistSavepresets.SelectedItem) + "\\" + title + ".mp3"))
                    {
                        using (WaveStream pcm = WaveFormatConversionStream.CreatePcmStream(mp3))
                        {
                            WaveFileWriter.CreateWaveFile(Convert.ToString(checklistSavepresets.SelectedItem) + "\\" + title + ".wav", pcm);
                        }
                    }
                    File.Delete(Convert.ToString(checklistSavepresets.SelectedItem) + "\\" + title + ".mp3");

                    ConvUsespreset = true;
                    Filepath       = Convert.ToString(checklistSavepresets.SelectedItem);
                    ConvTitle      = title;
                    Thread OGGconversionThread = new Thread(OGGConversion);
                    OGGconversionThread.Start();
                }
                progBarVisible = false;
                return;
            }
            else
            {
                checkSaveInprogramfolder.Checked = true;
            }


            progBarVisible = false;
        }
Exemplo n.º 8
0
 public async Task DownloadMediaStream(YoutubeExplode.YoutubeClient youtubeClient, MediaStreamInfo mediaStreamInfo, string filePath, IProgress <DownloadProgress> progress, CancellationToken cancellationToken)
 => await DownloadMediaStream(youtubeClient, mediaStreamInfo, filePath, progress, cancellationToken, 4096).ConfigureAwait(false);
Exemplo n.º 9
0
 public async Task DownloadMediaStream(YoutubeExplode.YoutubeClient youtubeClient, MediaStreamInfo mediaStreamInfo, string filePath)
 => await DownloadMediaStream(youtubeClient, mediaStreamInfo, filePath, null).ConfigureAwait(false);