使用 Java 读取远程 MP3 文件的 ID3 标签

发布于 2024-09-29 09:42:38 字数 178 浏览 5 评论 0原文

我正在寻找一种从远程服务器上的 MP3 文件读取 ID3 标签的方法,而无需实际下载该文件。我见过像 JAudioTagger 和 Entagged 这样的库,但两者似乎都需要文件对象而不是 URL 或 InputStream,我知道如何通过远程文件获取它们。还有其他库可以做到这一点吗?或者有没有办法让正确的对象使用 URL 与这些类交互?

I am looking for a way to read the ID3 tags from an MP3 file on a remote server without actually downloading the file. I have seen libraries like JAudioTagger and Entagged, but both seem to require a file object and not a URL or InputStream, which I know how to get with a remote file. Is there another library that can do this? Or is there a way to get the correct object to interact with these classes using a URL?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

暮倦 2024-10-06 09:42:38

ID3 标签位于文件的最后 128 个字节(如果使用扩展标签则为 355 个字节),因此您至少需要下载文件的一部分。由于 HTTP 支持范围特定的文件访问,理论上应该可以做到这一点(尽管我不知道有任何库可以为您做到这一点)。

本质上,需要做的是执行 HEAD 请求来获取文件的长度(以字节为单位),然后对文件执行 GET,范围 length-355 到文件末尾。这将返回必要的元数据。 很好地了解了范围请求的样子。

抱歉,虽然我不知道有任何库可以自动执行此操作,但设置 getter 并不是一项特别困难的任务。从那里可以将元数据写入临时文件并由 ID3 解析器对其进行解析。

ID3 Tags are located in the last 128 ( 355 if using extended tag ) bytes of the file, so you are going to at least have to download part of the file. As HTTP supports range specific file access, it should be theoretically possible to do this (though I do not know of any libraries that would do it for you).

Essentially what would need to happen is to do a HEAD request to get the length of the file in bytes, then perform a GET on the file with the Range length-355 to the end of the file. This would return the necessary metadata. This gives a good idea of what a ranged request looks like.

Sorry though that I do not know of any libraries that would do this automatically, but it isn't a particularly difficult task to set up the getter. From there it is possible to write the metadata to a temp file and have it parsed by your ID3 parser.

