酷狗krc歌詞轉換爲lrc格式

<pre name="code" class="java">// 傳遞一個krc格式歌詞的路徑,返回讀取並返回解密的字符串
<pre name="code" class="java">public String krc2lrc(String krc){
	try {
		// 酷狗音樂歌詞文件
		File krcfile = new File(krc);
		byte[] zip_byte = new byte[(int) krcfile.length()];
		FileInputStream fileinstrm = new FileInputStream(krcfile);
		byte[] top = new byte[4];
		fileinstrm.read(top);
		fileinstrm.read(zip_byte);
		int j = zip_byte.length;
		for (int k = 0; k < j; k++) {
			int l = k % 16;
			int tmp67_65 = k;
			byte[] tmp67_64 = zip_byte;
		<span style="white-space:pre">	</span>tmp67_64[tmp67_65] = (byte) (tmp67_64[tmp67_65] ^ miarry[l]);
		}
		String krc_text = new String(decompress(zip_byte), "utf-8");
		krc_text = krc_text.replaceAll("<.*?>", "");//刪除krc多餘的時間(此時間是單字顯示的時間)
		krc_text = extractMessageByRegular(krc_text);
<span style="font-family: Arial, Helvetica, sans-serif;"><span>				</span>System.out.println(krc_text);
</span><span style="font-family: Arial, Helvetica, sans-serif;"><span>			</span>} catch (Exception e) {
</span><span style="font-family: Arial, Helvetica, sans-serif;"><span>		</span>}
}</span>


<pre name="code" class="java">/**
 * 使用正則表達式提取中括號中的內容
 * @param msg
 * @return 
 */
public static String extractMessageByRegular(String msg){
	Pattern p = Pattern.compile("(\\[[^\\]]*\\])");
	Matcher m = p.matcher(msg);
	while(m.find()){
		String str = m.group().substring(1, m.group().length()-1);
		try{
			String[] split = str.split(",");
			String spl = "[" + ms2Date(Long.parseLong(split[0])) + "]";//只截取前面的時間(後面的時間爲單字時間,捨棄)
			msg = msg.replace(m.group(), spl);
		}catch (Exception e) {
<pre name="code" class="java">//由於標題之類的格式也是以[開頭以]結尾,所以此時轉換爲long會轉換異常
		}
	}
	return msg;
}



public static String ms2Date(long ms){//格式化時間格式
	DateFormat formatter = new SimpleDateFormat("mm:ss");
	Calendar calendar = Calendar.getInstance();
	calendar.setTimeInMillis(ms);
	return formatter.format(calendar.getTime());
}
// 通過字節讀取krc文件
public static byte[] decompress(byte[] data) {
	byte[] output = new byte[0];
	Inflater decompresser = new Inflater();
	decompresser.reset();
	decompresser.setInput(data);
	ByteArrayOutputStream o = new ByteArrayOutputStream(data.length);
	try {
		byte[] buf = new byte[1024];
		while (!decompresser.finished()) {
			int i = decompresser.inflate(buf);
			o.write(buf, 0, i);
		}
		output = o.toByteArray();
	} catch (Exception e) {
		output = data;
		e.printStackTrace();
	} finally {
		try {
			o.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	decompresser.end();
	return output;
}



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章