Exemplo n.º 1
0
        public async Task TestVideoInformationYoutube()
        {
            string url = "https://www.youtube.com/watch?v=C0DPdy98e4c";
            RunResult <VideoData> result = await ydl.RunVideoDataFetch(url);

            Assert.IsTrue(result.Success);
            Assert.AreEqual(MetadataType.Video, result.Data.ResultType);
            Assert.AreEqual("TEST VIDEO", result.Data.Title);
            Assert.AreEqual("Youtube", result.Data.ExtractorKey);
            Assert.AreEqual(new DateTime(2007, 02, 21), result.Data.UploadDate);
            Assert.IsNotNull(result.Data.Formats);
            Assert.IsNotNull(result.Data.Tags);
            Assert.IsNull(result.Data.Entries);
        }
        /// <summary>
        /// Verifies if the url of a job is valid and adding the result to it.
        /// </summary>
        /// <param name="job">The job object that should be verified.</param>
        private static async Task VerifyUrl(DownloadJob job)
        {
            var dl     = new YoutubeDL();
            var result = await dl.RunVideoDataFetch(job.Url);

            if (result.Data == null)
            {
                if (result.ErrorOutput.Contains("Forbidden"))
                {
                    job.VerificationResult = VerificationResult.DrmProtected;
                    return;
                }

                if (result.ErrorOutput.Contains("Unsupported Url"))
                {
                    job.VerificationResult = VerificationResult.NoVideoFound;
                    return;
                }

                job.VerificationResult = VerificationResult.GenericError;
                return;
            }

            if (result.Data.Entries != null)
            {
                job.VerificationResult = VerificationResult.IsPlaylist;
                return;
            }

            job.VideoData          = result.Data;
            job.VerificationResult = VerificationResult.Valid;
        }
Exemplo n.º 3
0
        public async Task <VideoData> GetVideoInformation(string url, Dictionary <string, string> args = null)
        {
            var ytdl = new YoutubeDL()
            {
                YoutubeDLPath = args != null && args.ContainsKey("Downloader") ? args["Downloader"] : "youtube-dl",
                FFmpegPath    = args != null && args.ContainsKey("FFMPeg") ? args["FFMPeg"] : "/usr/bin/ffmpeg",
                OutputFolder  = Path.GetTempPath(),
            };

            RunResult <VideoData> result = await ytdl.RunVideoDataFetch(url);

            if (result.Success)
            {
                return(result.Data);
            }

            throw new AudioDownloadException($"Unable to get url information\n${url}");
        }
