2014-11-12 15 views
7

원격 mp3 파일의 ID3 태그 및 메타 데이터를 추출해야합니다. 나는 MP3 파일에 대한 URL 링크에 대해이 작업을 달성하는 데 필요한python을 사용하여 부분 다운로드 한 MP3 URL의 ID3 태그 추출

from mutagen.mp3 import MP3 
import urllib2 

audio = MP3("Whistle.mp3") 

songtitle = audio["TIT2"] 
artist = audio["TPE1"] 

print "Title: " + str(songtitle) 
print "Artist: "+str(artist) 

:

나는 로컬 파일의 ID3 태그를 얻을 수있는 몇 줄을 썼다. urllib2를 사용하여 파일을 부분적으로 다운로드하려고했습니다.

import urllib2 
from mutagen.mp3 import MP3 

req = urllib2.Request('http://www.1songday.com/wp-content/uploads/2013/08/Lorde-Royals.mp3') 
req.headers['Range'] = 'bytes=%s-%s' % (0, 100) 
response = urllib2.urlopen(req) 
headers = response.info() 
print headers.type 
print headers.maintype 

data = response.read() 
print len(data) 

어떻게 파일을 다운로드하지 않고도 MP3 url의 ID3 태그를 추출 할 수 있습니까?

+0

MP3 파일의 처음 100 바이트를 가져온 것입니다. 어디서 붙어 있니? – scav

+2

아, id3 태그가 파일의 마지막 128 바이트에있는 것처럼 보입니다. 따라서 파일의 크기를 알지 못하면 Range 헤더를 만들 수 없습니다. 아마도 HEAD 요청은 파일의 길이를 먼저 얻을 수 있습니다 ... – scav

답변

0

예에서 ID3 태그는 가져 오지 않으므로 추출 할 수 없습니다.

ID3 사양을 읽은 후 약간 놀았습니다. 시작하기에 좋은 방법이 있습니다.

#Search for ID3v1 tags 
import string 
tagIndex = string.find(data,'TAG') 
if (tagIndex>0): 
    if data[tagIndex+3]=='+': 
    print "Found extended ID3v1 tag!" 
    title = data[tagIndex+3:tagIndex+63] 
    print title 
    else: 
    print "Found ID3v1 tags" 
    title = data[tagIndex+3:tagIndex+33] 
    print title 
    #So on. 
else: 
    #Look for ID3v2 tags 
    if 'TCOM' in data: 
    composerIndex = string.find(data,'TCOM') 
    #and so on. See wikipedia for a full list of frame specifications 
0

태그 (음성을 포함)를 MP3 프레임 앞 통상 되는 ID3 메타 데이터에 저장되는 ID3하지만, MP3 표준 "follow the mp3 frames"에도이를 수있다. 의 ID3v2 헤더를 추출하고 전체 ID3V2를 검색 할 ID3V2 헤더

  • 의 크기를 계산,

    1. 다운로드 mp3 파일의 처음 10 바이트를 :

      바이트의 최소 수를 다운로드하려면 다음을 수행해야 태그
    2. 여기

  • 스크립트 년대 ID3 태그를 추출하는 파이썬 라이브러리를 사용하는 MP3의 size 바이트를 다운로드 (파이썬 2 또는 3) 앨범 아트 WI를 추출하는

    try: 
        import urllib2 as request # python 2 
    except ImportError: 
        from urllib import request # python 3 
        from functools import reduce 
    import sys 
    from io import BytesIO 
    from mutagen.mp3 import MP3 
    
    url = sys.argv[1] 
    
    def get_n_bytes(url, size): 
        req = request.Request(url) 
        req.headers['Range'] = 'bytes=%s-%s' % (0, size-1) 
        response = request.urlopen(req) 
        return response.read() 
    
    data = get_n_bytes(url, 10) 
    if data[0:3] != 'ID3': 
        raise Exception('ID3 not in front of mp3 file') 
    
    size_encoded = bytearray(data[-4:]) 
    size = reduce(lambda a,b: a*128+b, size_encoded, 0) 
    
    header = BytesIO() 
    # mutagen needs one full frame in order to function. Add max frame size 
    data = get_n_bytes(url, size+2881) 
    header.write(data) 
    header.seek(0) 
    f = MP3(header) 
    
    if f.tags and 'APIC:' in f.tags.keys(): 
        artwork = f.tags['APIC:'].data 
        with open('image.jpg', 'wb') as img: 
         img.write(artwork) 
    

    몇 가지 발언 : 다운로드 크기의 최소한의 토륨

    는 ID3 파일의 앞에있는 것을 확인
    • 과는 ID3 태그의 크기 ID3v2는
    • 의 그 유감스럽게도 mutagen은 id3 태그를 파싱하려면 하나의 풀 mp3 오디오 프레임이 필요합니다. 그러므로 당신은 앨범 아트가 먼저 ID3로 이미지 형식을 확인해야 JPG된다고 가정 맹목적으로
    • 대신 (최대 2,881 긴 this comment에 따라 바이트에있다) 한 MP3 프레임을 다운로드해야 allows many different image types
    • 테스트 인터넷에서 약 10 임의의 mp3s, 예 : 이 하나 : python url.py http://www.fuelfriendsblog.com/listenup/01%20America.mp3