酷狗krc歌词转换为lrc格式

来源:互联网 发布:java websocket客户端 编辑:程序博客网 时间:2024/05/16 11:39
<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;}



0 0
原创粉丝点击