-
Notifications
You must be signed in to change notification settings - Fork 157
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
No io.BytesIO support in mutagen.File function #572
Comments
import io
import urllib.request
import mutagen
r = urllib.request.urlopen("https://opus-codec.org/static/examples/ehren-paper_lights-96.opus")
f = io.BytesIO(r.read())
print(mutagen.File(f))
yeah, this was all added years later while trying to not break existing users, so lots of hacks and magic in that area... |
Mutagen waits that music will have ID3 tag, but it may not (most common situation for me is mp3/m4a music file without tag header - I create it with my code). But here we have only pure BytesIO (with music content) so Kinds have 0 score. Funny story that I have codec from service (but I cannot be sure that I will cover any cases), so I want to use this codec within BytesIO. class Descriptor:
...
@contextlib.asynccontextmanager
async def to_track(self) -> AsyncIterator[mutagen.FileType]:
async with aiofiles.open(self.__filepath, "br") as file:
buffer = io.BytesIO(await file.read())
logging.debug("BUFFER IS READY")
# if (track := mutagen.File(FileThing(buffer, self.__filepath.name, self.__filepath.name))) is None:
if (track := mutagen.File(buffer)) is None:
logging.debug("MUTAGEN ERROR")
raise RuntimeError
... This is an example from my application (for now I totally rewrite it and public ready pieces in my GitHub). Commented line is solution. |
I see, thanks. So we need some way to pass a filename as a hint for the type selection. |
I need to open the file from
BytesIO
butmutagen
doesn't allow me to use it.is_fileobj
: Broken - doesn't checks anything for real (almost any types is notstr
orbytes
). You can useruntime_checkable
protocols for example (or abstract classes from standart library)mutagen/mutagen/_util.py
Lines 62 to 69 in 30f373f
_openfile
: Useless check -FileThing
is just a container butisinstance
check deny to use user containers variants. Consider to add special class or makeFileThing
public (PyCharm warning:Access to a protected member _util of a module
) (for now you can just check attributes of filething by hasattr as you do in other places)mutagen/mutagen/_util.py
Lines 219 to 223 in 30f373f
You know, that's very funny when open is not supported
BytesIO
but save supports.For now temporary solution is to use
ThingFile
from non-public module_util.py
If you now the correct way to use BytesIO in mutagen, let me now.
P.S. Mutagen code is awful, have a nice day :D
The text was updated successfully, but these errors were encountered: