Skip to content

Commit

Permalink
Now get the track title for various music trackers files
Browse files Browse the repository at this point in the history
  • Loading branch information
son-link committed May 8, 2023
1 parent f3079b8 commit f300758
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 18 deletions.
30 changes: 24 additions & 6 deletions PQMusic/player.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from PyQt5.QtGui import QIcon, QPixmap, QStandardItem
from PyQt5 import QtWidgets
from pathlib import Path
from .utils import getMetaData, openM3U, saveM3U
from .utils import getMetaData, openM3U, saveM3U, getTrackerTitle
from urllib.parse import urlparse
from os import path, access, R_OK, mkdir, environ, listdir
from .sys_notify import Notification, init
Expand Down Expand Up @@ -65,14 +65,20 @@ def addFile(self, file):
if self.checkValidFile(file):
self.queueList.addMedia(QMediaContent(QUrl.fromLocalFile(file)))
tags = getMetaData(file)
trackerTitle = getTrackerTitle(file)

if tags['notags']:
if trackerTitle:
item = QStandardItem(trackerTitle)
elif tags['notags']:
item = QStandardItem(tags['notags'])
else:
item = QStandardItem('{} - {}'.format(
tags['artist'],
tags['title']
))
if tags['artist']:
item = QStandardItem('{} - {}'.format(
tags['artist'],
tags['title']
))
else:
item = QStandardItem(tags['title'])

tags['file'] = file

Expand Down Expand Up @@ -164,6 +170,18 @@ def durationChanged(self, duration):
self.currentTrackDuration = duration
self.parent.totalTimeLabel.setText(total_time)

filepath = self.player.currentMedia().canonicalUrl().toString()
trackerTitle = getTrackerTitle(filepath)
if trackerTitle:
self.parent.titleLabel.setText(trackerTitle)
self.parent.artistLabel.setText(
_translate('MainWindow', 'Unknown')
)

self.parent.albumLabel.setText(
_translate('MainWindow', 'Unknown')
)

def metaDataChanged(self):
""" This function is called whenever the metadata changes,
e.g. track changes or is received during a live stream.
Expand Down
72 changes: 61 additions & 11 deletions PQMusic/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,9 @@ def getMetaData(filename):
dict: A dictionary with the metadata
"""

trackers_extensions = ['s3m', 'xm', 'mod', 'it']
ext = Path(filename).suffix
ext = ext.replace('.', '').lower()

tags = {
'album': 'Unknown',
Expand All @@ -64,7 +66,11 @@ def getMetaData(filename):

info = None

if ext == '.mp3':
if ext in trackers_extensions:
tags['title'] = getTrackerTitle(filename, ext)
tags['artist'] = ''
return tags
elif ext == 'mp3':
info = MP3(filename, ID3=EasyID3)
else:
info = File(filename)
Expand Down Expand Up @@ -121,10 +127,15 @@ def openM3U(file):
track_info = {
'duration': duration,
}
artist, title = trackname.split(' - ', 1)
track_info['artist'] = artist
track_info['title'] = title
have_info = True

if int(duration) > 0:
artist, title = trackname.split(' - ', 1)
track_info['artist'] = artist
track_info['title'] = title
have_info = True
else:
track_info['notags'] = trackname
have_info = True
else:
have_info = False
if Path(line).is_file():
Expand Down Expand Up @@ -152,12 +163,15 @@ def saveM3U(self, filename, playlist):
with open(filename, 'w') as file:
file.write("#EXTM3U\n")
for data in playlist:
if 'notags' not in data:
file.write("#EXTINF:{},{} - {}\n".format(
data['duration'],
data['artist'],
data['title']
))
if 'notags' not in data or not data['notags']:
if not data['artist']:
file.write(f"#EXTINF:0,{data['title']}\n")
else:
file.write("#EXTINF:{},{} - {}\n".format(
data['duration'],
data['artist'],
data['title']
))
file.write("{}\n".format(data['file']))
file.close()
except IOError as x:
Expand All @@ -183,3 +197,39 @@ def getSaveVolume():
f.close()
remove(volfile)
return volume


def getTrackerTitle(filepath, ext=None):
"""Return the title of some Music Trackers formats
Args:
filepath (str): the path to the file
Return:
(str) The title if available or empty string
"""
title = ''

trackers_extensions = ['s3m', 'xm', 'mod', 'it']
filepath = filepath.replace('file://', '')

if not ext:
ext = Path(filepath).suffix
ext = ext.replace('.', '').lower()
if ext not in trackers_extensions:
return ''

with open(filepath, 'rb') as f:
head = f.read(128)
if ext == 'xm':
title = head[17:37]
elif ext == 's3m':
title = head[0:28]
elif ext == 'mod':
title = head[0:20]
elif ext == 'it':
title = head[4:26]

title = title.decode('utf-8', 'ignore').strip()
fil = filter(str.isprintable, title)
title = "".join(fil)
return title
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

PQMusic is a minimalist and easy to use audio player for download and use.

You can play your local files, from a direct url or streaming (for example a online radio) and import/export playlists on M3U format.
You can play your local files, from a direct url or streaming (for example a online radio) and import/export playlists on M3U format. Also you can play some music trackers files (.it, .mod, .s3m and .xm)

Adding files from Open with on your file manager
![Add files from Open with on your file manager](file-manager-menu.png)
Expand Down

0 comments on commit f300758

Please sign in to comment.