Exemplo n.º 4
0
        public async Task <ResponseDTO <long> > CrawlVideosByUrlAsync(CrawlOtherVideoDTO otherVideoDTO)
        {
            var exVideo = dbContext.OtherCrawledVideos.FirstOrDefault(x => x.VideoUrl == otherVideoDTO.VideoUrl);

            if (exVideo != null)
            {
                return(new ResponseDTO <long>
                {
                    success = false,
                    data = -1,
                    responseMessage = "alrdy Crawled This video"
                });
            }
            string url = otherVideoDTO.VideoUrl;

            if (!System.IO.Directory.Exists($"{Config.OtherVideoPhysicalFilePath}/{otherVideoDTO.SiteName.Trim()}_{otherVideoDTO.Author.Trim()}"))
            {
                System.IO.Directory.CreateDirectory($"{Config.OtherVideoPhysicalFilePath}/{otherVideoDTO.SiteName.Trim()}_{otherVideoDTO.Author.Trim()}");
            }
            var ytdl = new YoutubeDL();

            // set the path of the youtube-dl and FFmpeg if they're not in PATH or current directory
            ytdl.YoutubeDLPath = $"H:/MyProjects/youtube-dl.exe";
            ytdl.FFmpegPath    = $"C:/ffmpeg/bin/ffmpeg.exe";
            // optional: set a different download folder
            ytdl.OutputFolder      = $"{Config.OtherVideoPhysicalFilePath}/{otherVideoDTO.SiteName.Trim()}_{otherVideoDTO.Author.Trim()}";
            ytdl.RestrictFilenames = true;
            // download a video
            var data = await ytdl.RunVideoDataFetch(url : url);

            string title            = data.Data.Title;
            string publishedAt      = data.Data.UploadDate.ToString();
            string author           = data.Data.Uploader;
            string description      = data.Data.Description;
            string channelId        = data.Data.ChannelID;
            string thumbnailDefault = data.Data.Thumbnail;
            double duration         = data.Data.Duration ?? 0;

            var res = await ytdl.RunVideoDownload(url : url);

            // the path of the downloaded file
            string path        = res.Data;
            var    splitedPath = path.Split("\\");
            string folder      = $"{otherVideoDTO.SiteName.Trim()}_{otherVideoDTO.Author.Trim()}";
            string fileName    = splitedPath[splitedPath.Length - 1];
            string fullPath    = $"{otherVideoDTO.SiteName.Trim()}_{otherVideoDTO.Author.Trim()}/{fileName}";

            string timeStr = DateTime.UtcNow.ToString("MMddHHmmss");

            string newFileName = otherVideoDTO.TagString.Trim() + "_" + timeStr + fileName;
            string newFullPath = $"{folder}/{newFileName}";

            System.IO.File.Move($"{Config.OtherVideoPhysicalFilePath}/{fullPath}", $"{Config.OtherVideoPhysicalFilePath}/{newFullPath}");

            if (duration < 1)
            {
                var mediaInfo = await FFmpeg.GetMediaInfo($"{Config.OtherVideoPhysicalFilePath}/{newFullPath}");

                duration = mediaInfo.VideoStreams.First().Duration.TotalSeconds;
            }
            string      thumbnailOutputPath = $"{Config.OtherVideoPhysicalFilePath}/{newFullPath}_thumbnail.png";
            string      thumbnailPath       = $"{newFullPath}_thumbnail.png";
            IConversion conversion          = await FFmpeg.Conversions.FromSnippet.Snapshot($"{Config.OtherVideoPhysicalFilePath}/{newFullPath}"
                                                                                            , thumbnailOutputPath, TimeSpan.FromSeconds(duration / 2));

            IConversionResult result = await conversion.Start();

            OtherCrawledVideos otherCrawledVideos = new OtherCrawledVideos
            {
                Author    = otherVideoDTO.Author.Trim(),
                FileName  = newFileName,
                FullPath  = newFullPath,
                Path      = folder,
                Type      = otherVideoDTO.Type,
                SiteName  = otherVideoDTO.SiteName.Trim(),
                TagString = otherVideoDTO.TagString.Trim(),
                Title     = title ?? newFileName,
                VideoUrl  = otherVideoDTO.VideoUrl,
                Thumbnail = thumbnailPath,
                order     = dbContext.OtherCrawledVideos.DefaultIfEmpty().Max(x => x == null ? 0 : x.order) + 1
            };

            await dbContext.OtherCrawledVideos.AddAsync(otherCrawledVideos);

            await dbContext.SaveChangesAsync();

            return(new ResponseDTO <long> {
                success = true, data = otherCrawledVideos.Id
            });
        }
        public async Task <ResponseDTO <string> > SaveVideoToDisk(string youtubeUrlId = null, [FromQuery] string inputStr = "undefined", [FromQuery] bool bMakeItPrivate = false)
        {
            inputStr = CleanString.RemoveEmojisSChars(inputStr);
            string videoId      = youtubeUrlId;
            string url          = $"https://www.youtube.com/watch?v={videoId}";
            var    crawledVideo = youtubeDownloadManager.getCrawledByYoutubeVideoId(videoId);

            if (crawledVideo != null)
            {
                return(new ResponseDTO <string>
                {
                    success = false,
                    data = null,
                    responseMessage = "alrdy Crawled this video"
                });
            }
            if (!System.IO.Directory.Exists($"{Config.PhysicalFilePath}/{inputStr}"))
            {
                System.IO.Directory.CreateDirectory($"{Config.PhysicalFilePath}/{inputStr}");
            }
            var ytdl = new YoutubeDL();

            // set the path of the youtube-dl and FFmpeg if they're not in PATH or current directory
            ytdl.YoutubeDLPath = $"H:/MyProjects/youtube-dl.exe";
            ytdl.FFmpegPath    = $"C:/ffmpeg/bin/ffmpeg.exe";
            // optional: set a different download folder
            ytdl.OutputFolder      = $"{Config.PhysicalFilePath}/{inputStr}";
            ytdl.RestrictFilenames = true;
            // download a video
            var data = await ytdl.RunVideoDataFetch(url : url);

            string title            = data.Data.Title;
            string publishedAt      = data.Data.UploadDate.ToString();
            string author           = CleanString.RemoveEmojisSChars(data.Data.Uploader);
            string description      = data.Data.Description;
            string channelId        = data.Data.ChannelID;
            string thumbnailDefault = data.Data.Thumbnail;
            var    res = await ytdl.RunVideoDownload(url : url);

            // the path of the downloaded file
            string path        = res.Data;
            var    splitedPath = path.Split("\\");
            string folder      = $"{inputStr}";
            string fileName    = splitedPath[splitedPath.Length - 1];
            string fullPath    = $"{inputStr}/{fileName}";

            string timeStr = DateTime.UtcNow.ToString("yyyyMMddHHmmss");

            string newFileName = timeStr + fileName;
            string newFullPath = $"{folder}/{newFileName}";

            System.IO.File.Move($"{Config.PhysicalFilePath}/{fullPath}", $"{Config.PhysicalFilePath}/{newFullPath}");

            try
            {
                Crawled crawled = new Crawled
                {
                    ChannelId        = channelId,
                    CreatedAt        = DateTime.UtcNow,
                    Description      = description,
                    FileName         = newFileName,
                    FullPath         = newFullPath,
                    Path             = folder,
                    PublishedAt      = publishedAt,
                    ThumbnailDefault = thumbnailDefault,
                    Title            = title,
                    UpdatedAt        = DateTime.UtcNow,
                    VideoId          = videoId,
                    Order            = (await youtubeDownloadManager.getCrawledMaxOrderAsync()) + 1
                };
                using (TransactionScope scope = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled))
                {
                    crawled = await youtubeDownloadManager.AddCrawledAsync(crawled);

                    if (bMakeItPrivate)
                    {
                        await youtubeDownloadManager.AddPrivateCrawledAsync(new PrivateCrawled
                        {
                            CrawledId = crawled.Id,
                            Order     = await youtubeDownloadManager.getPrivateCrawledMaxOrderAsync() + 1
                        });
                    }
                    else if (!bMakeItPrivate)
                    {
                        await youtubeDownloadManager.AddPublicCrawledAsync(new PublicCrawled
                        {
                            CrawledId = crawled.Id,
                            Order     = await youtubeDownloadManager.getPublicCrawledMaxOrderAsync() + 1
                        });
                    }

                    scope.Complete();
                }
            }
            catch (Exception e)
            {
                Console.WriteLine($"!!! {e.Message}");
                Console.WriteLine($"!!! {e.InnerException}");
                return(new ResponseDTO <string>
                {
                    success = false,
                    data = null,
                    responseMessage = e.Message
                });
            }


            return(new ResponseDTO <string>
            {
                success = true,
                data = videoId,
            });
        }
