예제 #1
0
        protected override async Task <RunResult <string> > RunRealDownload(YoutubeDL ydl, string url,
                                                                            CancellationToken ct, IProgress <DownloadProgress> progress)
        {
#if DEBUG
            return(await ydl.RunVideoDownload(url, FormatSelection,
                                              DownloadMergeFormat.Mkv, RecodeFormat, ct, progress,
                                              new Progress <string>(s => System.Diagnostics.Debug.WriteLine(s))));
#else
            return(await ydl.RunVideoDownload(url, FormatSelection,
                                              DownloadMergeFormat.Mkv, RecodeFormat, ct, progress));
#endif
        }
예제 #2
0
        public async Task TestVideoDownloadSimple()
        {
            var result = await ydl.RunVideoDownload(URL, mergeFormat : DownloadMergeFormat.Mkv);

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

            Assert.IsTrue(File.Exists(file));
            Assert.AreEqual(".mkv", Path.GetExtension(file));
            Assert.AreEqual("TEST VIDEO", Path.GetFileNameWithoutExtension(file));
            downloadedFiles.Add(file);
        }
예제 #3
0
 protected override async Task <RunResult <string> > RunRealDownload(YoutubeDL ydl, string url,
                                                                     CancellationToken ct, IProgress <DownloadProgress> progress, OptionSet overrideOptions = null)
 {
     return(await ydl.RunVideoDownload(
                url, FormatSelection,
                DownloadMergeFormat.Mkv, RecodeFormat, ct, progress,
                output : new Progress <string>(s => DownloadOutputLogger.Instance.WriteOutput(url, s)),
                overrideOptions : overrideOptions
                ));
 }
        /// <summary>
        /// Starts the the video download of a job.
        /// </summary>
        /// <param name="job">The job object that should be downloaded.</param>
        /// <param name="token">A cancellation token.</param>
        private async Task DownloadVideo(DownloadJob job, CancellationToken token)
        {
            job.JobState = JobState.Downloading;
            var dl = new YoutubeDL
            {
                OutputFolder       = Path.Combine(_workingDirectory, job.JobId.ToString()),
                OutputFileTemplate = "%(title)s.%(ext)s"
            };
            var progress = new Progress <DownloadProgress>();

            progress.ProgressChanged += (sender, downloadProgress) => job.Progress = downloadProgress.Progress * 100;
            await dl.RunVideoDownload(job.Url, "bestvideo+bestaudio[format_id*=TV_Ton]/bestvideo+bestaudio/best",
                                      progress : progress, ct : token);
        }
예제 #5
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,
            });
        }