将酷狗krc歌词转换为lrc格式

传递一个krc格式歌词的路径,返回读取并返回解密的字符串

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;  
            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);  
           
        System.out.println(krc_text);  
    } catch (Exception e) {  
    }  
}

使用正则表达式提取中括号中的内容

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) {  
//由于标题之类的格式也是以[开头以]结尾,所以此时转换为long会转换异常  
        }  
    }  
    return msg;  
}  

通过字节读取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;  
}  

格式化时间格式

public static String ms2Date(long ms){
    DateFormat formatter = new SimpleDateFormat("mm:ss");  
    Calendar calendar = Calendar.getInstance();  
    calendar.setTimeInMillis(ms);  
    return formatter.format(calendar.getTime());  
}  

猜你喜欢

转载自my.oschina.net/u/3269106/blog/837888
今日推荐