如何阅读MP3文件标签

时间:2009-10-29 18:51:09

标签: java mp3 metadata id3

我想要一个从MP3文件中读取元数据的程序。我的程序还应该能够编辑这些元数据。我能做什么?

我要搜索一些开源代码。但他们有代码;但是我的工作不会简化他们的想法。

当我进一步阅读时,我发现元数据存储在MP3文件中。但我还是无法完全了解我的宝宝计划。

任何帮助将不胜感激;有一个程序或非常想法(如算法)。 :)

3 个答案:

答案 0 :(得分:26)

mp3文件的最后128个字节包含有关mp3文件的元数据。你可以编写一个程序来读取最后128个字节......

更新:

ID3v1实施

信息存储在MP3的最后128个字节中。标签    有以下字段,这里给出的偏移来自    0-127。

 Field      Length    Offsets
 Tag        3           0-2
 Songname   30          3-32
 Artist     30         33-62
 Album      30         63-92
 Year       4          93-96
 Comment    30         97-126
 Genre      1           127

WARINING-这只是获取元数据的一种丑陋方式,它可能实际上并不存在,因为世界已经转移到了id3v2。 id3v1实际上已经过时了。 Id3v2比这更复杂,所以理想情况下你应该使用现有的库来读取mp3中的id3v2数据。把它放在那里。

答案 1 :(得分:13)

您可以使用 apache tika Java API对MP3进行元数据解析,例如标题,专辑,流派,duraion,作曲家,艺术家等 ..所需的jar是 tika-parsers-1.4,tika-core-1.4。

示例程序:

package com.parse.mp3;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;

import org.apache.tika.exception.TikaException;
import org.apache.tika.metadata.Metadata;
import org.apache.tika.parser.ParseContext;
import org.apache.tika.parser.Parser;
import org.apache.tika.parser.mp3.Mp3Parser;
import org.xml.sax.ContentHandler;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

public class AudioParser {

    /**
     * @param args
     */
    public static void main(String[] args) {
        String fileLocation = "G:/asas/album/song.mp3";

        try {

        InputStream input = new FileInputStream(new File(fileLocation));
        ContentHandler handler = new DefaultHandler();
        Metadata metadata = new Metadata();
        Parser parser = new Mp3Parser();
        ParseContext parseCtx = new ParseContext();
        parser.parse(input, handler, metadata, parseCtx);
        input.close();

        // List all metadata
        String[] metadataNames = metadata.names();

        for(String name : metadataNames){
        System.out.println(name + ": " + metadata.get(name));
        }

        // Retrieve the necessary info from metadata
        // Names - title, xmpDM:artist etc. - mentioned below may differ based
        System.out.println("----------------------------------------------");
        System.out.println("Title: " + metadata.get("title"));
        System.out.println("Artists: " + metadata.get("xmpDM:artist"));
        System.out.println("Composer : "+metadata.get("xmpDM:composer"));
        System.out.println("Genre : "+metadata.get("xmpDM:genre"));
        System.out.println("Album : "+metadata.get("xmpDM:album"));

        } catch (FileNotFoundException e) {
        e.printStackTrace();
        } catch (IOException e) {
        e.printStackTrace();
        } catch (SAXException e) {
        e.printStackTrace();
        } catch (TikaException e) {
        e.printStackTrace();
        }
        }
    }

答案 2 :(得分:2)

对于J2ME(我正在努力解决),这里的代码对我有用..

import java.io.InputStream;
import javax.microedition.io.Connector;
import javax.microedition.io.file.FileConnection;
import javax.microedition.lcdui.*;
import javax.microedition.media.Manager;
import javax.microedition.media.Player;
import javax.microedition.media.control.MetaDataControl;
import javax.microedition.midlet.MIDlet;

public class MetaDataControlMIDlet extends MIDlet implements CommandListener {
  private Display display = null;
  private List list = new List("Message", List.IMPLICIT);
  private Command exitCommand = new Command("Exit", Command.EXIT, 1);
  private Alert alert = new Alert("Message");
  private Player player = null;  

  public MetaDataControlMIDlet() {    
    display = Display.getDisplay(this);
    alert.addCommand(exitCommand);
    alert.setCommandListener(this);
    list.addCommand(exitCommand);
    list.setCommandListener(this);
    //display.setCurrent(list);

  }

  public void startApp() {
      try {
      FileConnection connection = (FileConnection) Connector.open("file:///e:/breathe.mp3");
      InputStream is = null;
      is = connection.openInputStream();
      player = Manager.createPlayer(is, "audio/mp3");
      player.prefetch();
      player.realize();
    } catch (Exception e) {
      alert.setString(e.getMessage());
      display.setCurrent(alert);
      e.printStackTrace();
    }

    if (player != null) {
      MetaDataControl mControl = (MetaDataControl) player.getControl("javax.microedition.media.control.MetaDataControl");
      if (mControl == null) {
        alert.setString("No Meta Information");
        display.setCurrent(alert);
      } else {
        String[] keys = mControl.getKeys();
        for (int i = 0; i < keys.length; i++) {
          list.append(keys[i] + " -- " + mControl.getKeyValue(keys[i]), null);
        }
        display.setCurrent(list);
      }
    }
  }

  public void commandAction(Command cmd, Displayable disp) {
    if (cmd == exitCommand) {
      notifyDestroyed();
    }
  }

  public void pauseApp() {
  }

  public void destroyApp(boolean unconditional) {
  }

}