-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMusicClass.py
More file actions
80 lines (69 loc) · 2.42 KB
/
MusicClass.py
File metadata and controls
80 lines (69 loc) · 2.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
from tinytag import TinyTag
import math
from PyQt5 import QtCore
from PyQt5.QtMultimedia import QMediaContent
from PyQt5 import QtCore, QtGui, QtWidgets
# tinytag gets
'''
{
'filesize': 3031559,
'album': None,
'albumartist': None,
'artist': None,
'audio_offset': 5668,
'bitrate': None,
'channels': 2,
'comment': None,
'composer': None,
'disc': None,
'disc_total': None,
'duration': 2.56,
'genre': None,
'samplerate': 44100,
'title':None,
'track': None,
'track_total': None,
'year': None
}
'''
# My current plan for class
# init takes file loc and gets base details on it stuff needed to show
# if the song is played or if later on the user can look at the info on the file show information got from the properties of the file
# if the user wants to change these properties they can and it will change for the file
# thus never storing much and keeping memory low
class Song():
def __init__(self, location, fileName):
self.location = location
self.fileName = fileName
self.artist = 'No Artist'
# for now duration will be (min, sec) to get proper time min + (sec / 100)
self.duration = self.getDuration()
self.parseSongName()
self.makeStandardItem()
def getDuration(self):
ret = self.location + '/' + self.fileName
tag = TinyTag.get(ret)
if tag.artist:
self.artist = tag.artist
return splitDuration(tag.duration)
def returnMediaContent(self):
url = QtCore.QUrl.fromLocalFile(self.location + '/' + self.fileName)
return QMediaContent(url)
def parseSongName(self):
splitAt = len(self.fileName.split('.')[-1]) + 1
print(self.artist)
if ' - ' in self.fileName:
splitStr = self.fileName.split(' - ')[0]
self.songName = self.fileName[len(splitStr) + 3:(len(self.fileName) - splitAt)]
if self.artist == 'No Artist':
self.artist = self.fileName.split(' - ')[0]
else:
self.songName = self.fileName[0:(len(self.fileName) - splitAt)]
def makeStandardItem(self):
min, sec = self.duration
self.songStandardItem = QtGui.QStandardItem((self.songName + "\n-" + self.artist + " " + str(min) + "." + str(sec)))
self.songStandardItem.setEditable(False)
def splitDuration(duration):
min = math.floor(duration / 60)
sec = math.floor(duration % 60)
return min,sec