Esempio n. 1
0
        protected override async Task <RunResult <string> > RunRealDownload(YoutubeDL ydl, string url,
                                                                            CancellationToken ct, IProgress <DownloadProgress> progress)
        {
#if DEBUG
            return(await ydl.RunAudioDownload(url, ConversionFormat, ct, progress,
                                              new Progress <string>(s => System.Diagnostics.Debug.WriteLine(s))));
#else
            return(await ydl.RunAudioDownload(url, ConversionFormat, ct, progress));
#endif
        }
Esempio n. 2
0
 protected override async Task <RunResult <string> > RunRealDownload(YoutubeDL ydl, string url,
                                                                     CancellationToken ct, IProgress <DownloadProgress> progress, OptionSet overrideOptions = null)
 {
     return(await ydl.RunAudioDownload(
                url, ConversionFormat, ct, progress,
                output : new Progress <string>(s => DownloadOutputLogger.Instance.WriteOutput(url, s)),
                overrideOptions : overrideOptions
                ));
 }
Esempio n. 3
0
        public async Task TestAudioDownloadSimple()
        {
            var result = await ydl.RunAudioDownload(URL, AudioConversionFormat.Mp3);

            Assert.IsTrue(result.Success);
            Assert.AreEqual(String.Empty, String.Join("", result.ErrorOutput));
            string file = result.Data;

            Assert.IsTrue(File.Exists(file));
            Assert.AreEqual(".mp3", Path.GetExtension(file));
            Assert.AreEqual("TEST VIDEO", Path.GetFileNameWithoutExtension(file));
            downloadedFiles.Add(file);
        }
Esempio n. 4
0
        /// <summary>
        /// Downloads a file from anywhere youtube-dl supports and converts it to wav
        /// </summary>
        /// <param name="url">The url to download from</param>
        /// <returns>A boolean indicating whether the download was successful</returns>
        private static async Task <bool> Download(string url)
        {
            var ytdl = new YoutubeDL()
            {
                FFmpegPath           = "ffpmeg.exe",
                YoutubeDLPath        = "youtube-dl.exe",
                IgnoreDownloadErrors = false,
                OutputFileTemplate   = "temp.%(ext)s",
                OverwriteFiles       = true
            };

            var res = await ytdl.RunAudioDownload(url, AudioConversionFormat.Wav);

            return(await Task.FromResult(res.Success));
        }
Esempio n. 5
0
 protected override async Task <RunResult <string> > RunRealDownload(
     YoutubeDL ydl, string url, CancellationToken ct,
     IProgress <DownloadProgress> progress, OptionSet overrideOptions = null)
 {
     if (IsAudio)
     {
         return(await ydl.RunAudioDownload(
                    url, AudioConversionFormat, ct, progress,
                    output : new Progress <string>(s => DownloadOutputLogger.Instance.WriteOutput(url, s)),
                    overrideOptions : overrideOptions
                    ));
     }
     else
     {
         return(await ydl.RunVideoDownload(
                    url, FormatSelection,
                    DownloadMergeFormat.Mkv, VideoRecodeFormat, ct, progress,
                    output : new Progress <string>(s => DownloadOutputLogger.Instance.WriteOutput(url, s)),
                    overrideOptions : overrideOptions
                    ));
     }
 }
