-1

I have a simple Python program that gets artwork from MP3 files. But when I try to open the resulting file in my web page, it's not loaded.

This is my code:

#!"C:/Python32/python.exe"

import binascii
print('Content-type:image/jpeg\n\n\n')


mp3 = r'music.mp3'
mp3_File = open(mp3, "rb")
mp3_Data = mp3_File.read()
mp3_File.close()


hexs = str(binascii.hexlify(mp3_Data))

hexol = []

for ix in range(2, len(hexs)-1, 2):
    hex = hexs[ix]+hex_str[ix+1]
    hexol.append('|'+hex)

hex_str = "".join(hex_list)

img = hexs.split('|41|50|49|43')

p = img[1]
p = p.replace('|','')
p = p[:34*1500]
hexl = []
for ix in range(2, len(p)-1, 2):
    hex = p[ix]+p[ix+1]
    hexl.append(hex)

 art = open('C:\\hi.jpg','wb')
 art.write(binascii.unhexlify(''.join(hexl).encode('utf-8')))
 art.close()
 data = open('C:\\hi.jpg', 'rb').read()
 print(data)
alex4o
  • 33
  • 1
  • 5
  • 1
    What are you doing to "open" this in your web page? Also, do you really have the indentation in your real code as shown? – Wooble Jul 05 '12 at 12:13
  • 1
    Fixed your English. Please use a spell checker next time. – cha0site Jul 05 '12 at 12:15
  • The code needs proper indentation, e.g., the bodies of the for-loops are not clear due to indentation. Once fixed will be easier to help. – Levon Jul 05 '12 at 12:16

1 Answers1

0
  • a Windows shebang line? Windows doesn't understand or use shebang lines

  • it's usually better to use with to open files (it ensures they get closed again)

  • you do not provide a path to the .mp3 file; are you sure it is in the right directory?

  • it would be clearer if you separated out the 'get artwork' stuff as a function, or better yet, use an existing module like mutagen

  • similarly, it is not clear how you are exposing this to a browser; using an existing module like Flask is probably a good solution

Your code becomes something like

import mutagen
from flask import make_response
import os

music_dir = 'c:/www/music'

@app.route('/cover')
def cover_art():
    fname = 'music.mp3'
    mp3 = mutagen.File(os.path.join(music_dir, fname))
    art = mp3.tags['APIC:'].data
    def wsgi_app(environ, start_response):
        start_response('200 OK', [('Content-type', 'image/jpeg')])
        return art
    return make_response(wsgi_app)
Hugh Bothwell
  • 55,315
  • 8
  • 84
  • 99