Home  >  Article  >  Backend Development  >  将酷狗krc歌词解析并转换为lrc歌词php源码_PHP

将酷狗krc歌词解析并转换为lrc歌词php源码_PHP

WBOY
WBOYOriginal
2016-06-01 11:51:461050browse

酷狗

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

对酷狗的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; 
}

根据这段代码可以很快转换成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 = '';
for ($k = 0; $k < $len; $k++){
 $krc_compress .= chr(ord($krc_content[$k]) ^ $enKey[$k % 16]);
}
$krc_text = gzuncompress($krc_compress);

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

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 = '';
 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(!empty($matches[1])){
 foreach($matches[1] as $k=>$v){
 list($seconds, $millis) = explode('.', $v / 1000); 
 $lrc_time = date('i:s',1388534400 + $seconds).'.'.substr($millis,0,2);
 $lrc_cont = preg_replace("/<d{1,8},d{1,8},d{1,8}>/",'',$matches[3][$k]);
 $lrc_text .= "[{$lrc_time}]{$lrc_cont}{$glue}";
 }
 }
 return $lrc_text;
}

简单示例如下:

$content = file_get_contents('1.krc');
echo krc2lrc($content,'<br/>');
Statement:
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