Esempio n. 6
0
        public async Task GetYouTubeAudioData(YoutubeDL YoutubeDLWorker, YoutubeDLSharp.Options.AudioConversionFormat type = YoutubeDLSharp.Options.AudioConversionFormat.Mp3, bool Force = false)
        {
            if (YoutubeDLWorker == null)
            {
                throw new NullReferenceException("GetYouTubeAudioData: YoutubeDLWorker was provided null");
            }

            if (VideoAquireTask != null)
            {
                await VideoAquireTask;
                return;
            }

            if (LocalFile)
            {
                MainForm.StaticPostToDebug($"GetYouTubeVideoInformation: Skipped {(string.IsNullOrEmpty(Title) ? Link : Title)} download, Song is Local.");
                return;
            }

            if (string.IsNullOrEmpty(Title))
            {
                if (InformationAquireTask != null)
                {
                    await InformationAquireTask;
                }
                else
                {
                    await GetYouTubeVideoInformation(YoutubeDLWorker);
                }
            }

            if (!Force && AudioCached())
            {
                MainForm.StaticPostToDebug($"GetYouTubeAudioData: {(string.IsNullOrEmpty(Title) ? Link : Title)} Audio found, download canceled.");
                return;
            }
            else if (Force)
            {
                MainForm.StaticPostToDebug($"GetYouTubeVideoInformation: Forced download of {(string.IsNullOrEmpty(Title) ? Link : Title)} Audio");
            }
            DownloadWorking = true;

            if (GlobalFunctions.GetYouTubeVideoID(Link, out string ID))
            {
                if (AudioCached())
                {
                    File.Delete(FullDirLocation);
                }
                RunResult <string> Youtubedata = null;
                Exception          exception   = null;

                try {
                    VideoAquireTask = YoutubeDLWorker.RunAudioDownload("https://www.youtube.com/watch?v=" + ID, type);
                    Youtubedata     = await VideoAquireTask;
                } catch (Exception e) {
                    exception = e;
                }

                if (exception != null)
                {
                    MainForm.StaticPostToDebug($"{Title} : Error attempting to download song data. : {ErrorMessage = exception.Message}");
                    LastPingFailed = true;
                }
                else if (Youtubedata != null && Youtubedata.Success)
                {
                    string extension     = $".{type.ToString().ToLower()}";
                    string filenameTitle = Title;
                    char[] invalidchars  = Path.GetInvalidFileNameChars();
                    foreach (char invalid in invalidchars)
                    {
                        filenameTitle = filenameTitle.Replace(invalid, 'X');
                    }
                    string newFilename = $"{Path.GetDirectoryName(Youtubedata.Data)}\\{ID} - {filenameTitle}{extension}";

                    try {
                        bool check;
                        do                           // Youtube-dl might still be processing the file so wait until it is created then rename
                        {
                            check = true;
                            IEnumerable <string> directories = Directory.GetFiles($"{Path.GetDirectoryName(Youtubedata.Data)}\\");
                            foreach (string dir in directories)
                            {
                                if (dir.Contains(ID) && dir.Contains(extension))
                                {
                                    File.Move(dir, newFilename);
                                    check = false;
                                    break;
                                }
                            }
                            await Task.Delay(1000);
                        } while (check);
                    } catch (Exception e) {
                        MainForm.StaticPostToDebug($"Rename function failed on {Title} : {e.Message}");
                    }

                    string successMessage = $"Audio Download of {Link} Successfull: {newFilename}";

                    MainForm.StaticPostToDebug(successMessage);

                    //Find filename by ID then rename that entry to new one, need to change special characters before that too

                    DirLocation    = newFilename.Replace(Directory.GetCurrentDirectory(), "");
                    LastValidPing  = DateTime.Now;
                    LastPingFailed = false;
                }
                else
                {
                    if (Youtubedata == null)
                    {
                        MainForm.StaticPostToDebug($"GetYouTubeAudioData using link: {Link} Failed, Downloader encountered errors.");
                    }
                    else
                    {
                        string errors = "";
                        foreach (string error in Youtubedata.ErrorOutput)
                        {
                            errors += error + " :: ";
                        }
                        MainForm.StaticPostToDebug(errors);
                    }

                    LastPingFailed = true;
                }
            }
            else
            {
                ErrorMessage = $"{Link} Failed, link was not recognised by Regex.";
                MainForm.StaticPostToDebug($"GetYouTubeAudioData using link: {Link} Failed, link was not recognised by Regex.");
                LastPingFailed = true;
            }

            VideoAquireTask = null;
            DownloadWorking = false;
        }