From deb588564a267ff64e6371e5b9bbc770a4ac91d3 Mon Sep 17 00:00:00 2001 From: Zhong Yang Date: Tue, 8 Sep 2020 11:08:42 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9EFLAC=E6=A0=BC=E5=BC=8F?= =?UTF-8?q?=E5=86=85=E5=B5=8C=E6=AD=8C=E8=AF=8D=E7=9A=84=E8=AF=BB=E5=86=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- MusicPlayer2/AudioTag.cpp | 4 +++- MusicPlayer2/TagLibHelper.cpp | 27 ++++++++++++++++++++++++--- MusicPlayer2/TagLibHelper.h | 1 + 3 files changed, 28 insertions(+), 4 deletions(-) diff --git a/MusicPlayer2/AudioTag.cpp b/MusicPlayer2/AudioTag.cpp index 27253b1d..5d31877b 100644 --- a/MusicPlayer2/AudioTag.cpp +++ b/MusicPlayer2/AudioTag.cpp @@ -216,6 +216,8 @@ bool CAudioTag::WriteAudioLyric(const wstring& lyric_contents) { case AU_MP3: return CTagLibHelper::WriteMpegLyric(m_song_info.file_path, lyric_contents); + case AU_FLAC: + return CTagLibHelper::WriteFlacLyric(m_song_info.file_path, lyric_contents); default: break; } @@ -325,6 +327,6 @@ bool CAudioTag::IsFileTypeLyricWriteSupport(const wstring& ext) wstring _ext = ext; CCommon::StringTransform(_ext, false); AudioType type = CAudioCommon::GetAudioTypeByFileExtension(_ext); - return type == AU_MP3; + return type == AU_MP3 || type == AU_FLAC; } diff --git a/MusicPlayer2/TagLibHelper.cpp b/MusicPlayer2/TagLibHelper.cpp index 533c34cd..0148651e 100644 --- a/MusicPlayer2/TagLibHelper.cpp +++ b/MusicPlayer2/TagLibHelper.cpp @@ -32,6 +32,8 @@ using namespace TagLib; #define STR_MP4_LYRICS_TAG "----:com.apple.iTunes:Lyrics" #define STR_ASF_COVER_TAG "WM/Picture" #define STR_APE_COVER_TAG "COVER ART (FRONT)" +#define STR_ID3V2_LYRIC_TAG "USLT" +#define STR_FLAC_LYRIC_TAG "LYRICS" //将taglib中的字符串转换成wstring类型。 //由于taglib将所有非unicode编码全部作为Latin编码处理,因此无法正确处理本地代码页 @@ -657,7 +659,7 @@ wstring CTagLibHelper::GetMpegLyric(const wstring& file_path) if (id3v2 != nullptr) { auto frame_list_map = id3v2->frameListMap(); - auto lyric_frame = frame_list_map["USLT"]; + auto lyric_frame = frame_list_map[STR_ID3V2_LYRIC_TAG]; if (!lyric_frame.isEmpty()) lyrics = lyric_frame.front()->toString().toWString(); } @@ -684,7 +686,7 @@ wstring CTagLibHelper::GetFlacLyric(const wstring& file_path) wstring lyrics; FLAC::File file(file_path.c_str()); auto properties = file.properties(); - auto lyric_item = properties["LYRICS"]; + auto lyric_item = properties[STR_FLAC_LYRIC_TAG]; if (!lyric_item.isEmpty()) { lyrics = lyric_item.front().toWString(); @@ -714,7 +716,7 @@ bool CTagLibHelper::WriteMpegLyric(const wstring& file_path, const wstring& lyri if (id3v2 != nullptr) { //先删除歌词帧 - auto lyric_frame_list = id3v2->frameListMap()["USLT"]; + auto lyric_frame_list = id3v2->frameListMap()[STR_ID3V2_LYRIC_TAG]; if (!lyric_frame_list.isEmpty()) { for (auto frame : lyric_frame_list) @@ -736,6 +738,25 @@ bool CTagLibHelper::WriteMpegLyric(const wstring& file_path, const wstring& lyri return saved; } +bool CTagLibHelper::WriteFlacLyric(const wstring& file_path, const wstring& lyric_contents) +{ + FLAC::File file(file_path.c_str()); + auto properties = file.properties(); + if (lyric_contents.empty()) + { + properties.erase(STR_FLAC_LYRIC_TAG); + } + else + { + StringList lyric_item; + lyric_item.append(lyric_contents); + properties[STR_FLAC_LYRIC_TAG] = lyric_item; + } + file.setProperties(properties); + bool saved = file.save(); + return saved; +} + bool CTagLibHelper::WriteMp3AlbumCover(const wstring& file_path, const wstring& album_cover_path, bool remove_exist) { MPEG::File file(file_path.c_str()); diff --git a/MusicPlayer2/TagLibHelper.h b/MusicPlayer2/TagLibHelper.h index b4226ccd..fcfcc351 100644 --- a/MusicPlayer2/TagLibHelper.h +++ b/MusicPlayer2/TagLibHelper.h @@ -48,6 +48,7 @@ public: //写入歌词 static bool WriteMpegLyric(const wstring& file_path, const wstring& lyric_contents); + static bool WriteFlacLyric(const wstring& file_path, const wstring& lyric_contents); //写专辑封面 static bool WriteMp3AlbumCover(const wstring& file_path, const wstring& album_cover_path, bool remove_exist = true);