10行代码解析krc歌词文件

时间:2022-09-07 21:24:37

互联网上,我们常见的歌词格式有 LRC、TRC(天天动听歌词)、KRC(KuGou ResourCe,酷狗资源文件)和 QRC(QQ音乐歌词);在影视制作中,人们通常会用其他的卡拉 OK 字幕格式,例如 KSC(KBuilder 卡拉 OK 字幕描述脚本文件) 和 KAJ(Sayatoo 项目文件) 格式。

一、lrc文件实例

lrc意思是lyric

[ti:最炫民族风]

[ar:凤凰传奇]

[by:百度网友]

[00:23.25]苍茫的天涯是我的爱

[00:27.16]绵绵的青山脚下花正开

[00:30.88]什么样的节奏是最呀最摇摆

[00:34.64]什么样的歌声才是最开怀

首先有一些记录歌曲及歌词信息的东东,我们将其称作“ID 标签”(ID Tags),它可以包含歌曲名(ti)、专集(al)、歌手(ar)、歌词创建者(by)、歌词延迟调整(offset)等信息。

LRC 格式为每行歌词指定起始时刻,格式为[分钟:秒.百分秒]。

LRC文件的时间一般精确到行,如果精确到字,lyc文件也是可以的。只需要在每个字开头填充好时间即可。

在LRC基础上,有人搞出了增强LRC、扩展LRC,其实跟LRC差不多,毕竟歌词文件这个问题本身就很简单:建立歌词和时间之间的映射,顺便在文件头部添加一些歌曲信息。

二、krc文件

krc文件跟lrc文件很像,但是krc是二进制文件。在解析速度、存储空间上具有一定优势,但是可读性差(可是又有谁会去读歌词文件呢)。

krc文件像大多数二进制文件一样,开头四个字节是krc1,表示文件类型和版本号。接下来的内容需要先解密,然后解压缩。

import zlib

a = open("haha.krc", "rb").read()
encrypt_key = (64, 71, 97, 119, 94, 50, 116, 71, 81, 54, 49, 45, 206, 210, 110, 105)
print(a[:4]) # krc1
content = a[4:] # 前四个字节是magic number,表示版本号等
compress_content = bytes(content[i] ^ encrypt_key[i % len(encrypt_key)] for i in range(len(content)))
text_bytes = zlib.decompress(bytes(compress_content))
text = text_bytes.decode("utf8")
print(text)

参考资料

lrc歌词文件格式

https://zh.wikipedia.org/wiki/LRC格式

https://shansing.com/read/392/

https://www.jianshu.com/p/dfae11a9599b

附录:《后会无期》krc歌词

