2009-10-23 14:35:51 +00:00
|
|
|
# Created By: Virgil Dupras
|
|
|
|
# Created On: 2009-10-23
|
2015-01-03 21:30:57 +00:00
|
|
|
# Copyright 2015 Hardcoded Software (http://www.hardcoded.net)
|
2014-10-13 19:08:59 +00:00
|
|
|
#
|
2015-01-03 21:33:16 +00:00
|
|
|
# This software is licensed under the "GPLv3" License as described in the "LICENSE" file,
|
2014-10-13 19:08:59 +00:00
|
|
|
# which should be included with this package. The terms are also available at
|
2015-01-03 21:33:16 +00:00
|
|
|
# http://www.gnu.org/licenses/gpl-3.0.html
|
2009-10-23 14:35:51 +00:00
|
|
|
|
2021-08-19 05:14:26 +00:00
|
|
|
import mutagen
|
2013-07-14 21:43:58 +00:00
|
|
|
from hscommon.util import get_file_ext, format_size, format_time
|
|
|
|
|
2016-06-01 01:43:24 +00:00
|
|
|
from core.util import format_timestamp, format_perc, format_words, format_dupe_count
|
2009-12-30 10:37:57 +00:00
|
|
|
from core import fs
|
2009-10-23 14:35:51 +00:00
|
|
|
|
2014-10-13 19:08:59 +00:00
|
|
|
TAG_FIELDS = {
|
2020-01-01 02:16:27 +00:00
|
|
|
"audiosize",
|
|
|
|
"duration",
|
|
|
|
"bitrate",
|
|
|
|
"samplerate",
|
|
|
|
"title",
|
|
|
|
"artist",
|
|
|
|
"album",
|
|
|
|
"genre",
|
|
|
|
"year",
|
|
|
|
"track",
|
|
|
|
"comment",
|
2014-10-13 19:08:59 +00:00
|
|
|
}
|
2009-10-23 14:35:51 +00:00
|
|
|
|
2021-08-19 05:14:26 +00:00
|
|
|
# This is a temporary workaround for migration from hsaudiotag for the can_handle method
|
|
|
|
SUPPORTED_EXTS = {"mp3", "wma", "m4a", "m4p", "ogg", "flac", "aif", "aiff", "aifc"}
|
|
|
|
|
2020-01-01 02:16:27 +00:00
|
|
|
|
2009-10-23 14:35:51 +00:00
|
|
|
class MusicFile(fs.File):
|
|
|
|
INITIAL_INFO = fs.File.INITIAL_INFO.copy()
|
2020-01-01 02:16:27 +00:00
|
|
|
INITIAL_INFO.update(
|
|
|
|
{
|
|
|
|
"audiosize": 0,
|
|
|
|
"bitrate": 0,
|
|
|
|
"duration": 0,
|
|
|
|
"samplerate": 0,
|
|
|
|
"artist": "",
|
|
|
|
"album": "",
|
|
|
|
"title": "",
|
|
|
|
"genre": "",
|
|
|
|
"comment": "",
|
|
|
|
"year": "",
|
|
|
|
"track": 0,
|
|
|
|
}
|
|
|
|
)
|
2012-05-29 21:39:54 +00:00
|
|
|
__slots__ = fs.File.__slots__ + tuple(INITIAL_INFO.keys())
|
2014-10-13 19:08:59 +00:00
|
|
|
|
2009-10-23 14:35:51 +00:00
|
|
|
@classmethod
|
|
|
|
def can_handle(cls, path):
|
|
|
|
if not fs.File.can_handle(path):
|
|
|
|
return False
|
2021-08-19 05:14:26 +00:00
|
|
|
return get_file_ext(path.name) in SUPPORTED_EXTS
|
2014-10-13 19:08:59 +00:00
|
|
|
|
2013-07-14 21:43:58 +00:00
|
|
|
def get_display_info(self, group, delta):
|
|
|
|
size = self.size
|
|
|
|
duration = self.duration
|
|
|
|
bitrate = self.bitrate
|
|
|
|
samplerate = self.samplerate
|
|
|
|
mtime = self.mtime
|
|
|
|
m = group.get_match_of(self)
|
|
|
|
if m:
|
|
|
|
percentage = m.percentage
|
|
|
|
dupe_count = 0
|
|
|
|
if delta:
|
|
|
|
r = group.ref
|
|
|
|
size -= r.size
|
|
|
|
duration -= r.duration
|
|
|
|
bitrate -= r.bitrate
|
|
|
|
samplerate -= r.samplerate
|
|
|
|
mtime -= r.mtime
|
|
|
|
else:
|
|
|
|
percentage = group.percentage
|
|
|
|
dupe_count = len(group.dupes)
|
2020-01-01 02:16:27 +00:00
|
|
|
dupe_folder_path = getattr(self, "display_folder_path", self.folder_path)
|
2013-07-14 21:43:58 +00:00
|
|
|
return {
|
2020-01-01 02:16:27 +00:00
|
|
|
"name": self.name,
|
|
|
|
"folder_path": str(dupe_folder_path),
|
|
|
|
"size": format_size(size, 2, 2, False),
|
|
|
|
"duration": format_time(duration, with_hours=False),
|
|
|
|
"bitrate": str(bitrate),
|
|
|
|
"samplerate": str(samplerate),
|
|
|
|
"extension": self.extension,
|
|
|
|
"mtime": format_timestamp(mtime, delta and m),
|
|
|
|
"title": self.title,
|
|
|
|
"artist": self.artist,
|
|
|
|
"album": self.album,
|
|
|
|
"genre": self.genre,
|
|
|
|
"year": self.year,
|
|
|
|
"track": str(self.track),
|
|
|
|
"comment": self.comment,
|
|
|
|
"percentage": format_perc(percentage),
|
|
|
|
"words": format_words(self.words) if hasattr(self, "words") else "",
|
|
|
|
"dupe_count": format_dupe_count(dupe_count),
|
2013-07-14 21:43:58 +00:00
|
|
|
}
|
2014-10-13 19:08:59 +00:00
|
|
|
|
2009-10-23 14:35:51 +00:00
|
|
|
def _read_info(self, field):
|
2010-12-29 12:17:30 +00:00
|
|
|
fs.File._read_info(self, field)
|
2009-10-23 14:35:51 +00:00
|
|
|
if field in TAG_FIELDS:
|
2021-08-19 05:14:26 +00:00
|
|
|
# The various conversions here are to make this look like the previous implementation
|
|
|
|
file = mutagen.File(str(self.path), easy=True)
|
|
|
|
self.audiosize = self.path.stat().st_size
|
|
|
|
self.bitrate = file.info.bitrate / 1000
|
|
|
|
self.duration = file.info.length
|
|
|
|
self.samplerate = file.info.sample_rate
|
|
|
|
self.artist = ", ".join(file.tags.get("artist") or [])
|
|
|
|
self.album = ", ".join(file.tags.get("album") or [])
|
|
|
|
self.title = ", ".join(file.tags.get("title") or [])
|
|
|
|
self.genre = ", ".join(file.tags.get("genre") or [])
|
|
|
|
self.comment = ", ".join(file.tags.get("comment") or [""])
|
|
|
|
self.year = ", ".join(file.tags.get("date") or [])
|
|
|
|
self.track = (file.tags.get("tracknumber") or [""])[0]
|