Java 如何读取 MP3 文件标签

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1645803/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 18:37:56  来源:igfitidea点击:

How to read MP3 file tags

javamp3metadataid3

提问by vijay.shad

I want to have a program that reads metadata from an MP3 file. My program should also able to edit these metadata. What can I do?

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

I got to search out for some open source code. But they have code; but not simplified idea for my job they are going to do.

我必须寻找一些开源代码。但他们有代码;但他们将要做的工作并没有简化。

When I read further I found the metadata is stored in the MP3 file itself. But I am yet not able to make a full idea of my baby program.

当我进一步阅读时,我发现元数据存储在 MP3 文件本身中。但我还不能完全了解我的婴儿计划。

Any help will be appreciated; with a program or very idea (like an algorithm). :)

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

采纳答案by Jaskirat

The last 128 bytes of a mp3 file contains meta data about the mp3 file., You can write a program to read the last 128 bytes...

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

UPDATE:

更新:

ID3v1 Implementation

ID3v1 实现

The Information is stored in the last 128 bytes of an MP3. The Tag has got the following fields, and the offsets given here, are from 0-127.

信息存储在 MP3 的最后 128 个字节中。Tag 有以下字段,这里给出的偏移量是 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- This is just an ugly way of getting metadata and it might not actually be there because the world has moved to id3v2. id3v1 is actually obsolete. Id3v2 is more complex than this, so ideally you should use existing libraries to read id3v2 data from mp3s . Just putting this out there.

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

回答by MADHAIYAN M

You can use apache tikaJava API for meta-data parsing from MP3 such as title, album, genre, duraion, composer, artist and etc.. required jars are tika-parsers-1.4, tika-core-1.4.

您可以使用apache tikaJava API 从 MP3 解析元数据,例如标题、专辑、流派、持续时间、作曲家、艺术家等。所需的 jars 是tika-parsers-1.4、tika-core-1.4。

Sample Program:

示例程序:

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();
        }
        }
    }

回答by user1048839

For J2ME(which is what I was struggling with), here's the code that worked for me..

对于 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) {
  }

}