Exemplo n.º 6
0
        public async Task GetYouTubeVideoInformation(YoutubeDL YoutubeDLWorker, bool Force = false)
        {
            if (YoutubeDLWorker == null)
            {
                throw new NullReferenceException("GetYouTubeVideoInformation: YoutubeDLWorker was provided null");
            }

            if (InformationAquireTask != null)
            {
                await InformationAquireTask;
                return;
            }
            else if (LocalFile)
            {
                MainForm.StaticPostToDebug($"GetYouTubeVideoInformation: Skipped {(string.IsNullOrEmpty(Title) ? Link : Title)} data, Song is Local.");
                return;
            }
            else if (!Force && PingValid)
            {
                MainForm.StaticPostToDebug($"GetYouTubeVideoInformation: {(string.IsNullOrEmpty(Title) ? Link : Title)} is still within valid period, download canceled.");
                return;
            }
            else if (Force && PingValid)
            {
                MainForm.StaticPostToDebug($"GetYouTubeVideoInformation: Forced download of {(string.IsNullOrEmpty(Title) ? Link : Title)} data.");
            }

            DownloadWorking = true;
            if (GlobalFunctions.GetYouTubeVideoID(Link, out string youtubeMatch))
            {
                RunResult <VideoData> Youtubedata = null;
                Exception             exception   = null;

                try {
                    MainForm.StaticPostToDebug($"GetYouTubeVideoInformation: Download of {(string.IsNullOrEmpty(Title) ? Link : Title)} started.");
                    InformationAquireTask = YoutubeDLWorker.RunVideoDataFetch("https://www.youtube.com/watch?v=" + youtubeMatch
                                                                              , overrideOptions: new YoutubeDLSharp.Options.OptionSet()
                    {
                        DumpJson           = true,
                        DumpSingleJson     = true,
                        HlsPreferNative    = true,
                        IgnoreConfig       = true,
                        NoPlaylist         = true,
                        SkipDownload       = true,
                        GetThumbnail       = false,
                        ListThumbnails     = false,
                        WriteAllThumbnails = false,
                        WriteThumbnail     = false
                    });
                    Youtubedata = await InformationAquireTask;
                } catch (Exception e) {
                    exception = e;
                }

                if (exception != null)
                {
                    MainForm.StaticPostToDebug($"{Link} : Error attempting to download song information. : {ErrorMessage = exception.Message}");
                    LastPingFailed = true;
                }
                else if (Youtubedata != null && Youtubedata.Success)
                {
                    Title     = Youtubedata.Data.Title;
                    LengthSec = (int)Youtubedata.Data.Duration;
                    if (LengthSec > 900)
                    {
                        LastPingFailed = true;
                        ErrorMessage   = "Video Length exceeds set limit of 15 Mins.";

                        MainForm.StaticPostToDebug($"Video Length exceeds set limit of 15 Mins.... {Title}");
                    }
                    else
                    {
                        LastValidPing  = DateTime.Now;
                        LastPingFailed = false;

                        MainForm.StaticPostToDebug($"Secondary Song Info Downloaded... {Title}");
                    }
                }
                else
                {
                    if (Youtubedata == null)
                    {
                        MainForm.StaticPostToDebug($"https://www.youtube.com/watch?v={youtubeMatch} : YoutubeDLWorker Crashed Out");
                    }
                    else
                    {
                        string errors = "";
                        foreach (string error in Youtubedata.ErrorOutput)
                        {
                            errors += error + " :: ";
                        }
                        //This video is not available
                        if (errors.Contains("This video is not available"))
                        {
                            ErrorMessage = "Video not available, it may not be available at streamers location.";
                        }
                        MainForm.StaticPostToDebug(errors);
                    }
                    LastPingFailed = true;
                }
            }
            else
            {
                ErrorMessage = $"{Link} Failed, link was not recognised by Regex.";
                MainForm.StaticPostToDebug($"GetYouTubeVideoInformation using link: {Link} Failed, link was not recognised by Regex.");
                LastPingFailed = true;
            }

            DownloadWorking       = false;
            InformationAquireTask = null;
            return;
        }