コード例 #1
0
        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);
        }
コード例 #2
0
        public async Task <string> DownloadAudioNative(string folderPath, string fileName = "")
        {
            var client = new YoutubeClient();

            if (StreamInfoSet == null)
            {
                StreamInfoSet = await client.GetVideoMediaStreamInfosAsync(videoID); // Get metadata for all streams in this video
            }
            // Select one of the streams, e.g. highest quality muxed stream
            var streamInfo = this.StreamInfoSet.Audio.WithHighestBitrate();

            // Get file extension based on stream's container
            var ext = streamInfo.Container.GetFileExtension();

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

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

            var fs = File.Create(filePath);
            await client.DownloadMediaStreamAsync(streamInfo, fs); //wait for the download to finish

            fs.Close();

            return(filePath);
        }
コード例 #3
0
        public async Task Download()
        {
            try
            {
                StartTime = DateTime.Now;
                Status    = DownloadStates.Downloading;
                OnChange?.Invoke();

                var savePath = await Video.DownloadAudioMP3(YoutubeVideo.DefaultDlFolder);

                await Video.TagMP3File(savePath);

                if (TaggedSong != null)
                {
                    await TaggedSong.TagMP3File(savePath, _taggedSongReleaseIndex);
                }

                MusicTagging.Folderize(savePath, YoutubeVideo.DefaultDlFolder);

                this.Status = DownloadStates.DownloadComplete;
            }
            catch (Exception ex)
            {
                Status    = DownloadStates.Error;
                ErrorText = ex.ToString();
            }
            finally
            {
                OnChange?.Invoke(); //raise event that status has changed
            }
        }
コード例 #4
0
        public async Task TagMP3File(string filePath, int releaseIndex)
        {
            var tfile = TagLib.File.Create(filePath);

            // change title in the file
            tfile.Tag.Title = this.Name;
            tfile.Tag.MusicBrainzTrackId = this.MBID;

            //change artists
            if (Artists.Count > 0)
            {
                tfile.Tag.Performers = new String[1] {
                    this.ArtistsNameString
                };
                tfile.Tag.MusicBrainzReleaseArtistId = this.Artists[0].MBID;
            }

            //change release info
            if (releaseIndex < Releases.Count && releaseIndex >= 0)
            {
                var release = Releases[releaseIndex];

                tfile.Tag.Album = release.Name;
                tfile.Tag.MusicBrainzReleaseId      = release.MBID;
                tfile.Tag.MusicBrainzReleaseCountry = release.Country;

                //-------------- Safely parse track num and count---------------------

                uint tmpVal = 0;

                if (uint.TryParse(release.SongTrackNum, out tmpVal))
                {
                    tfile.Tag.Track = uint.Parse(release.SongTrackNum);
                }

                if (uint.TryParse(release.TrackCount, out tmpVal))
                {
                    tfile.Tag.TrackCount = uint.Parse(release.TrackCount);
                }

                //-------------------- get the album art -------------------
                await release.GetCoverArt(); //query the server for the link

                if (release.CoverArtLink != Release.CoverArtLinkNotFound)
                {
                    MusicTagging.addPictureNoSave(tfile, await MBServer.GetImage(release.CoverArtLink));
                }
            }

            tfile.Save();
        }
コード例 #5
0
        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);
        }
コード例 #6
0
        public async Task TagMP3File(string filePath)
        {
            var    tfile = TagLib.File.Create(filePath);
            string title = tfile.Tag.Title;

            // change title only if one doesn't exist
            if (tfile.Tag.Title == "" || tfile.Tag.Title == null)
            {
                tfile.Tag.Title = this.Video.Title;
            }

            // add video thumbnail
            var thumbUrl = this.Video.Thumbnails.HighResUrl;

            if (thumbUrl != "" && thumbUrl != null)
            {
                MusicTagging.addPictureNoSave(tfile, await MBServer.GetImage(thumbUrl));
            }

            tfile.Save();
        }