private async Task HandleDownloadRequest(MediaDownloadJob downloadJob)
 {
     await _vidloadCache.SetJobStatus(downloadJob.TraceId, JobStatus.InProgress)
     .Bind(() => _videoLoader.HandleVideoDownloadRequest(downloadJob))
     .Bind(ml =>
           _vidloadCache.SetMediaLocation(downloadJob.DownloadLink, ml)
           .Bind(() => _vidloadCache.SetJobStatus(downloadJob.TraceId, JobStatus.Completed)))
     .OnFailure(r => _vidloadCache.SetJobStatus(downloadJob.TraceId, JobStatus.Error));
 }
Beispiel #2
0
        public Task <Result <MediaLocation> > HandleVideoDownloadRequest(MediaDownloadJob job)
        {
            if (!Uri.TryCreate(job.DownloadLink, UriKind.Absolute, out _))
            {
                return(Task.FromResult(Result.Failure <MediaLocation>("The URL is not valid")));
            }

            var commandlineArguments = new List <string> {
                $"\"{job.DownloadLink}\"",
                "-c",
                "-i",
                "-w",
                "--no-part",
                "--no-call-home",
                $"-o \"{_vidloadConfiguration.FilesystemConfiguration.DownloadDirectory}/{job.TraceId}.%(ext)s\"",
            };

            if (_vidloadConfiguration.NetworkConfiguration.UseProxy)
            {
                commandlineArguments.Add($"--proxy \"{_vidloadConfiguration.NetworkConfiguration.Proxy}\"");
            }


            if (FormatSpecifier.IsAudioFormat(job.TargetFormat))
            {
                commandlineArguments.Add("--extract-audio");
                commandlineArguments.Add($"--audio-format {job.TargetFormat.ToString().ToLower()}");
                commandlineArguments.Add("-f \"bestaudio\"");
            }

            if (FormatSpecifier.IsVideoFormat(job.TargetFormat))
            {
                commandlineArguments.Add("-f \"bestvideo\"");
                commandlineArguments.Add($"--recode-video {job.TargetFormat.ToString().ToLower()}");
            }

            var downloadResult = _shellCommandExecutor
                                 .Execute("youtube-dl", commandlineArguments, TimeSpan.FromHours(1));

            if (downloadResult.IsSuccess)
            {
                var fileName = $"{job.TraceId}.{FormatSpecifier.GetFileExtensionsForFormat(job.TargetFormat)}";
                var filePath = Path.Join(_vidloadConfiguration.FilesystemConfiguration.DownloadDirectory, fileName);

                return(Task.FromResult(File.Exists(filePath)
          ? Result.Success(new MediaLocation {
                    FilePath = filePath, TraceId = job.TraceId, DownloadLink = job.DownloadLink
                })
          : Result.Failure <MediaLocation>("Could not post-process the video. Output File not found")));
            }

            return(Task.FromResult(Result.Failure <MediaLocation>("WHOOOP")));
        }
Beispiel #3
0
        public Task <Result> Enqueue(MediaDownloadJob mediaDownloadJob)
        {
            try {
                var serialized = JsonConvert.SerializeObject(mediaDownloadJob);
                var encoded    = Encoding.UTF8.GetBytes(serialized);

                _rabbitMqChannel.BasicPublish(
                    string.Empty,
                    _vidloadConfiguration.JobQueueConfiguration.MediaDownloadJobQueueName,
                    null,
                    encoded
                    );
                return(Task.FromResult(Result.Success()));
            } catch {
                return(Task.FromResult(Result.Failure("Could not enqueue Job")));
            }
        }