/// <summary> /// Send Audio to last destionation (user) /// </summary> /// <param name="streamInfo">audio file</param> /// <param name="video">need for get some information about song.</param> /// <returns></returns> public async Task SendAudio(IAudioStreamInfo streamInfo, YoutubeExplode.Videos.Video video) { using (var audioStream = await _youtubeClient.Videos.Streams.GetAsync(streamInfo)) { await _telegramBotClient.SendAudioAsync( chatId : ChatId, audio : audioStream, caption : video.Url, duration : (int)video.Duration.TotalSeconds, performer : video.Author, title : video.Title); } }
/// <summary> /// Get Audio Stream /// </summary> /// <param name="streamManifest">manifest bases on video bitray/quality</param> /// <param name="messageText">text which user input, need for exception</param> /// <returns></returns> private async Task <IAudioStreamInfo> GetAudioStreamInfo(StreamManifest streamManifest, string messageText) { IAudioStreamInfo streamInfo = (IAudioStreamInfo)streamManifest .GetAudioOnly() .WithHighestBitrate(); if (streamInfo == null || streamInfo.Size.TotalMegaBytes > 50) { var failMessage = streamManifest.GetAudioOnly().Any() ? ValidationMessages.FileSizeExceedLimitMessage : ValidationMessages.Mp4DoesNotExistsMessage; await SendTextMessage(failMessage); _logger.LogInformation($"Stream info for {messageText} was empty and error for user is {failMessage}"); return(null); } return(streamInfo); }
public async Task DownloadTrack(MusixSongResult Track, string OutputDirectory, AudioEffectStack Effects = null, CancellationToken cancellationToken = default) { int Steps; int Step = 0; if (Effects == null) { Steps = 9; } else { Steps = 9 + Effects.EffectCount; } TryCallback(Step, Steps, "Starting Download", Track); if (cancellationToken.IsCancellationRequested) { return; } bool HasEffects = Effects != null; if (HasEffects) { Console.WriteLine("Has Effects"); if (string.IsNullOrEmpty(Effects.AudioCachePath)) { Effects.AudioCachePath = AudioCache; } } // Step 1 Step++; TryCallback(Step, Steps, "Preparing Download", Track); Console.WriteLine("Start Download"); if (!Track.HasVideo) { Console.WriteLine("No Vid"); } if (!Track.HasVideo) { return; } string SourceAudio = Path.Combine(AudioCache, $"audio_source_{DateTime.Now.Ticks}"); string AlbumCover = Path.Combine(ImageCachePath, $"cover_{DateTime.Now.Ticks}.jpg"); string OutputFile = Path.Combine(OutputDirectory, FileHelpers.ScrubFileName($"{Track.SpotifyTrack.Artists[0].Name} - {Track.SpotifyTrack.Name.Replace("?", "").Trim(' ')}.mp3")); string MidConversionFile = Path.Combine(AudioCache, FileHelpers.ScrubFileName($"MidConversion_{DateTime.Now.Ticks}.mp3")); // Step 2 Step++; TryCallback(Step, Steps, "Aquiring streams", Track); StreamManifest StreamData = await YouTube.Videos.Streams.GetManifestAsync(Track.YoutubeVideo.Id); if (cancellationToken.IsCancellationRequested) { return; } // Step 3 Step++; TryCallback(Step, Steps, "Sorting Streams", Track); List <AudioOnlyStreamInfo> AudioStreams = StreamData.GetAudioOnlyStreams().ToList(); AudioStreams.OrderBy(dat => dat.Bitrate); if (AudioStreams.Count() == 0) { Console.WriteLine("No Streams"); } if (AudioStreams.Count() == 0) { return; } IAudioStreamInfo SelectedStream = AudioStreams[0]; // Step 4 Step++; TryCallback(Step, Steps, "Starting downloads", Track); if (cancellationToken.IsCancellationRequested) { return; } //Task AudioDownloadTask = new Task(async () => await YouTube.Videos.Streams.DownloadAsync(SelectedStream, SourceAudio)); var req = WebRequest.CreateHttp(SelectedStream.Url); req.Method = "GET"; using (var resp = req.GetResponse()) using (var network = resp.GetResponseStream()) using (var fs = new FileStream(SourceAudio, FileMode.OpenOrCreate, FileAccess.ReadWrite)) { Console.WriteLine("Downloading"); await network.CopyToAsync(fs); Console.WriteLine("flushing"); await fs.FlushAsync(); Console.WriteLine("done"); } WebClient WebCl = new WebClient(); Step++; TryCallback(Step, Steps, "Starting", Track); SpotifyImage Cover = Track.SpotifyTrack.Album.Images[0]; var CoverDownloadTask = new Task(() => { Console.WriteLine("Downloading Cover"); WebCl.DownloadFile(new Uri(Cover.Url), AlbumCover); } ); CoverDownloadTask.Start(); Step++; TryCallback(Step, Steps, "Waiting for downloads", Track); if (cancellationToken.IsCancellationRequested) { return; } //if (!AudioDownloadTask.IsCompleted) //{ // Console.WriteLine("Waiting on artwork..."); // CoverDownloadTask.Wait(); //} if (cancellationToken.IsCancellationRequested) { return; } //if (!AudioDownloadTask.IsCompleted) //{ // Console.WriteLine("Waiting on audio..."); // AudioDownloadTask.Wait(); // Console.WriteLine("Download Complete."); //} Thread.Sleep(100); if (cancellationToken.IsCancellationRequested) { return; } string ConversionFile = OutputFile; if (HasEffects) { ConversionFile = MidConversionFile; } if (File.Exists(OutputFile)) { File.Delete(OutputFile); } if (File.Exists(ConversionFile)) { File.Delete(ConversionFile); } Step++; TryCallback(Step, Steps, "Transcoding audio to mp3", Track); // Step 8 Console.WriteLine("Starting Conversion..."); await ConversionsProvider.Convert(SourceAudio, ConversionFile); Console.WriteLine("Conversion Complete."); // Step 9 if (cancellationToken.IsCancellationRequested) { return; } if (HasEffects) { Step++; int InternalStep = Step; TryCallback(Step, Steps, "Applying audio effects", Track); Effects.ApplyEffects(ConversionFile, OutputFile, (step, stepmax, status, download) => { step++; TryCallback(Step, Steps, status, Track); }, cancellationToken); } if (cancellationToken.IsCancellationRequested) { return; } Step++; TryCallback(Step, Steps, "Applying ID3 metadata tags", Track); // Step 10 TagLib.Id3v2.Tag.DefaultVersion = 3; TagLib.Id3v2.Tag.ForceDefaultVersion = true; TagLibFile TLF = TagLibFile.Create(OutputFile); TagLibPicture Pic = new TagLibPicture(AlbumCover); TagLib.Id3v2.AttachedPictureFrame Frame = new TagLib.Id3v2.AttachedPictureFrame(Pic) { MimeType = System.Net.Mime.MediaTypeNames.Image.Jpeg }; Pic.Type = TagLib.PictureType.FrontCover; TagLib.IPicture[] Pics = { Pic }; TLF.Tag.Pictures = Pics; TLF.Tag.Title = Track.SpotifyTrack.Name.Split('-')[0].Trim(' '); TLF.Tag.Album = Track.SpotifyTrack.Album.Name; TLF.Tag.AlbumArtists = Track.SpotifyTrack.Album.Artists.CastEnumerable(x => x.Name).ToArray(); TLF.Tag.Disc = (uint)Track.SpotifyTrack.DiscNumber; TLF.Tag.AlbumSort = Track.SpotifyTrack.Album.AlbumType; DateTime?DT = GetDate(Track.SpotifyTrack.Album.ReleaseDate); if (DT.HasValue) { TLF.Tag.Year = (uint)DT.Value.Year; } if (cancellationToken.IsCancellationRequested) { return; } TLF.Save(); // Clean Up // Step 11 Step++; TryCallback(Step, Steps, "Cleaning up", Track); WebCl.Dispose(); TLF.Dispose(); Console.WriteLine("Done."); OnMusixDownloadComplete?.Invoke(Track); }
public VideoDownloadInfo(IVideoStreamInfo video, IAudioStreamInfo audio) { Video = video; Audio = audio; }
private async Task DownloadMuxedFromStreamsAsync(IVideoStreamInfo videoStreamInfo, IAudioStreamInfo audioStreamInfo, string file) { var videoStream = await client.Videos.Streams.GetAsync(videoStreamInfo); var audioStream = await client.Videos.Streams.GetAsync(audioStreamInfo); var videoPipeName = "ffvideopipe"; var audioPipeName = "ffaudiopipe"; var args = $@"-y -i \\.\pipe\{videoPipeName} -i \\.\pipe\{audioPipeName} -c copy -map 0:v:0 -map 1:a:0 {file}.{videoStreamInfo.Container.Name}"; var videoPipe = new NamedPipeServerStream(videoPipeName, PipeDirection.Out, 1, PipeTransmissionMode.Byte, PipeOptions.Asynchronous, 10000, 10000); var audioPipe = new NamedPipeServerStream(audioPipeName, PipeDirection.Out, 1, PipeTransmissionMode.Byte, PipeOptions.Asynchronous, 10000, 10000); var process = new Process { StartInfo = { FileName = "ffmpeg", Arguments = args, UseShellExecute = false, CreateNoWindow = true, RedirectStandardInput = true } }; process.Start(); videoPipe.WaitForConnection(); var videoCopy = videoStream.CopyToAsync(videoPipe); audioPipe.WaitForConnection(); var audioCopy = audioStream.CopyToAsync(audioPipe); Task.WaitAll(videoCopy, audioCopy); videoPipe.Flush(); audioPipe.Flush(); videoPipe.Dispose(); audioPipe.Dispose(); process.WaitForExit(); }