public static async Task <string> AudioFileToMP3(string filePath, bool deleteOriginal = true)
        {
            if (!File.Exists(filePath))
            {
                throw new Exception("Couldn't locate file to convert.");
            }

            //Save file to the same location with changed extension
            string outputFilePath = Path.ChangeExtension(filePath, ".mp3");

            outputFilePath = MusicTagging.ReplaceInvalidChars(outputFilePath);         //replace any invalid chars in filePath with valid
            outputFilePath = MusicTagging.UpdateFileNameForDuplicates(outputFilePath); //add "copy" to filename if file exists


            //------------ Set library directory --------------
            MusicConverting.setFFMPEGPath();


            //get info of input file
            IMediaInfo mediaInfo = await MediaInfo.Get(filePath);

            IStream audioStream = mediaInfo.AudioStreams.FirstOrDefault();

            //------------ do the conversion ----------------
            await convertAudio(audioStream, outputFilePath);

            if (deleteOriginal)
            {
                File.Delete(filePath); //delete the original file
            }
            return(outputFilePath);
        }
Esempio n. 2
0
        public async Task <string> DownloadAudioMP3(string folderPath, string fileName = "")
        {
            if (StreamInfoSet == null)
            {
                var client = new YoutubeClient();
                StreamInfoSet = await client.GetVideoMediaStreamInfosAsync(videoID); // Get metadata for all streams in this video
            }



            if (fileName == "")
            {
                fileName = this.Video.Title; //set filename to the video title if not specified
            }
            string filePath = Path.Combine(folderPath, fileName);

            // Select one of the streams, e.g. highest quality muxed stream
            var streamInfo = this.StreamInfoSet.Audio.WithHighestBitrate();

            if (this.StreamInfoSet.Audio.Count == 0) //sometimes the libary can't seem to find the audio
            {
                throw new Exception("Failed to locate audio in chosen video.");
            }

            var savePath = await MusicConverting.YoutubeAudioToMP3(streamInfo.Url, streamInfo.AudioEncoding.ToString(), this.Video.Duration, filePath);

            return(savePath);
        }
        public const string FFmpegLibraryPath_Linux   = @"/usr/bin";                             //linux location

        public static async Task <string> YoutubeAudioToMP3(string audioLink, string audioFormat, TimeSpan audioDuration, string filePath)
        {
            //Save file to the same location with changed extension
            string outputFilePath = Path.ChangeExtension(filePath, ".mp3");

            outputFilePath = MusicTagging.ReplaceInvalidChars(outputFilePath);         //replace any invalid chars in filePath with valid
            outputFilePath = MusicTagging.UpdateFileNameForDuplicates(outputFilePath); //add "copy" to filename if file exists


            //------------ Set library directory --------------
            MusicConverting.setFFMPEGPath();

            //----- Get youtube video audio stream -----------------
            IStream audioStream = new WebStream(new Uri(audioLink), audioFormat, audioDuration);

            //--- do the conversion ----------------
            await convertAudio(audioStream, outputFilePath);

            return(outputFilePath);
        }