b'krc1'
[id:$01A3DC49]
[ar:邓紫棋]
[ti:后会无期]
[by:]
[hash:d48a9cb7c0b167aa3e041a69c94950b3]
[al:]
[sign:]
[qq:]
[total:223015]
[offset:102]
[language:eyJjb250ZW50IjpbXSwidmVyc2lvbiI6MX0=]
[1709,750]<0,150,0>作<150,150,0>词<300,150,0>: <450,150,0>韩<600,150,0>寒
[2459,1600]<0,150,0>作<150,150,0>曲<300,150,0>: <450,150,0>Arthur <600,200,0>Kent<800,300,0>、<1100,250,0>Dee <1350,250,0>Sylvia
[7971,5053]<0,1101,0>当<1101,200,0>一<1301,251,0>艘<1552,950,0>船<2502,450,0>沉<2952,349,0>入<3301,601,0>海<3902,1151,0>底
[14824,5401]<0,1251,0>当<1251,250,0>一<1501,251,0>个<1752,950,0>人<2702,450,0>成<3152,400,0>了<3552,1849,0>谜
[21876,2500]<0,900,0>你<900,500,0>不<1400,300,0>知<1700,800,0>道
[24826,2952]<0,250,0>他<250,300,0>们<550,650,0>为<1200,850,0>何<2050,252,0>离<2302,650,0>去
[28478,2651]<0,200,0>那<200,250,0>声<450,600,0>再<1050,751,0>见<1801,250,0>竟<2051,200,0>是<2251,400,0>他
[31678,2601]<0,250,0>最<250,301,0>后<551,249,0>一<800,1801,0>句
[35931,5103]<0,1101,0>当<1101,400,0>一<1501,301,0>辆<1802,950,0>车<2752,500,0>消<3252,300,0>失<3552,450,0>天<4002,1101,0>际
[43034,5249]<0,1299,0>当<1299,250,0>一<1549,251,0>个<1800,900,0>人<2700,450,0>成<3150,649,0>了<3799,1450,0>谜
[50084,2501]<0,950,0>你<950,551,0>不<1501,300,0>知<1801,700,0>道
[53085,3000]<0,250,0>他<250,300,0>们<550,1200,0>为<1750,251,0>何<2001,299,0>离<2300,700,0>去
[56585,2651]<0,250,0>就<250,351,0>像<601,600,0>你<1201,600,0>不<1801,250,0>知<2051,250,0>道<2301,350,0>这
[59837,2901]<0,299,0>竟<299,250,0>是<549,600,0>结<1149,1752,0>局
[63988,2002]<0,250,0>在<250,550,0>每<800,350,0>个<1150,551,0>繁<1701,301,0>星
[65990,3350]<0,600,0>抛<600,299,0>弃<899,301,0>银<1200,250,0>河<1450,249,0>的<1699,501,0>夜<2200,1150,0>里
[71040,1401]<0,300,0>我<300,451,0>会<751,200,0>告<951,450,0>别
[73440,3201]<0,200,0>告<200,300,0>别<500,651,0>我<1151,250,0>自<1401,1800,0>己
[77841,2752]<0,250,0>因<250,250,0>为<500,1200,0>我<1700,253,0>不<1953,249,0>知<2202,550,0>道
[81393,2901]<0,200,0>我<200,300,0>也<500,1200,0>不<1700,250,0>想<1950,251,0>知<2201,700,0>道
[85144,5653]<0,250,0>和<250,551,0>相<801,700,0>聚<1501,250,0>之<1751,300,0>间<2051,1400,0>的<3451,401,0>距<3852,1801,0>离
[92685,4752]<0,1150,0>当<1150,150,0>一<1300,250,0>辆<1550,950,0>车<2500,451,0>消<2951,349,0>失<3300,551,0>天<3851,901,0>际
[99537,5253]<0,1301,0>当<1301,201,0>一<1502,249,0>个<1751,952,0>人<2703,400,0>成<3103,650,0>了<3753,1500,0>谜
[106593,2501]<0,1001,0>你<1001,450,0>不<1451,300,0>知<1751,750,0>道
[109594,2899]<0,200,0>他<200,300,0>们<500,1199,0>为<1699,301,0>何<2000,249,0>离<2249,650,0>去
[113094,2602]<0,250,0>就<250,300,0>像<550,601,0>你<1151,651,0>不<1802,199,0>知<2001,250,0>道<2251,351,0>这
[116295,2951]<0,300,0>竟<300,301,0>是<601,800,0>结<1401,1550,0>局
[120480,1800]<0,251,0>在<251,549,0>每<800,300,0>个<1100,550,0>银<1650,150,0>河
[122480,3251]<0,500,0>坠<500,300,0>入<800,350,0>深<1150,250,0>谷<1400,350,0>的<1750,502,0>梦<2252,999,0>里
[127433,1350]<0,300,0>我<300,450,0>会<750,200,0>醒<950,400,0>来
[129483,2852]<0,200,0>也<200,400,0>忘<600,651,0>记<1251,450,0>梦<1701,1151,0>境
[134335,2900]<0,250,0>因<250,250,0>为<500,1300,0>你<1800,200,0>不<2000,250,0>知<2250,650,0>道
[137836,2851]<0,251,0>你<251,300,0>也<551,1200,0>不<1751,300,0>会<2051,250,0>知<2301,550,0>道
[141644,5203]<0,250,0>逝<250,550,0>去<800,700,0>的<1500,300,0>就<1800,250,0>已<2050,1451,0>经<3501,300,0>失<3801,1402,0>去
[148947,5251]<0,1351,0>当<1351,249,0>一<1600,250,0>艘<1850,1001,0>船<2851,449,0>沉<3300,300,0>入<3600,551,0>海<4151,1100,0>底
[156098,5002]<0,1100,0>当<1100,300,0>一<1400,301,0>个<1701,1050,0>人<2751,200,0>成<2951,500,0>了<3451,1551,0>谜
[163100,2452]<0,951,0>你<951,500,0>不<1451,300,0>知<1751,701,0>道
[166152,2850]<0,200,0>他<200,300,0>们<500,1200,0>为<1700,250,0>何<1950,300,0>离<2250,600,0>去
[169602,2651]<0,300,0>那<300,250,0>声<550,550,0>再<1100,651,0>见<1751,300,0>竟<2051,201,0>是<2252,399,0>他
[172804,3150]<0,300,0>最<300,300,0>后<600,549,0>一<1149,2001,0>句
[177256,5151]<0,1001,0>当<1001,350,0>一<1351,350,0>辆<1701,900,0>车<2601,300,0>消<2901,650,0>失<3551,350,0>天<3901,1250,0>际
[184257,5550]<0,1200,0>当<1200,350,0>一<1550,250,0>个<1800,950,0>人<2750,250,0>成<3000,950,0>了<3950,1600,0>谜
[191357,2453]<0,902,0>你<902,550,0>不<1452,301,0>知<1753,700,0>道
[194310,3000]<0,250,0>他<250,300,0>们<550,1051,0>为<1601,449,0>何<2050,250,0>离<2300,700,0>去
[197860,3251]<0,251,0>就<251,300,0>像<551,650,0>你<1201,500,0>不<1701,350,0>知<2051,300,0>道<2351,900,0>这
[204480,223015]<0,350,0>竟<350,350,0>是<700,701,0>结<1401,2100,0>局