当前位置: 移动技术网 > IT编程>开发语言>Java > java实现酷狗音乐临时缓存文件转换为MP3文件的方法

java实现酷狗音乐临时缓存文件转换为MP3文件的方法

2019年07月22日  | 移动技术网IT编程  | 我要评论

本文实例讲述了java实现酷狗音乐临时缓存文件转换为mp3文件的方法。分享给大家供大家参考,具体如下:

酷狗临时缓存文件,其实已经是吧mp3文件下载好了,只是名字看上去好像是通过md5算法重命名的。

酷狗在缓存文件的时候会同时缓存歌词。这个程序就是根据md5管理对应的歌词文件和缓存文件,然后把缓存文件改成 歌曲名+.mp3格式。

原谅我取这么长也不知道对不对的类名。

package com.zhou.run;
import java.io.file;
import java.util.hashmap;
import java.util.map;
public class kugoutempfiletomp3andmodifynametotruename {
  public static string kgtemp = ".kgtemp";
  public static string krc = "krc";
  public void change(string temppath, string krcpath) {
    file temp = new file(temppath);
    file krc = new file(krcpath);
    if (temp.exists() && temp.getname().endswith(kgtemp)) {
      string filename = temp.getname();
      string filemd5 = filename
          .substring(0, filename.lastindexof(kgtemp));
      if (!krc.exists())
        return;
      string krcname = krc.getname();
      string krcmd5 = krcname.substring(krcname.lastindexof("-") + 1,
          krcname.lastindexof(krc) - 1);
      string mp3name = krcname.substring(0, krcname.lastindexof("-"));
      if (krcmd5.equals(filemd5)) {
        string path = temp.getpath().substring(0,
            temp.getpath().lastindexof("\\"));
        file mp3file = new file(path + "\\" + mp3name + ".mp3");
        temp.renameto(mp3file);
      }
      system.out.println(filename + " " + filemd5);
      system.out.println(krcname + " " + mp3name + " " + krcmd5);
    }
  }
  public void changebydir(string temppath,string krcpath){
    map<string,file> temps = filemd5map(temppath);
    map<string,string> mp3names = krcnamemd5map(krcpath);
    for(string key :temps.keyset()){
      file f = temps.get(key);
      if(f.exists()){
        string path = f.getpath().substring(0,
            f.getpath().lastindexof("\\"));
        string mp3name = mp3names.get(key);
        file mp3file = new file(path + "\\" + mp3name + ".mp3");
        if(f.renameto(mp3file)){
          system.out.println(f.getname()+" to "+mp3file.getname());
          system.err.print("  success");
        }
      }
    }
  }
  public map<string, file> filemd5map(string path) {
    file dirfile = new file(path);
    map<string, file> map = null;
    if (dirfile.isdirectory()) {
      map = new hashmap<string, file>();
      for (file f : dirfile.listfiles()) {
        if (f.exists()&&f.isfile()&& f.getname().endswith(kgtemp)) {
          string filename = f.getname();
          string filemd5 = filename.substring(0,
              filename.lastindexof(kgtemp));
          map.put(filemd5, f);
        }
      }
    }
    return map;
  }
  public map<string,string> krcnamemd5map(string path){
    file dirfile = new file(path);
    map<string, string> map = null;
    if (dirfile.isdirectory()) {
      map = new hashmap<string, string>();
      for (file f : dirfile.listfiles()) {
        if (f.exists()&&f.isfile()&& f.getname().endswith(krc)) {
          string krcname = f.getname();
          if(!krcname.contains("-"))continue;
          string krcmd5 = krcname.substring(krcname.lastindexof("-") + 1,
              krcname.lastindexof(krc) - 1);
          string mp3name = krcname.substring(0, krcname.lastindexof("-"));
          map.put(krcmd5, mp3name);
        }
      }
    }
    return map;
  }
}
public static void main(string[] args) {
    kugoutempfiletomp3andmodifynametotruename ktf = new kugoutempfiletomp3andmodifynametotruename();
    /*string temppath = "d:/kugou/mp3/2fad259e357078e89404be12e1fd7ae3.kgtemp";
    string krcpath ="d:/kugou/lyric/周杰伦、袁咏琳 - 怎么了-2fad259e357078e89404be12e1fd7ae3.krc";
    ktf.change(temppath,krcpath);*/
    string tempdir ="d:/kugou/mp3";
    string krcdir="d:/kugou/lyric";
    ktf.changebydir(tempdir, krcdir);
}

change(string,string) 方法只是用来测试用的。调一下字符串之类的

主要使用changebydir方法,参数是临时文件的文件夹和歌词文件的文件夹

更多关于java算法相关内容感兴趣的读者可查看本站专题:《java数据结构与算法教程》、《java操作dom节点技巧总结》、《java文件与目录操作技巧汇总》和《java缓存操作技巧汇总

希望本文所述对大家java程序设计有所帮助。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网