Domanda

Sto cercando un modo per leggere i tag ID3 da un file MP3 su un server remoto senza in realtà il download. Ho visto librerie come JAudioTagger e Entagged, ma entrambi sembrano richiedere un oggetto file e non un URL o InputStream, che so come ottenere con un file remoto. C'è un'altra libreria che può fare questo? O c'è un modo per ottenere l'oggetto giusto per interagire con queste classi utilizzando un URL?

È stato utile?

Soluzione

In questa pagina viene descritto come ottenere l'ID3 V. 1 i tag di un file MP3. http://willcode4beer.com/parsing.jsp?set=mp3ID3

Offre una ..

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

.. metodo che è quello che si vuole per un URL remoto. L'idea di base sarebbe quella di ottenere un URLConnection e di query per la lunghezza dei dati nel formato MP3, quindi sottrarre 128 da quel numero e l'uso che come argomento di partenza (in caso contrario sarà molto lenta) .

Altri suggerimenti

ID3 tag si trovano negli ultimi 128 (355 se si utilizza tag estesa) byte del file, in modo che si sta per almeno necessario scaricare parte del file. Come HTTP supporti vanno accesso ai file specifico, dovrebbe essere teoricamente possibile per fare questo (anche se non so di eventuali librerie che lo farebbe per voi).

In sostanza ciò che avrebbe bisogno di accadere è quello di fare una richiesta HEAD per ottenere la lunghezza del file in byte, quindi eseguire un GET sul file con il length-355 intervallo fino alla fine del file. Ciò restituirebbe i metadati necessari. Questo dà una buona idea di ciò che un ranged richiesta assomiglia.

Ci dispiace però che io non conosco le librerie che avrebbe fatto automaticamente, ma non è un compito particolarmente difficile da configurare il getter. Da lì è possibile scrivere i metadati a un file temporaneo e lo hanno analizzato dal parser ID3.

    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();
    }   
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top