Home > php教程 > php手册 > 酷狗krc歌词解析并转换为lrc歌词php版

酷狗krc歌词解析并转换为lrc歌词php版

WBOY
Release: 2016-05-23 08:34:00
Original
1931 people have browsed it

最近在进行一次对酷狗音乐歌词采集时发现酷狗音乐的歌词直接浏览都是"乱码",自己平时所见的歌词都是lrc格式的文本,这种酷狗专用的krc格式的显然是经过特别处理过的,平时用酷狗听音乐也没仔细看他的歌词有什么不同,只是与天天静听等不同的是可以逐字高亮显示歌词.

于是搜索了下发现有个java版的半成品解密代码,测试了下竟然还是"乱码",巨坑!于是只有采用最原始的办法,对酷狗的flash播放器进行反编译,发现这段krc解密的ActionScript代码:

public function loaderCompleteHandler(param1:ByteArray) : void{  
this.newkeyBytes = [64, 71, 97, 119, 94, 50, 116, 71, 81, 54, 49, 45, 206, 210, 110, 105];  
var result:* = param1;  
var krcBytes:* = new ByteArray();  
result.position = 4;  
result.readBytes(krcBytes);  
var l:* = krcBytes.length;  
var i:int;  
while (i < l) { 
 krcBytes[i] = krcBytes[i] ^ this.newkeyBytes[i % this.newkeyBytes.length];  
 i = (i + 1);  
} 
try {  
 krcBytes.uncompress();  
} catch (error:SecurityError) {  
 errorfun("uncompressError"); 
 return;  
}  
krcBytes.position = 0;  
this.myLyric.dataStr = krcBytes.readUTFBytes(krcBytes.length);  
this.successfun();  
return;  
}
Copy after login

根据这段代码可以很快转换成php解析代码:

$enKey = array(64, 71, 97, 119, 94, 50, 116, 71, 81, 54, 49, 45, 206, 210, 110, 105); 
$krc_content = substr($krc_content,4); 
$len = strlen($krc_content); 
$krc_compress = &#39;&#39;; 
for ($k = 0; $k < $len; $k++){ 
 $krc_compress .= chr(ord($krc_content[$k]) ^ $enKey[$k % 16]); 
}
Copy after login

$krc_text = gzuncompress($krc_compress);

上面的$krc_content为krc歌词压缩加密的文本内容,得到了解析后的krc明文文本歌词$krc_text,然后就可以很容易的转换成lrc歌词了,代码如下:

/** 
 * krc歌词解析并转换为lrc歌词 
 * $Name: krc2lrc  2014-4-18  qaulau<qaulau@hotmail.com>$ 
 *  
 * @param string $krc_content 加密的krc歌词内容 
 * @param string $glue   处理后的lrc歌词连接字符,默认为换行符n 
 */ 
function krc2lrc($krc_content,$glue = "n"){ 
$enKey = array(64, 71, 97, 119, 94, 50, 116, 71, 81, 54, 49, 45, 206, 210, 110, 105); 
$krc_content = substr($krc_content,4); 
$len = strlen($krc_content); 
$krc_compress = &#39;&#39;; 
for ($k = 0; $k < $len; $k++){ 
$krc_compress .= chr(ord($krc_content[$k]) ^ $enKey[$k % 16]); 
} 
$krc_text = gzuncompress($krc_compress); 
preg_match_all("/[(ar|ti|offset):(.+)]/",$krc_text,$matches); 
$lrc_text = implode($glue,$matches[0]).$glue; 
preg_match_all("/[(d{0,8}),(d{0,8})](.+)/",$krc_text,$matches); 
unset($krc_text,$krc_compress,$krc_content,$enKey); 
if(!emptyempty($matches[1])){ 
foreach($matches[1] as $k=>$v){ 
list($seconds, $millis) = explode(&#39;.&#39;, $v / 1000);  
$lrc_time = date(&#39;i:s&#39;,1388534400 + $seconds).&#39;.&#39;.substr($millis,0,2); 
$lrc_cont = preg_replace("/<d{1,8},d{1,8},d{1,8}>/",&#39;&#39;,$matches[3][$k]);   
$lrc_text .= "[{$lrc_time}]{$lrc_cont}{$glue}"; 
} 
} 
 return $lrc_text; 
}
Copy after login

简单的示例:

$content = file_get_contents(&#39;1.krc&#39;); 
echo krc2lrc($content,&#39;<br/>&#39;);
Copy after login

分享一下,方便日后有类似需要的人查询.


本文地址:

转载随意,但请附上文章地址:-)

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template