Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get a uploaded zip file in Django and save its contents (mp3 files)?

I'm new in Django and im trying to achieve what mentioned...but i was not able to get some AudioField or MediaField in Django models, more or less likely ImageField.

Explaining better what i want:

I want to give to the user a form where he can fill with some information and can upload a zip file containing mp3 files. Then, in server i want to get this zip file, unzip it, get all of mp3 inside and get some information about these files (name, artist, duration, etc) and save this in my model (Music).

Is there some tutorial explaining how to achieve that or some links explaining how to work with zip files, and mp3 files?

like image 710
WitaloBenicio Avatar asked Feb 10 '23 09:02

WitaloBenicio


1 Answers

I think all you need here are the 2 following links:

Python standard library (both 2.x.x and 3.x.x) contains module for work with zip files. https://docs.python.org/3/library/zipfile.html

i.e.:

with ZipFile('music_files.zip') as zip_file:
    # get the list of files
    names = zip_file.namelist()
    # handle your files as you need. You can read the file with:
    with zip_file.open(name) as f:
        music_file = f.read()
        # retrieve music_file metadata here

As for the extraction of mp3 files metadata there is a library: http://eyed3.nicfit.net

Hope it will help you.

like image 65
shalakhin Avatar answered Feb 13 '23 23:02

shalakhin