Comments (4)
Have you tried using the parser classes directly? If you know what kind of file type you're handling, you should be able to use:
from tinytag import MP3
mp3_info = MP3(my_bytes_io)
mp3_info.load(tags=True, duration=True)
I agree that this is pretty ugly API wise, so we can discuss how we could make this more accessible, but I think this should work.
from tinytag.
I got this to work by writing my BytesIO
object into a temporary file and then reading that file into TinyTag. The temporary file automatically gets deleted as soon as the with
statement is finished.
Assuming data
contains a BytesIO
object:
import tempfile
with tempfile.NamedTemporaryFile() as tmp:
tmp.write(data.getvalue())
tmp.seek(0)
tag = TinyTag.get(tmp.name)
Here is a full example showing how it works in practice:
Full example
import tempfile
import requests
import io
from tinytag import TinyTag
def download_file(url: str) -> io.BytesIO:
"""
Downloads the file from the given URL and returns it as a BytesIO object
Args:
url: The url to run from
Returns:
An object containing the file
"""
try:
response = requests.get(url, stream=True)
response.raise_for_status()
except requests.exceptions.RequestException as e:
raise Exception(f"Could not run file from {url}: {e}")
file = io.BytesIO()
for chunk in response.iter_content(chunk_size=8192):
file.write(chunk)
file.seek(0)
return file
def read_bytes_io_as_tiny_tag(data: io.BytesIO) -> TinyTag:
"""
Reads the given BytesIO object as a file and returns it
Args:
data: The BytesIO object to read
Returns:
The file
"""
with tempfile.NamedTemporaryFile() as tmp:
tmp.write(data.getvalue())
tmp.seek(0)
tag = TinyTag.get(tmp.name)
return tag
def run():
url = "https://example.com/file.mp3"
data = download_file(url)
tag = read_bytes_io_as_tiny_tag(data=data)
print('This track is by %s.' % tag.artist)
print('It is %f seconds long.' % tag.duration)
from tinytag.
Tested on Windows, the code above won't run if you don't set delete = False
in tempfile.NamedTemporaryFile()
I don't know why but this should work
`
import tempfile
with tempfile.NamedTemporaryFile(delete = False, suffix = ".mp3") as tmp:
tmp.write(data.getvalue())
tmp.seek(0)
tag = TinyTag.get(tmp.name)
`
Do you have any solution for this? Deleting temp file manually seems not a good idea :(
from tinytag.
Can you check if this branch works well for your needs? https://github.com/mathiascode/tinytag/tree/bytesio
You can provide a file-like object like this:
tag = TinyTag.get(file_obj=file_obj)
from tinytag.
Related Issues (20)
- [BUG] TinyTag cannot properly read metadata of two folders HOT 3
- Feature Request: Adding Song Titles HOT 2
- [BUG] AIFF text should be treated as UTF-8 HOT 2
- [BUG] Incorrect bitrate and duration for VBR mp3s HOT 5
- [BUG] UnicodeDecodeError on valid MP3 file
- Make use of code linting HOT 1
- How to handle filebuffer instead of filenames
- [BUG] Some mp3 files report 1/2 of actual duration HOT 2
- Some MP3‘s genre cannot be read correctly since v1.8.0
- [Question] - detect image encoding / format HOT 1
- tag artist, quando adicionado mais de um artista a string fica mal formatada. HOT 1
- [BUG] struct.error: unpack requires a buffer of 4 bytes HOT 8
- [BUG] Parsing mp3 file w/ valid TDRC tag does not populate year HOT 4
- Replace deprecated aifc module HOT 3
- [BUG] TDRC tag not captured as "year" HOT 2
- [BUG] Can't read `extra` tags from .m4a files HOT 1
- Expose a list of supported file extensions HOT 4
- Fail to read track_total, but it is ok in foobar
- asyncio interfaces HOT 3
- [BUG] Incompatibility with Python-pip HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from tinytag.