/// <summary> /// A class for downloading audio files so that we can play them with the <see cref="AudioService"/> /// </summary> /// <param name="message">The 'base' message that we will modify over time to tell the user what we are up to</param> /// <param name="guild">The guild that the command was executed in</param> /// <param name="maxVideoTime">The max video length we will download</param> /// <param name="downloadLocation">The place to download to</param> /// <param name="audioFileContainer">The audio files container</param> public MusicDownloader(IUserMessage message, SocketGuild guild, TimeSpan maxVideoTime, string downloadLocation = "Music/", MusicFileFormat audioFileContainer = MusicFileFormat.Mp3) { //Setup our cancellation token cancellationSource = new CancellationTokenSource(); downloadCancellationToken = cancellationSource.Token; this.message = message; this.guild = guild; maxAudioTime = maxVideoTime; this.downloadLocation = downloadLocation; downloadFileContainer = audioFileContainer; }
/// <summary> /// Searches music folder for similar or same results to <see cref="search"/> /// </summary> /// <param name="search">The name of the song to search for</param> /// <param name="fileFormat"></param> /// <returns>Returns the first found similar or matching result</returns> public static string SearchMusicDirectory(string search, MusicFileFormat fileFormat) { if (!Directory.Exists(MusicDir)) { Directory.CreateDirectory(MusicDir); } DirectoryInfo hdDirectoryInWhichToSearch = new DirectoryInfo(MusicDir); FileInfo[] filesInDir = hdDirectoryInWhichToSearch.GetFiles($"*{search}*.{fileFormat.GetFormatExtension()}"); return(filesInDir.Select(foundFile => foundFile.FullName).FirstOrDefault()); }
private readonly IYouTubeSearcher youTubeSearcher; //Default: YouTubeService public StandardMusicDownloader(string musicDir, MusicFileFormat musicFileFormat, HttpClient httpClient, CancellationTokenSource cancelSource) { if (!Directory.Exists(musicDir)) { Directory.CreateDirectory(musicDir); } musicDirectory = musicDir; fileFormat = musicFileFormat; cancellationTokenSource = cancelSource; audioConverter = new FfmpegAudioConverter(cancelSource.Token); musicDownloader = new YouTubeExplodeDownloader(musicDir, httpClient, cancelSource.Token); youTubeSearcher = new YouTubeService(httpClient); }
public async Task <string> ConvertFileToAudio(string originalLocation, string location, bool deleteOriginal = true, MusicFileFormat musicFileFormat = MusicFileFormat.Mp3) { try { string fullNewLocation = $"{location}{Path.GetFileName(originalLocation).Replace(Path.GetExtension(originalLocation), "")}.{musicFileFormat.GetFormatExtension()}"; Logger.Log($"Converting '{originalLocation}' to '{fullNewLocation}'...", LogVerbosity.Debug); //Start our ffmpeg process Process ffmpeg = new Process { StartInfo = new ProcessStartInfo { FileName = Config.bot.AudioSettings.FfmpegLocation, Arguments = $"-i \"{originalLocation}\" -ar 48000 -y \"{fullNewLocation}\"" } }; ffmpeg.Start(); while (!ffmpeg.HasExited) { if (cancellationToken.IsCancellationRequested) { ffmpeg.Kill(true); ffmpeg.Dispose(); return(null); } await Task.Delay(50); } ffmpeg.Dispose(); //Delete our old file if (deleteOriginal) { //if (File.Exists(originalLocation)) //File.Delete(originalLocation); //else //Were the f**k is our fileToConvert then?? This should never happen but it is here anyway //return null; } //So obviously there was an issue converting... if (!File.Exists(fullNewLocation)) { Logger.Log("There was an issue converting the file!", LogVerbosity.Debug); return(null); } //Ayy, we converted Logger.Log($"Successfully converted to '{fullNewLocation}'.", LogVerbosity.Debug); return(fullNewLocation); } catch (NullReferenceException ex) { #if DEBUG Logger.Log($"Null reference exception while trying to convert a song! FFMPEG path could be set incorrectly!\n{ex}", LogVerbosity.Error); #else Logger.Log($"Null refrence exception while trying to convert a song! FFMPEG path could be set incorrectly!\n{ex.Message}", LogVerbosity.Error); #endif return(null); } catch (Exception ex) { #if DEBUG Logger.Log(ex.ToString(), LogVerbosity.Error); #else Logger.Log(ex.Message, LogVerbosity.Error); #endif return(null); } }
public static string GetFormatExtension(this MusicFileFormat fileFormat) { string fileFormatInString = fileFormat.ToString(); return(fileFormatInString.ToLowerInvariant()); }
public async Task <string> ConvertFileToAudio(string originalLocation, string location, bool deleteOriginal = true, MusicFileFormat musicFileFormat = MusicFileFormat.Mp3) { try { string fullNewLocation = $"{location}{Path.GetFileName(originalLocation).Replace(Path.GetExtension(originalLocation), "")}.{musicFileFormat.GetFormatExtension()}"; Logger.Debug("Converting {@OriginalLocation} to {@FullNewLocation}...", originalLocation, fullNewLocation); //Start our ffmpeg process Process ffmpeg = new Process { StartInfo = new ProcessStartInfo { FileName = $"{Config.bot.AudioSettings.ExternalDirectory}ffmpeg", Arguments = $"-loglevel fatal -nostdin -i \"{originalLocation}\" -ar 48000 -y \"{fullNewLocation}\"", CreateNoWindow = true, UseShellExecute = false, RedirectStandardOutput = false } }; ffmpeg.Start(); while (!ffmpeg.HasExited) { if (cancellationToken.IsCancellationRequested) { ffmpeg.Kill(true); ffmpeg.Dispose(); return(null); } await Task.Delay(50); } ffmpeg.Dispose(); //Delete our old file if (deleteOriginal) { if (File.Exists(originalLocation)) { File.Delete(originalLocation); } else //Were the f**k is our fileToConvert then?? This should never happen but it is here anyway { return(null); } } //So obviously there was an issue converting... if (!File.Exists(fullNewLocation)) { Logger.Debug("There was an issue converting the file!"); return(null); } //Ayy, we converted Logger.Debug($"Successfully converted to '{fullNewLocation}'."); return(fullNewLocation); } catch (NullReferenceException ex) { Logger.Error( "Null reference exception while trying to convert a song! FFMPEG path could be set incorrectly! {@Exception}", ex); return(null); } catch (Exception ex) { Logger.Error("An error occured while trying to convert a video! {@Exception}", ex); return(null); } }