krc转lrc 酷狗加密歌词转换

import java.io.ByteArrayOutputStream;
import java.io.FileOutputStream;
import java.io.RandomAccessFile;
import java.text.SimpleDateFormat;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.zip.Inflater;

public class Krc2Lrc {
	private static final char key[] = { '@', 'G', 'a', 'w', '^', '2', 't', 'G', 'Q', '6', '1', '-', '\316', '\322','n', 'i' };

	public static void main(String[] args) {
		try {
			convert("c:/a.krc");
		}
		catch (Exception e) {
			System.err.println("failure!");
			e.printStackTrace();
			return;
		}
		System.out.println("success");
	}

	/*
	 * 参数:文件名 函数作用:解密转换
	 */
	public static void convert(String fileName) throws Exception {
		RandomAccessFile raf = new RandomAccessFile(fileName, "r");
		byte[] content = new byte[(int) (raf.length() - 4)];
		raf.skipBytes(4);
		raf.read(content);
		raf.close();

		for (int i = 0, length = content.length; i < length; i++) {
			int j = i % 16; // 循环异或解密
			content[i] ^= key[j];
		}

		String lrc = null;

		lrc = new String(decompress(content), "utf-8"); // 解压为 utf8

		String final_lrc = lrc.replaceAll("<([^>]*)>", "").replaceAll(",([^]]*)]", "] ");
		/* 处理时间标签 */
		Pattern p = Pattern.compile("\\[\\d+?\\]");
		Matcher m = p.matcher(final_lrc);
		while (m.find()) {
			final_lrc = m.replaceFirst(toTime(m.group()));
			m = p.matcher(final_lrc);
		}
		String lrcFileName = fileName.replaceAll(".krc", ".lrc").replaceAll("\\s-\\s\\w+.lrc", ".lrc");
		FileOutputStream fos = new FileOutputStream(lrcFileName);
		byte[] lrcbyte = final_lrc.getBytes();
		fos.write(lrcbyte);
		fos.close();
		System.out.println("文件保存为:"+lrcFileName);
	}

	private static String toTime(String num) {
		SimpleDateFormat sdf = new SimpleDateFormat("mm:ss");
		long time = Long.parseLong(num.substring(1, num.length() - 1));
		return "[" + sdf.format(time) + "." + ((time % 1000) / 10) + "]";
	}

	/*
	 * 解压
	 */
	private static byte[] decompress(byte[] data) throws Exception {

		byte[] output = new byte[0];
		Inflater decompresser = new Inflater();
		decompresser.reset();
		decompresser.setInput(data);
		ByteArrayOutputStream o = new ByteArrayOutputStream(data.length);
		byte[] buf = new byte[1024];
		while (!decompresser.finished()) {
			int i = decompresser.inflate(buf);
			o.write(buf, 0, i);
		}
		output = o.toByteArray();
		o.close();
		decompresser.end();
		return output;
	}
}

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值