Reputation: 467
I just started coding Python a few days ago. I'm still a noob but I'm familiar with other languages so I'm learning quick. I need help with this script I'm writing. I'm using Mutagen to tag m4a files but I'm having issues with saving artwork from a url. (Python Version 3.7.4)
Mutagen Api: https://mutagen.readthedocs.io/en/latest/api/mp4.html
Below is code that works but it only works for local images. I need to be able to do the same thing but with an image from a url: https://is1-ssl.mzstatic.com/image/thumb/Music123/v4/e3/4a/e6/e34ae621-5922-140d-7db0-f6ce0b44d626/19UMGIM78396.rgb.jpg/1400x1400bb.jpg.
from mutagen.mp4 import MP4, MP4Cover
from time import sleep
from os import listdir, getcwd
import datetime
import requests
import urllib.request
.....MORECODE HERE......
with open('artwork.jpg', "rb") as f:
currentfile["covr"] = [
MP4Cover(f.read(), imageformat=MP4Cover.FORMAT_JPEG)
]
Now below is the code I have right now but Python keeps crashing every time I run it. I tried a couple different methods but I can't seem to figure it out. It has to be fairly simple for someone experienced with Python. I tried this already (didn't work) Python Mutagen: add cover photo/album art by url?. I think it might be for an older version of Python. Any ideas?
from mutagen.mp4 import MP4, MP4Cover
from time import sleep
from os import listdir, getcwd
import datetime
import requests
import urllib.request
.....MORE CODE HERE.....
fd = urllib.request.urlopen("https://is1-ssl.mzstatic.com/image/thumb/Music123/v4/e3/4a/e6/e34ae621-5922-140d-7db0-f6ce0b44d626/19UMGIM78396.rgb.jpg/1400x1400bb.jpg")
with open(fd, "rb") as f:
currentfile["covr"] = [
MP4Cover(f.read(), imageformat=MP4Cover.FORMAT_JPEG)
]
Upvotes: 0
Views: 541
Reputation: 91
File "mutagen.py", line 11, in with open(fd, "rb") as f:
TypeError: expected str, bytes or os.PathLike object, not HTTPResponse
You might consider something as follows:
url_artwork="https://is1-ssl.mzstatic.com/image/thumb/Music123/v4/e3/4a/e6/e34ae621-5922-140d-7db0-f6ce0b44d626/19UMGIM78396.rgb.jpg/1400x1400bb.jpg"
urllib.request.urlretrieve(url_artwork,"local.jpg")
with open("local.jpg", "rb") as f:
currentfile["covr"] = [
MP4Cover(f.read(), imageformat=MP4Cover.FORMAT_JPEG)
]
Upvotes: 1