奶茶白久 2024-10-06 09:42:38
    String tmp="";
    int bytes_read;

    try{
        //RandomAccessFile file = new RandomAccessFile("/mnt/sdcard/Music/6 am (reggaeton).mp3", "r");
        RandomAccessFile file = new RandomAccessFile("/mnt/sdcard/Music/Jorge Vercilo - Final feliz.mp3", "r");

        long fsize=file.length();
        System.out.println("Abriu arquivo");
        byte[] buf = new byte[1024];
        bytes_read = file.read(buf, 0, 10);
        System.out.println("Leu "+String.valueOf(bytes_read));
        if(bytes_read==10){
            if(buf[0] == 'I' &&buf[1] == 'D' && buf[2] == '3') {
                System.out.println("É ID3");
                int version = (buf[4]<<8) + (buf[3] & 0xff);
                System.out.println("Versão: "+String.valueOf(version));
                if(buf[5] == 0x00) System.out.println("Clear flags");
                long size = ((buf[9] & 0xFF) <<  0) |
                            ((buf[8] & 0xFF) <<  8) |
                            ((buf[7] & 0xFF) << 16) |
                            ((buf[6] & 0xFF) << 24);
                System.out.println("Size: "+String.valueOf(size));

                long p = 10;
                long frame_size;
                String encoding="";
                while(p<fsize){
                    file.seek(p);
                    bytes_read = file.read(buf, 0, 10);
                    if(bytes_read==10){
                        frame_size = ((buf[7] & 0xFF) <<  0) |
                            ((buf[6] & 0xFF) <<  8) |
                            ((buf[5] & 0xFF) << 16) |
                            ((buf[4] & 0xFF) << 24);
                        System.out.println("Frame size: "+String.valueOf(frame_size));
                        tmp = new String(buf,0,4);
                        System.out.println("Frame type: "+tmp);

                        if(buf[0] == 'T' && buf[1] == 'P' && buf[2] == 'E' && buf[3] == '1') {// artist
                            p+=10;
                            file.seek(p);
                            if(file.read(buf, 0, (int)frame_size)==frame_size){
                                if(buf[0]==0x01) encoding="UTF-16";
                                else if(buf[0]==0x02) encoding="UTF-16BE";
                                else if(buf[0]==0x03) encoding="UTF-8";
                                if(buf[0]==0x00) tmp = new String(buf,1,(int)frame_size-1);
                                else tmp = new String(buf,1,(int)frame_size-1,encoding);
                                System.out.println("Artist: "+tmp);
                            }
                            p+=frame_size;
                        }
                        else if(buf[0] == 'T' && buf[1] == 'I' && buf[2] == 'T' && buf[3] == '2') {// title
                            p+=10;
                            file.seek(p);
                            if(file.read(buf, 0, (int)frame_size)==frame_size){
                                if(buf[0]==0x01) encoding="UTF-16";
                                else if(buf[0]==0x02) encoding="UTF-16BE";
                                else if(buf[0]==0x03) encoding="UTF-8";
                                if(buf[0]==0x00) tmp = new String(buf,1,(int)frame_size-1);
                                else tmp = new String(buf,1,(int)frame_size-1,encoding);
                                System.out.println("title: "+tmp);
                            }
                            p+=frame_size;
                        }
                        else if(buf[0] == 0x00 && buf[1] == 0x00 && buf[2] == 0x00 && buf[3] == 0x00) {// END OF HEADER
                            break;
                        }
                        else p+= frame_size+10;
                    }
                    //checar se ja pegou o title e o artist
                }
            }
        }

        if(file!=null)  file.close();
    }catch(Exception e){
        System.out.println("ERROOOOOOOOO!");
        e.printStackTrace();
    }   
    String tmp="";
    int bytes_read;

    try{
        //RandomAccessFile file = new RandomAccessFile("/mnt/sdcard/Music/6 am (reggaeton).mp3", "r");
        RandomAccessFile file = new RandomAccessFile("/mnt/sdcard/Music/Jorge Vercilo - Final feliz.mp3", "r");

        long fsize=file.length();
        System.out.println("Abriu arquivo");
        byte[] buf = new byte[1024];
        bytes_read = file.read(buf, 0, 10);
        System.out.println("Leu "+String.valueOf(bytes_read));
        if(bytes_read==10){
            if(buf[0] == 'I' &&buf[1] == 'D' && buf[2] == '3') {
                System.out.println("É ID3");
                int version = (buf[4]<<8) + (buf[3] & 0xff);
                System.out.println("Versão: "+String.valueOf(version));
                if(buf[5] == 0x00) System.out.println("Clear flags");
                long size = ((buf[9] & 0xFF) <<  0) |
                            ((buf[8] & 0xFF) <<  8) |
                            ((buf[7] & 0xFF) << 16) |
                            ((buf[6] & 0xFF) << 24);
                System.out.println("Size: "+String.valueOf(size));

                long p = 10;
                long frame_size;
                String encoding="";
                while(p<fsize){
                    file.seek(p);
                    bytes_read = file.read(buf, 0, 10);
                    if(bytes_read==10){
                        frame_size = ((buf[7] & 0xFF) <<  0) |
                            ((buf[6] & 0xFF) <<  8) |
                            ((buf[5] & 0xFF) << 16) |
                            ((buf[4] & 0xFF) << 24);
                        System.out.println("Frame size: "+String.valueOf(frame_size));
                        tmp = new String(buf,0,4);
                        System.out.println("Frame type: "+tmp);

                        if(buf[0] == 'T' && buf[1] == 'P' && buf[2] == 'E' && buf[3] == '1') {// artist
                            p+=10;
                            file.seek(p);
                            if(file.read(buf, 0, (int)frame_size)==frame_size){
                                if(buf[0]==0x01) encoding="UTF-16";
                                else if(buf[0]==0x02) encoding="UTF-16BE";
                                else if(buf[0]==0x03) encoding="UTF-8";
                                if(buf[0]==0x00) tmp = new String(buf,1,(int)frame_size-1);
                                else tmp = new String(buf,1,(int)frame_size-1,encoding);
                                System.out.println("Artist: "+tmp);
                            }
                            p+=frame_size;
                        }
                        else if(buf[0] == 'T' && buf[1] == 'I' && buf[2] == 'T' && buf[3] == '2') {// title
                            p+=10;
                            file.seek(p);
                            if(file.read(buf, 0, (int)frame_size)==frame_size){
                                if(buf[0]==0x01) encoding="UTF-16";
                                else if(buf[0]==0x02) encoding="UTF-16BE";
                                else if(buf[0]==0x03) encoding="UTF-8";
                                if(buf[0]==0x00) tmp = new String(buf,1,(int)frame_size-1);
                                else tmp = new String(buf,1,(int)frame_size-1,encoding);
                                System.out.println("title: "+tmp);
                            }
                            p+=frame_size;
                        }
                        else if(buf[0] == 0x00 && buf[1] == 0x00 && buf[2] == 0x00 && buf[3] == 0x00) {// END OF HEADER
                            break;
                        }
                        else p+= frame_size+10;
                    }
                    //checar se ja pegou o title e o artist
                }
            }
        }

        if(file!=null)  file.close();
    }catch(Exception e){
        System.out.println("ERROOOOOOOOO!");
        e.printStackTrace();
    }   
爱冒险 2024-10-06 09:42:38

本页介绍如何获取 ID3 V。 1 MP3 文件的 标签。 http://willcode4beer.com/parsing.jsp?set=mp3ID3

它提供了一个 . ..

public Tag readTag(InputStream in, long start) throws ..

这就是您想要的远程 URL 的方法。基本思想是获取 URLConnection &查询 MP3 中数据的长度,然后从该数字中减去 128使用它作为开始参数(否则它会非常慢)。

This page describes how to get the ID3 V. 1 tags of an MP3 file. http://willcode4beer.com/parsing.jsp?set=mp3ID3

It offers a ..

public Tag readTag(InputStream in, long start) throws ..

..method that is what you will want for a remote URL. The basic idea would be to get an URLConnection & query it for the length of the data in the MP3, then subtract 128 from that number & use that as the start argument (otherwise it will be very slow).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文