Krc lyrics of cool dog parsed and converted to lrc lyrics php version

Source: Internet
Author: User
Tags chr ord strlen

Recently, when collecting the lyrics of Codoy music, we found that the lyrics of Codoy music are all "garbled" and the lyrics we usually see are all in lrc format, this kind of krc format for Codoy is obviously specially handled. It is usually different to listen to music with Codoy, the only difference is that the lyrics can be highlighted by words.

So I searched and issued an existing semi-finished product decryption code for java, and tested whether it was "garbled! Therefore, only the most primitive method is used to decompile the Flash player of Codoon and find the following code of the codes decrypted by krc:

The code is as follows: Copy code

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;
}

Based on this code, you can quickly convert it into a php parsing code:

The code is as follows: Copy code

$ 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 );

The above $ krc_content is the krc lyrics compressed and encrypted text content. The parsed krc plaintext text lyrics $ krc_text can be easily converted into lrc lyrics, as shown below:

The code is as follows: Copy code

/**
* Parse and convert krc lyrics to lrc lyrics
* $ Name: krc2lrc 2014-4-18 qaulau <qaulau@hotmail.com> $
 *
* @ Param string $ krc_content-encrypted krc lyrics
* @ Param string $ the lrc lyric connection character after glue processing. The default value is line break 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 = '';
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 );
$ Lrc_cont = preg_replace ("/<d {}, d {}, d {}>/", '', $ matches [3] [$ k]);
$ Lrc_text. = "[{$ lrc_time}] {$ lrc_cont} {$ glue }";
  }
 }
Return $ lrc_text;
}

Simple example:

The code is as follows: Copy code

$ Content = file_get_contents ('1. krc ');
Echo krc2lrc ($ content, '<br/> ');

Share it with others to facilitate future query of similar needs!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.