示例#1
0
        public async static Task AddTags(IMusicInformation information, string path)
        {
            var filePath = new FileInfo(path);

            if (!filePath.Exists)
            {
                return;
            }
            try
            {
                using (var file = File.Create(filePath.FullName))
                {
                    file.Tag.Album      = information.Album;
                    file.Tag.Performers = new[] { information.Artist };
                    file.Tag.Year       = information.Year;
                    if (information.Genres != null)
                    {
                        file.Tag.Genres = information.Genres.Select(x => x.ToString()).ToArray();
                    }
                    file.Tag.Title = information.Title;
                    var image = await information.GetImage();

                    if (image != null)
                    {
                        byte[]            data;
                        JpegBitmapEncoder encoder = new JpegBitmapEncoder();
                        encoder.Frames.Add(BitmapFrame.Create(image));
                        using (MemoryStream ms = new MemoryStream())
                        {
                            encoder.Save(ms);
                            data = ms.ToArray();
                        }
                        file.Tag.Pictures = new IPicture[] { new Picture(new ByteVector(data, data.Length)) };
                    }
                    await Task.Run(() => file.Save());
                }
            }
            catch (CorruptFileException)
            {
            }
        }
示例#2
0
        public async static Task AddTags(IMusicInformation information, string path)
        {
            var filePath = new FileInfo(path);

            if (!filePath.Exists)
            {
                return;
            }
            try
            {
                var songResult = information.WebTrack;
                using (var file = File.Create(filePath.FullName))
                {
                    Tag.DefaultVersion      = 3;
                    Tag.ForceDefaultVersion = true;
                    Tag.DefaultEncoding     = StringType.UTF8;
                    if (file == null)
                    {
                        return;
                    }
                    if (filePath.FullName.Contains("ogg"))
                    {
                        var id3V1 = file.GetTag(TagTypes.Id3v1, true);
                        if (!string.IsNullOrEmpty(songResult.SongName))
                        {
                            id3V1.Title = songResult.SongName;
                        }
                        if (!string.IsNullOrEmpty(songResult.ArtistName))
                        {
                            id3V1.Performers = songResult.ArtistName.Split(new[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
                        }
                        if (!string.IsNullOrEmpty(songResult.AlbumName))
                        {
                            id3V1.Album = songResult.AlbumName;
                        }
                        if (songResult.TrackNum != 0)
                        {
                            id3V1.Track = Convert.ToUInt32(songResult.TrackNum);
                        }
                        id3V1.Disc = Convert.ToUInt32(songResult.Disc);
                        if (!string.IsNullOrEmpty(songResult.Year))
                        {
                            id3V1.Year = Convert.ToUInt32(songResult.Year.Substring(0, 4));
                        }
                        id3V1.Comment = "雅音FM";
                    }
                    else
                    {
                        TagLib.Tag tags;
                        if (filePath.FullName.Contains("ape"))
                        {
                            tags = file.GetTag(TagTypes.Ape, true);
                        }
                        else if (filePath.FullName.Contains("flac"))
                        {
                            tags = file.GetTag(TagTypes.FlacMetadata, true);
                        }
                        else
                        {
                            tags = (Tag)file.GetTag(TagTypes.Id3v2, true);
                        }
                        var picBasePath = Path.Combine(Environment.CurrentDirectory, "Pic");
                        if (!Directory.Exists(picBasePath))
                        {
                            Directory.CreateDirectory(picBasePath);
                        }
                        var picPath = Path.Combine(picBasePath, songResult.SongId + ".jpg");
                        try
                        {
                            if (!string.IsNullOrEmpty(songResult.PicUrl))
                            {
                                var picLocation = CommonHelper.GetLocation(songResult.PicUrl);
                                if (!string.IsNullOrEmpty(picLocation))
                                {
                                    if (!System.IO.File.Exists(picPath))
                                    {
                                        await new WebClient().DownloadFileTaskAsync(picLocation, picPath);
                                    }
                                }
                            }
                        }
                        catch (Exception ex)
                        {
                            CommonHelper.AddLog(ex.ToString());
                        }
                        try
                        {
                            if (System.IO.File.Exists(picPath))
                            {
                                var picture = new Picture(picPath)
                                {
                                    Description = "itwusun.com",
                                    MimeType    = MediaTypeNames.Image.Jpeg,
                                    Type        = PictureType.FrontCover
                                };
                                tags.Pictures = new IPicture[] { picture };
                            }
                        }
                        catch (Exception ex)
                        {
                            CommonHelper.AddLog(ex.ToString());
                        }
                        if (!string.IsNullOrEmpty(songResult.SongName))
                        {
                            tags.Title = songResult.SongName;
                        }
                        if (!string.IsNullOrEmpty(songResult.ArtistName))
                        {
                            tags.Performers = songResult.ArtistName.Split(new[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
                        }
                        if (!string.IsNullOrEmpty(songResult.AlbumName))
                        {
                            tags.Album = songResult.AlbumName;
                        }
                        if (!string.IsNullOrEmpty(songResult.AlbumArtist))
                        {
                            tags.AlbumArtists = songResult.AlbumArtist.Split(new[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
                        }
                        if (songResult.TrackNum != 0)
                        {
                            tags.Track = Convert.ToUInt32(songResult.TrackNum);
                        }
                        tags.Disc      = Convert.ToUInt32(songResult.Disc);
                        tags.Copyright = "雅音FM";
                        if (!string.IsNullOrEmpty(songResult.Year))
                        {
                            tags.Year = Convert.ToUInt32(songResult.Year.Substring(0, 4));
                            if (tags.TagTypes == TagTypes.Id3v2)
                            {
                                var dat = TextInformationFrame.Get((Tag)tags, "TDAT", true);
                                dat.Text = new[] { songResult.Year };
                            }
                        }
                        if (tags.TagTypes == TagTypes.Id3v2)
                        {
                            if (!string.IsNullOrEmpty(songResult.Company))
                            {
                                var cmp = TextInformationFrame.Get((Tag)tags, "TPUB", true);
                                cmp.Text = new[] { songResult.Company };
                            }
                            if (!string.IsNullOrEmpty(songResult.Language))
                            {
                                var cmp = TextInformationFrame.Get((Tag)tags, "TLAN", true);
                                cmp.Text = new[] { songResult.Language };
                            }
                            if (songResult.Length != 0)
                            {
                                var cmp = TextInformationFrame.Get((Tag)tags, "TLEN", true);
                                cmp.Text = new[] { songResult.Length.ToString() };
                            }
                            if (!string.IsNullOrEmpty(songResult.SongSubName))
                            {
                                var title = TextInformationFrame.Get((Tag)tags, "TIT3", true);
                                title.Text = new[] { songResult.SongSubName };
                            }
                        }
                        if (!string.IsNullOrEmpty(songResult.LrcUrl))
                        {
                            try
                            {
                                var html = await new WebClient {
                                    Encoding = Encoding.UTF8
                                }.DownloadStringTaskAsync(new Uri(songResult.LrcUrl));
                                if (!string.IsNullOrEmpty(html))
                                {
                                    html        = HttpUtility.HtmlDecode(html);
                                    html        = HttpUtility.HtmlDecode(html);
                                    tags.Lyrics = html;
                                }
                            }
                            catch (Exception)
                            {
                                //
                            }
                        }
                        try
                        {
                            System.IO.File.Delete(picPath);
                        }
                        catch (Exception)
                        {
                            //
                        }
                        // ReSharper disable once AccessToDisposedClosure
                        await Task.Run(() => file.Save());
                    }
                }
            }
            catch (Exception ex)
            {
                CommonHelper.AddLog(ex.ToString());
            }
        }
示例#3
0
        public async static Task <bool> DownloadAndConfigureTrack(IDownloadable downloadInformation, IMusicInformation musicInformation, string fileName, Action <double> progressChangedAction, DownloadSettings settings)
        {
            if (string.IsNullOrEmpty(CommonHelper.GetLocation(downloadInformation.DownloadParameter)))
            {
                return(false);
            }
            if (!await DownloadTrack(downloadInformation, fileName, progressChangedAction))
            {
                return(false);
            }

            if (settings.IsConverterEnabled)
            {
                var oldFile = new FileInfo(fileName);
                oldFile.MoveTo(GeneralHelper.GetFreeFileName(oldFile.Directory, oldFile.Extension).FullName); //We move the downloaded file to a temp location
                await ffmpeg.ConvertFile(oldFile.FullName, fileName, settings.Bitrate, settings.Format);
            }

            //TagLib# destroys all aac files...
            if (settings.AddTags && settings.Format != AudioFormat.AAC)
            {
                await AddTags(musicInformation, fileName);
            }
            return(true);
        }
示例#4
0
        public static async Task AddTags(IMusicInformation information, string path)
        {
            var filePath = new FileInfo(path);

            if (!filePath.Exists)
            {
                return;
            }
            try
            {
                var songResult = information.WebTrack;
                using (var file = File.Create(filePath.FullName))
                {
                    Tag.DefaultVersion      = 3;
                    Tag.ForceDefaultVersion = true;
                    Tag.DefaultEncoding     = StringType.UTF8;
                    if (file == null)
                    {
                        return;
                    }
                    if (filePath.FullName.Contains("ogg"))
                    {
                        var id3V1 = file.GetTag(TagTypes.Id3v1, true);
                        if (!string.IsNullOrEmpty(songResult.SongName))
                        {
                            id3V1.Title = songResult.SongName;
                        }
                        if (!string.IsNullOrEmpty(songResult.ArtistName))
                        {
                            id3V1.Performers = songResult.ArtistName.Split(new[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
                        }
                        if (!string.IsNullOrEmpty(songResult.AlbumName))
                        {
                            id3V1.Album = songResult.AlbumName;
                        }
                        if (songResult.TrackNum != 0)
                        {
                            id3V1.Track = Convert.ToUInt32(songResult.TrackNum);
                        }
                        id3V1.Disc = Convert.ToUInt32(songResult.Disc);
                        if (!string.IsNullOrEmpty(songResult.Year))
                        {
                            id3V1.Year = Convert.ToUInt32(songResult.Year.Substring(0, 4));
                        }
                        id3V1.Comment = "雅音FM";
                    }
                    else
                    {
                        TagLib.Tag tags;
                        if (filePath.FullName.Contains("ape"))
                        {
                            tags = file.GetTag(TagTypes.Ape, true);
                        }
                        else if (filePath.FullName.Contains("flac"))
                        {
                            tags = file.GetTag(TagTypes.FlacMetadata, true);
                        }
                        else
                        {
                            tags = (Tag)file.GetTag(TagTypes.Id3v2, true);
                        }
                        if (!string.IsNullOrEmpty(songResult.SongName))
                        {
                            tags.Title = songResult.SongName;
                        }
                        if (!string.IsNullOrEmpty(songResult.ArtistName))
                        {
                            tags.Performers = songResult.ArtistName.Split(new[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
                        }
                        if (!string.IsNullOrEmpty(songResult.AlbumName))
                        {
                            tags.Album = songResult.AlbumName;
                        }
                        if (!string.IsNullOrEmpty(songResult.AlbumArtist))
                        {
                            tags.AlbumArtists = songResult.AlbumArtist.Split(new[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
                        }
                        if (songResult.TrackNum != 0)
                        {
                            tags.Track = Convert.ToUInt32(songResult.TrackNum);
                        }
                        tags.Disc      = Convert.ToUInt32(songResult.Disc);
                        tags.Copyright = "雅音FM";
                        if (!string.IsNullOrEmpty(songResult.Year))
                        {
                            tags.Year = Convert.ToUInt32(songResult.Year.Substring(0, 4));
                            if (tags.TagTypes == TagTypes.Id3v2)
                            {
                                var dat = TextInformationFrame.Get((Tag)tags, "TDAT", true);
                                dat.Text = new[] { songResult.Year };
                            }
                        }
                        if (tags.TagTypes == TagTypes.Id3v2)
                        {
                            if (!string.IsNullOrEmpty(songResult.Company))
                            {
                                var cmp = TextInformationFrame.Get((Tag)tags, "TPUB", true);
                                cmp.Text = new[] { songResult.Company };
                            }
                            if (!string.IsNullOrEmpty(songResult.Language))
                            {
                                var cmp = TextInformationFrame.Get((Tag)tags, "TLAN", true);
                                cmp.Text = new[] { songResult.Language };
                            }
                            if (songResult.Length != 0)
                            {
                                var cmp = TextInformationFrame.Get((Tag)tags, "TLEN", true);
                                cmp.Text = new[] { songResult.Length.ToString() };
                            }
                            if (!string.IsNullOrEmpty(songResult.SongSubName))
                            {
                                var title = TextInformationFrame.Get((Tag)tags, "TIT3", true);
                                title.Text = new[] { songResult.SongSubName };
                            }
                        }
                        if (!string.IsNullOrEmpty(songResult.LrcUrl))
                        {
                            try
                            {
                                var html = await new WebClient {
                                    Encoding = Encoding.UTF8
                                }.DownloadStringTaskAsync(songResult.LrcUrl);
                                if (!string.IsNullOrEmpty(html))
                                {
                                    html        = HttpUtility.HtmlDecode(html);
                                    html        = HttpUtility.HtmlDecode(html);
                                    tags.Lyrics = html;
                                    if (AnyListenSettings.Instance.Config.DownLrc)
                                    {
                                        // ReSharper disable once StringLastIndexOfIsCultureSpecific.1
                                        var lrcPath = filePath.FullName.Substring(0, filePath.FullName.LastIndexOf(".")) +
                                                      ".lrc";
                                        System.IO.File.WriteAllText(lrcPath, html);
                                    }
                                }
                            }
                            catch (Exception)
                            {
                                //
                            }
                        }

                        try
                        {
                            if (!string.IsNullOrEmpty(songResult.PicUrl))
                            {
                                var datas   = new WebClient().DownloadData(songResult.PicUrl);
                                var picture = new Picture
                                {
                                    Description = "yyfm",
                                    MimeType    = MediaTypeNames.Image.Jpeg,
                                    Type        = PictureType.FrontCover,
                                    Data        = new ByteVector(datas, datas.Length)
                                };
                                tags.Pictures = new IPicture[] { picture };
                            }
                        }
                        catch (Exception ex)
                        {
                            CommonHelper.AddLog(ex.ToString());
                        }
                        // ReSharper disable once AccessToDisposedClosure
                        await Task.Run(() => file.Save());
                    }
                }
            }
            catch (Exception ex)
            {
                CommonHelper.AddLog(ex.ToString());
            }
        }
示例#5
0
        public async static Task <bool> DownloadAndConfigureTrack(IDownloadable downloadInformation, IMusicInformation musicInformation, string fileName, Action <double> progressChangedAction, DownloadSettings settings)
        {
            var link = CommonHelper.GetLocation(downloadInformation.DownloadParameter);

            if (string.IsNullOrEmpty(link))
            {
                return(false);
            }
            downloadInformation.DownloadParameter = link;
            var format = CommonHelper.GetFormat(link);

            if (!fileName.EndsWith(format))
            {
                fileName =
                    // ReSharper disable once StringLastIndexOfIsCultureSpecific.1
                    fileName.Substring(0, fileName.LastIndexOf(".")) + format;
            }
            downloadInformation.DownloadFilename = fileName;
            if (!await DownloadTrack(downloadInformation, fileName, progressChangedAction))
            {
                return(false);
            }

            if (settings.IsConverterEnabled)
            {
                var oldFile = new FileInfo(fileName);
                oldFile.MoveTo(GeneralHelper.GetFreeFileName(oldFile.Directory, oldFile.Extension).FullName); //We move the downloaded file to a temp location
                await ffmpeg.ConvertFile(oldFile.FullName, fileName, settings.Bitrate, settings.Format);
            }

            //TagLib# destroys all aac files...
            if (settings.AddTags && settings.Format != AudioFormat.AAC)
            {
                await AddTags(musicInformation, fileName);
            }
            return(true);
        }