forked from trav/lintunes
v0.75.0: Preferences › General › Compress lossless imports to MP3
FLAC, WAV, AIFF and Apple Lossless files being imported are converted by ffmpeg to an MP3 at 320/256/192/160/128 kbps (off by default) and the MP3 goes in the library, tags and cover art copied over. The original file is only ever read. One song shows its progress in the status bar; more get a window with a bar per song and Cancel / Done. Cancel takes back every song the import added, finished ones included: the copies it made go to the trash, and a file imported in place is never trashed.
This commit is contained in:
@@ -1,3 +1,3 @@
|
||||
"""LinTunes — iTunes-style music library manager and player for Linux."""
|
||||
|
||||
__version__ = "0.74.1"
|
||||
__version__ = "0.75.0"
|
||||
|
||||
@@ -0,0 +1,354 @@
|
||||
"""An import that compresses lossless files to MP3 (Round 106; the
|
||||
converter is ``importers/lossless.py``).
|
||||
|
||||
``CompressImport`` runs one batch: hands the files to a ``CompressWorker``,
|
||||
imports each one as it's ready (in order, so a playlist drop keeps its
|
||||
order), and remembers every track it added — Cancel takes back *all* of
|
||||
them, finished ones included, which is the point of the button. One file
|
||||
shows its progress in the status bar (MainWindow's shared widgets); more
|
||||
than one gets ``CompressProgress``, a non-modal window with a bar per song
|
||||
and Cancel / Done. Closing that window while it works only hides it (the
|
||||
status-bar line brings it back); closing it after is Done.
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
import logging
|
||||
import shutil
|
||||
from pathlib import Path
|
||||
|
||||
from PyQt6.QtCore import QObject, pyqtSignal
|
||||
from PyQt6.QtWidgets import (
|
||||
QDialog, QHBoxLayout, QHeaderView, QLabel, QMessageBox, QProgressBar,
|
||||
QPushButton, QTreeWidget, QTreeWidgetItem, QVBoxLayout,
|
||||
)
|
||||
|
||||
from lintunes import tagging
|
||||
from lintunes.importers import file_importer, lossless
|
||||
|
||||
log = logging.getLogger(__name__)
|
||||
|
||||
WAITING = "Waiting"
|
||||
IMPORTED = "Imported"
|
||||
CONVERTED = "Converted and imported"
|
||||
ALREADY = "Already in your library"
|
||||
CANCELLED = "Cancelled"
|
||||
NOT_IMPORTED = "Not imported"
|
||||
|
||||
|
||||
def plan_items(files: list[Path], manager, music_dir: Path):
|
||||
"""``(source, convert?, duration ms)`` for each file. A lossless file is
|
||||
converted unless this very file is already in the library (then it
|
||||
imports as it always did: as the existing track)."""
|
||||
by_location = file_importer.location_index(manager)
|
||||
items = []
|
||||
for path in files:
|
||||
source = Path(path).resolve()
|
||||
convert = (source not in by_location and lossless.is_lossless(source))
|
||||
duration = 0
|
||||
if convert:
|
||||
try:
|
||||
duration = int(tagging.read_tags(source).get("total_time") or 0)
|
||||
except Exception:
|
||||
duration = 0
|
||||
items.append((source, convert, duration))
|
||||
return items
|
||||
|
||||
|
||||
class CompressProgress(QDialog):
|
||||
"""A row per song: name, a progress bar, and what happened."""
|
||||
cancel_requested = pyqtSignal()
|
||||
done_requested = pyqtSignal()
|
||||
|
||||
def __init__(self, items, kbps: int, parent=None):
|
||||
super().__init__(parent)
|
||||
self.setWindowTitle("Importing")
|
||||
self.setModal(False)
|
||||
self.resize(640, 440)
|
||||
self._finished = False
|
||||
self._kbps = kbps
|
||||
|
||||
layout = QVBoxLayout(self)
|
||||
self._summary = QLabel()
|
||||
layout.addWidget(self._summary)
|
||||
self._tree = QTreeWidget()
|
||||
self._tree.setColumnCount(3)
|
||||
self._tree.setHeaderLabels(["Song", "Progress", "Status"])
|
||||
self._tree.setRootIsDecorated(False)
|
||||
self._tree.setUniformRowHeights(True)
|
||||
header = self._tree.header()
|
||||
header.setSectionResizeMode(0, QHeaderView.ResizeMode.Stretch)
|
||||
header.setSectionResizeMode(1, QHeaderView.ResizeMode.Fixed)
|
||||
header.setSectionResizeMode(2, QHeaderView.ResizeMode.ResizeToContents)
|
||||
header.setStretchLastSection(False)
|
||||
self._tree.setColumnWidth(1, 140)
|
||||
self._bars: list[QProgressBar] = []
|
||||
self._rows: list[QTreeWidgetItem] = []
|
||||
for source, convert, _duration in items:
|
||||
item = QTreeWidgetItem([source.name, "", WAITING])
|
||||
item.setToolTip(0, str(source))
|
||||
self._tree.addTopLevelItem(item)
|
||||
bar = QProgressBar()
|
||||
bar.setRange(0, 100)
|
||||
bar.setValue(0)
|
||||
bar.setTextVisible(convert)
|
||||
bar.setMaximumHeight(16)
|
||||
self._tree.setItemWidget(item, 1, bar)
|
||||
self._rows.append(item)
|
||||
self._bars.append(bar)
|
||||
layout.addWidget(self._tree, 1)
|
||||
|
||||
buttons = QHBoxLayout()
|
||||
self._note = QLabel(f"Lossless files become {kbps} kbps MP3s; "
|
||||
"the originals aren't touched.")
|
||||
self._note.setWordWrap(True)
|
||||
buttons.addWidget(self._note, 1)
|
||||
self.cancel_button = QPushButton("Cancel")
|
||||
self.cancel_button.setToolTip(
|
||||
"Stop, and take back every song this import added")
|
||||
self.cancel_button.clicked.connect(self.cancel_requested)
|
||||
self.done_button = QPushButton("Done")
|
||||
self.done_button.setEnabled(False)
|
||||
self.done_button.clicked.connect(self.done_requested)
|
||||
buttons.addWidget(self.cancel_button)
|
||||
buttons.addWidget(self.done_button)
|
||||
layout.addLayout(buttons)
|
||||
self._refresh_summary()
|
||||
|
||||
def status(self, index: int) -> str:
|
||||
return self._rows[index].text(2)
|
||||
|
||||
def set_status(self, index: int, text: str, percent: int | None = None):
|
||||
item = self._rows[index]
|
||||
item.setText(2, text)
|
||||
item.setToolTip(2, text)
|
||||
if percent is not None:
|
||||
self._bars[index].setValue(percent)
|
||||
self._refresh_summary()
|
||||
|
||||
def started(self, index: int, converting: bool):
|
||||
self.set_status(index, "Converting…" if converting else "Copying…")
|
||||
self._tree.scrollToItem(self._rows[index])
|
||||
|
||||
def progress(self, index: int, percent: int):
|
||||
self._bars[index].setValue(percent)
|
||||
|
||||
def finish(self, cancelled: bool):
|
||||
self._finished = True
|
||||
for index, item in enumerate(self._rows):
|
||||
if item.text(2) in (WAITING, "Converting…", "Copying…"):
|
||||
self.set_status(index, CANCELLED if cancelled else NOT_IMPORTED)
|
||||
self.done_button.setEnabled(True)
|
||||
self.done_button.setDefault(True)
|
||||
self._refresh_summary()
|
||||
|
||||
def _refresh_summary(self):
|
||||
statuses = [item.text(2) for item in self._rows]
|
||||
landed = sum(1 for s in statuses if s in (IMPORTED, CONVERTED, ALREADY))
|
||||
failed = sum(1 for s in statuses if s.startswith("Couldn't"))
|
||||
text = f"{landed} of {len(statuses)} songs imported"
|
||||
if failed:
|
||||
text += f" · {failed} couldn't be"
|
||||
self._summary.setText(("Done: " if self._finished else "") + text)
|
||||
|
||||
def closeEvent(self, event):
|
||||
# Working: just hide (the status bar brings it back). Finished: Done.
|
||||
if self._finished:
|
||||
self.done_requested.emit()
|
||||
super().closeEvent(event)
|
||||
|
||||
|
||||
class CompressImport(QObject):
|
||||
"""One compressing import, from start to Done or Cancel."""
|
||||
|
||||
# (label, value, maximum) for the status bar while it works
|
||||
status = pyqtSignal(str, int, int)
|
||||
landed = pyqtSignal() # a song joined the library
|
||||
worker_finished = pyqtSignal() # the status-bar widgets are free again
|
||||
over = pyqtSignal(str) # the batch is closed; a closing message
|
||||
|
||||
def __init__(self, manager, music_dir: Path, items, kbps: int,
|
||||
playlist_pid: str = "", position: int | None = None,
|
||||
parent_widget=None, worker_factory=None):
|
||||
super().__init__(parent_widget)
|
||||
self._manager = manager
|
||||
self._music_dir = music_dir
|
||||
self._items = items
|
||||
self._kbps = kbps
|
||||
self._pid = playlist_pid
|
||||
self._position = position
|
||||
self._inserted = 0
|
||||
self._by_location = file_importer.location_index(manager)
|
||||
self.added: list[int] = [] # new tracks, in order
|
||||
self._owned: set[int] = set() # ...whose file this import made
|
||||
self._converted = 0
|
||||
self._failures: list[str] = []
|
||||
self._percent = {}
|
||||
self._closed = False
|
||||
factory = worker_factory or lossless.CompressWorker
|
||||
self.worker = factory([(s, c, d) for s, c, d in items], kbps, self)
|
||||
self.worker.started.connect(self._on_started)
|
||||
self.worker.progress.connect(self._on_progress)
|
||||
self.worker.ready.connect(self._on_ready)
|
||||
self.worker.failed.connect(self._on_failed)
|
||||
self.worker.finished.connect(self._on_finished)
|
||||
self.window = None
|
||||
if len(items) > 1:
|
||||
self.window = CompressProgress(items, kbps, parent_widget)
|
||||
self.window.cancel_requested.connect(self.request_cancel)
|
||||
self.window.done_requested.connect(self.done)
|
||||
|
||||
def start(self):
|
||||
self.worker.start()
|
||||
if self.window is not None:
|
||||
self.window.show()
|
||||
|
||||
def show_window(self):
|
||||
if self.window is not None and not self._closed:
|
||||
self.window.show()
|
||||
self.window.raise_()
|
||||
self.window.activateWindow()
|
||||
|
||||
# ---- the worker's reports ----
|
||||
|
||||
def _label(self, index: int) -> str:
|
||||
return self._items[index][0].name
|
||||
|
||||
def _emit_status(self, index: int, percent: int):
|
||||
total = len(self._items)
|
||||
name = self._label(index)
|
||||
name = name if len(name) <= 40 else name[:39] + "…"
|
||||
if total == 1:
|
||||
verb = "Converting" if self._items[index][1] else "Importing"
|
||||
self.status.emit(f"{verb} {name}", percent, 100)
|
||||
else:
|
||||
self.status.emit(f"Importing {index + 1} of {total} · {name}",
|
||||
index * 100 + percent, total * 100)
|
||||
|
||||
def _on_started(self, index: int):
|
||||
if self.window is not None:
|
||||
self.window.started(index, self._items[index][1])
|
||||
self._emit_status(index, 0)
|
||||
|
||||
def _on_progress(self, index: int, percent: int):
|
||||
if self.window is not None:
|
||||
self.window.progress(index, percent)
|
||||
self._emit_status(index, percent)
|
||||
|
||||
def _on_ready(self, index: int, path: str, converted: bool):
|
||||
if self._closed:
|
||||
return
|
||||
source = self._items[index][0]
|
||||
before = set(self._manager.library.tracks)
|
||||
try:
|
||||
if converted:
|
||||
track = file_importer.import_converted(
|
||||
Path(path), source, self._music_dir, self._manager,
|
||||
self._by_location)
|
||||
else:
|
||||
track = file_importer.import_file(
|
||||
source, self._music_dir, self._manager, self._by_location)
|
||||
except Exception as e:
|
||||
log.warning("Failed to import %s: %s", source, e)
|
||||
self._on_failed(index, str(e))
|
||||
return
|
||||
if track is None:
|
||||
self._on_failed(index, "not an audio file LinTunes can read")
|
||||
return
|
||||
new = track.track_id not in before
|
||||
if new:
|
||||
self.added.append(track.track_id)
|
||||
# A file inside the music folder is imported where it is: not
|
||||
# ours to trash on Cancel.
|
||||
if Path(track.location) != source:
|
||||
self._owned.add(track.track_id)
|
||||
if converted:
|
||||
self._converted += 1
|
||||
if self._pid:
|
||||
position = (None if self._position is None
|
||||
else self._position + self._inserted)
|
||||
self._manager.add_tracks_to_playlist(
|
||||
self._pid, [track.track_id], position, undoable=False)
|
||||
self._inserted += 1
|
||||
if self.window is not None:
|
||||
self.window.set_status(
|
||||
index, (CONVERTED if converted else IMPORTED) if new else ALREADY,
|
||||
100)
|
||||
self.landed.emit()
|
||||
|
||||
def _on_failed(self, index: int, reason: str):
|
||||
self._failures.append(f"{self._label(index)}: {reason}")
|
||||
if self.window is not None:
|
||||
self.window.set_status(index, f"Couldn't import: {reason}")
|
||||
|
||||
def _on_finished(self, summary: dict):
|
||||
shutil.rmtree(self.worker.tmp_dir, ignore_errors=True)
|
||||
self.worker_finished.emit()
|
||||
if self._closed:
|
||||
return
|
||||
if self.window is not None:
|
||||
self.window.finish(summary.get("cancelled", False))
|
||||
return
|
||||
# One song: nothing more to decide.
|
||||
self.done()
|
||||
|
||||
# ---- closing ----
|
||||
|
||||
def summary(self) -> str:
|
||||
n = len(self.added)
|
||||
text = f"Imported {n} song{'s' if n != 1 else ''}"
|
||||
if self._converted:
|
||||
text += f" ({self._converted} converted to {self._kbps} kbps MP3)"
|
||||
if self._failures:
|
||||
text += f" · {len(self._failures)} couldn't be"
|
||||
return text
|
||||
|
||||
def done(self):
|
||||
if self._closed:
|
||||
return
|
||||
self._closed = True
|
||||
if self.window is not None:
|
||||
self.window.hide()
|
||||
self.over.emit(self.summary())
|
||||
|
||||
def request_cancel(self, confirm: bool = True):
|
||||
"""Stop, and take back every song this batch added. Asks first when
|
||||
that means removing songs already imported."""
|
||||
if self._closed:
|
||||
return
|
||||
if confirm and self.added:
|
||||
n = len(self.added)
|
||||
box = QMessageBox(self.window or self.parent())
|
||||
box.setWindowTitle("Cancel Import")
|
||||
box.setText(f"Cancel the import and remove the {n} "
|
||||
f"song{'s' if n != 1 else ''} it already added?")
|
||||
box.setInformativeText(
|
||||
"Their converted copies go to the trash. The files you "
|
||||
"imported from aren't touched.")
|
||||
go = box.addButton("Cancel Import", QMessageBox.ButtonRole.DestructiveRole)
|
||||
keep = box.addButton("Keep Them", QMessageBox.ButtonRole.RejectRole)
|
||||
box.setDefaultButton(keep)
|
||||
box.exec()
|
||||
if box.clickedButton() is not go:
|
||||
return
|
||||
self.worker.cancel()
|
||||
self._closed = True
|
||||
failures = self._manager.discard_imports(self.added, self._owned)
|
||||
removed = len(self.added)
|
||||
self.added = []
|
||||
if self.window is not None:
|
||||
self.window.hide()
|
||||
self.landed.emit()
|
||||
text = "Import cancelled"
|
||||
if removed:
|
||||
text += f" — removed {removed} song{'s' if removed != 1 else ''}"
|
||||
if failures:
|
||||
text += f" ({len(failures)} file(s) couldn't be trashed)"
|
||||
self.over.emit(text)
|
||||
|
||||
def shutdown(self):
|
||||
"""Quitting: stop converting, keep what landed."""
|
||||
self.worker.cancel()
|
||||
self._closed = True
|
||||
if self.window is not None:
|
||||
self.window.hide()
|
||||
shutil.rmtree(self.worker.tmp_dir, ignore_errors=True)
|
||||
@@ -25,7 +25,7 @@ from lintunes.fingerprint import TrackIdentifier, fpcalc_available
|
||||
from lintunes.inhibit import INHIBIT_LOGOUT, INHIBIT_SUSPEND, SleepInhibitor
|
||||
from lintunes.player import Player
|
||||
from lintunes.storage import listen_history
|
||||
from lintunes.importers import file_importer
|
||||
from lintunes.importers import file_importer, lossless
|
||||
from lintunes.gui.album_art_dialog import AlbumArtDialog
|
||||
from lintunes.gui.art_ops import embed_artwork
|
||||
from lintunes.gui.cast_dialog import AirPlayDialog, ChromecastDialog
|
||||
@@ -237,6 +237,7 @@ class MainWindow(QMainWindow):
|
||||
# A click brings back a hidden link-import progress window.
|
||||
self._sync_label.clicked.connect(self._show_url_progress)
|
||||
self._sync_label.clicked.connect(self._show_web_progress)
|
||||
self._sync_label.clicked.connect(self._show_compress_progress)
|
||||
self.statusBar().addPermanentWidget(self._sync_label)
|
||||
self._sync_progress = QProgressBar()
|
||||
self._sync_progress.setFixedWidth(160)
|
||||
@@ -274,6 +275,13 @@ class MainWindow(QMainWindow):
|
||||
self._url_worker = None
|
||||
# Calculate BPM: also shares the status-bar progress widgets.
|
||||
self._bpm_worker = None
|
||||
# A compressing import (gui/compress_import.py), from start until
|
||||
# Done or Cancel — which can be after its worker has finished.
|
||||
self._compress = None
|
||||
self._compress_reload = QTimer(self)
|
||||
self._compress_reload.setSingleShot(True)
|
||||
self._compress_reload.setInterval(400)
|
||||
self._compress_reload.timeout.connect(self._library_view_reload)
|
||||
self._bpm_results: dict[int, int] = {}
|
||||
self._url_target = None # url_import.ImportTarget, or None
|
||||
self._url_inserted = 0 # songs added to the target so far
|
||||
@@ -699,12 +707,16 @@ class MainWindow(QMainWindow):
|
||||
for worker in (self._andtunes_planner, self._andtunes_worker,
|
||||
self._export_worker, self._url_worker,
|
||||
self._bpm_worker, self._server_check,
|
||||
self._upload_worker, self._dj_prep_worker):
|
||||
self._upload_worker, self._dj_prep_worker,
|
||||
self._compress.worker if self._compress else None):
|
||||
if worker is not None and worker.busy():
|
||||
return worker
|
||||
return None
|
||||
|
||||
def _confirm_cancel_sync(self):
|
||||
if self._compress is not None and self._compress.worker.busy():
|
||||
self._compress.request_cancel()
|
||||
return
|
||||
if self._dj_prep_worker is not None and self._dj_prep_worker.busy():
|
||||
# What's prepared is kept; the song being split is abandoned.
|
||||
self._dj_prep_worker.cancel()
|
||||
@@ -1616,9 +1628,14 @@ class MainWindow(QMainWindow):
|
||||
|
||||
def import_files(self, paths, playlist_pid: str = "",
|
||||
position: int | None = None) -> list:
|
||||
"""Returns the tracks imported — except when lossless files are being
|
||||
compressed, which happens in the background and returns []."""
|
||||
music_dir = self._ensure_music_dir()
|
||||
if music_dir is None:
|
||||
return []
|
||||
if self._start_compressing_import(paths, music_dir, playlist_pid,
|
||||
position):
|
||||
return []
|
||||
self.statusBar().showMessage(f"Importing {len(paths)} item(s)…")
|
||||
imported = file_importer.import_paths(
|
||||
paths, music_dir, self._manager, playlist_pid,
|
||||
@@ -1628,6 +1645,71 @@ class MainWindow(QMainWindow):
|
||||
self._library_view.reload()
|
||||
return imported
|
||||
|
||||
def _start_compressing_import(self, paths, music_dir, playlist_pid,
|
||||
position) -> bool:
|
||||
"""With Compress lossless imports on and a lossless file among
|
||||
``paths``, run the whole import through the converter (in order, so a
|
||||
playlist drop keeps its order). False = import the ordinary way."""
|
||||
on, kbps = lossless.settings(self._prefs)
|
||||
if not on:
|
||||
return False
|
||||
from lintunes.gui import compress_import
|
||||
files = file_importer.expand_paths(paths)
|
||||
items = compress_import.plan_items(files, self._manager, music_dir)
|
||||
if not any(convert for _s, convert, _d in items):
|
||||
return False
|
||||
if not lossless.ffmpeg_available():
|
||||
QMessageBox.warning(
|
||||
self, "Compress Lossless Imports",
|
||||
"Compressing needs ffmpeg, which is not installed, so these "
|
||||
"songs are imported as they are.\n\nInstall your distro's "
|
||||
"“ffmpeg” package to compress the next import.")
|
||||
return False
|
||||
if self._busy_worker() is not None or self._compress is not None:
|
||||
self.statusBar().showMessage(
|
||||
"Wait for the current import or transfer to finish first", 6000)
|
||||
return True
|
||||
batch = compress_import.CompressImport(
|
||||
self._manager, music_dir, items, kbps, playlist_pid, position, self)
|
||||
batch.status.connect(self._on_compress_status)
|
||||
batch.landed.connect(self._compress_reload.start)
|
||||
batch.worker_finished.connect(self._free_sync_widgets)
|
||||
batch.over.connect(self._on_compress_over)
|
||||
self._compress = batch
|
||||
self._sync_label.setText("Importing…")
|
||||
self._sync_progress.setRange(0, 100)
|
||||
self._sync_progress.setValue(0)
|
||||
for widget in (self._sync_label, self._sync_progress, self._sync_cancel):
|
||||
widget.show()
|
||||
batch.start()
|
||||
return True
|
||||
|
||||
def _on_compress_status(self, label: str, value: int, maximum: int):
|
||||
self._sync_label.setText(label)
|
||||
self._sync_progress.setRange(0, maximum)
|
||||
self._sync_progress.setValue(value)
|
||||
|
||||
def _on_compress_over(self, message: str):
|
||||
self._compress = None
|
||||
self._free_sync_widgets() # mid-song, the worker's finish does it
|
||||
self._compress_reload.stop()
|
||||
self._library_view_reload()
|
||||
self.statusBar().showMessage(message, 8000)
|
||||
if not message.startswith("Import cancelled"):
|
||||
sounds.play_done(self._prefs)
|
||||
|
||||
def _free_sync_widgets(self):
|
||||
"""Hide the status-bar progress unless another transfer took it."""
|
||||
if self._busy_worker() is None:
|
||||
self._hide_sync_widgets()
|
||||
|
||||
def _show_compress_progress(self):
|
||||
if self._compress is not None:
|
||||
self._compress.show_window()
|
||||
|
||||
def _library_view_reload(self):
|
||||
self._library_view.reload()
|
||||
|
||||
def _import_files_to_playlist(self, paths, pid, position=None):
|
||||
self.import_files([Path(p) for p in paths], pid, position)
|
||||
|
||||
@@ -2907,7 +2989,10 @@ class MainWindow(QMainWindow):
|
||||
# A link import has nothing half-written on a device to warn about:
|
||||
# it is simply stopped below.
|
||||
busy = self._busy_worker()
|
||||
if busy is not None and busy not in (self._export_worker,
|
||||
if busy is not None and busy not in (
|
||||
self._compress.worker
|
||||
if self._compress else None,
|
||||
self._export_worker,
|
||||
self._url_worker,
|
||||
self._server_check,
|
||||
self._upload_worker):
|
||||
@@ -2929,6 +3014,8 @@ class MainWindow(QMainWindow):
|
||||
if self._url_worker is not None:
|
||||
self._url_worker.cancel()
|
||||
self._url_worker.cleanup()
|
||||
if self._compress is not None:
|
||||
self._compress.shutdown() # what landed stays imported
|
||||
for worker in (self._server_check, self._upload_worker):
|
||||
if worker is not None:
|
||||
worker.cancel() # kills ssh/rsync rather than orphan it
|
||||
|
||||
@@ -116,8 +116,8 @@ class PreferencesDialog(QDialog):
|
||||
self._color_save_timer.timeout.connect(self._prefs.save)
|
||||
|
||||
self._headings = []
|
||||
general = [self._build_playback_group(), self._build_sound_group(),
|
||||
self._build_wrapped_group()]
|
||||
general = [self._build_playback_group(), self._build_import_group(),
|
||||
self._build_sound_group(), self._build_wrapped_group()]
|
||||
if self._music_folder is not None:
|
||||
general.insert(0, self._build_music_group())
|
||||
self.pages_list = QListWidget()
|
||||
@@ -241,6 +241,48 @@ class PreferencesDialog(QDialog):
|
||||
|
||||
# ---- Sound ----
|
||||
|
||||
def _build_import_group(self) -> QGroupBox:
|
||||
"""Compress lossless imports (importers/lossless.py)."""
|
||||
from lintunes.importers import lossless
|
||||
group = QGroupBox("Importing")
|
||||
column = QVBoxLayout(group)
|
||||
row = QHBoxLayout()
|
||||
on, kbps = lossless.settings(self._prefs)
|
||||
self._compress_check = QCheckBox("Compress lossless imports to MP3 at")
|
||||
self._compress_check.setToolTip(
|
||||
"FLAC, WAV, AIFF and Apple Lossless files you add are converted to "
|
||||
"MP3, and the MP3 goes in your library. The file you added from "
|
||||
"isn't changed or moved.")
|
||||
self._compress_check.setChecked(on)
|
||||
self._compress_kbps = QComboBox()
|
||||
for rate in lossless.BITRATES:
|
||||
note = {320: " (best)", 256: " (recommended)", 128: " (smallest)"}
|
||||
self._compress_kbps.addItem(f"{rate} kbps{note.get(rate, '')}", rate)
|
||||
self._compress_kbps.setCurrentIndex(lossless.BITRATES.index(kbps))
|
||||
self._compress_kbps.setEnabled(on)
|
||||
self._compress_check.toggled.connect(self._on_compress_toggled)
|
||||
self._compress_kbps.currentIndexChanged.connect(
|
||||
lambda _i: self._prefs.set("import_compress_kbps",
|
||||
self._compress_kbps.currentData()))
|
||||
row.addWidget(self._compress_check)
|
||||
row.addWidget(self._compress_kbps)
|
||||
row.addStretch(1)
|
||||
column.addLayout(row)
|
||||
if lossless.ffmpeg_available():
|
||||
note = ("FLAC, WAV, AIFF and Apple Lossless files you add are "
|
||||
"converted, and the MP3 goes in your library — a fraction "
|
||||
"of the size. The file you added from isn't changed or "
|
||||
"moved. Songs already in your library are left as they are.")
|
||||
else:
|
||||
note = "Needs ffmpeg, which isn't installed."
|
||||
self._compress_check.setEnabled(False)
|
||||
column.addWidget(self._note(note))
|
||||
return group
|
||||
|
||||
def _on_compress_toggled(self, on: bool):
|
||||
self._compress_kbps.setEnabled(on)
|
||||
self._prefs.set("import_compress", on)
|
||||
|
||||
def _build_sound_group(self) -> QGroupBox:
|
||||
group = QGroupBox("Sound effects")
|
||||
row = QHBoxLayout(group)
|
||||
|
||||
@@ -101,6 +101,28 @@ def stage_file(source: Path, music_dir: Path,
|
||||
return dest, fields
|
||||
|
||||
|
||||
def import_converted(converted: Path, source: Path, music_dir: Path, manager,
|
||||
by_location: dict[Path, Track] | None = None) -> Track | None:
|
||||
"""File an MP3 that ``importers.lossless`` made from ``source`` and add it
|
||||
to the library. The MP3 is *moved* out of the worker's temp folder (it is
|
||||
ours); ``source`` is never touched. Named after the source, so it files
|
||||
like the original would have."""
|
||||
converted = Path(converted)
|
||||
fields = tagging.read_tags(converted)
|
||||
dest = organized_destination(music_dir, fields, Path(source).stem + ".mp3")
|
||||
if by_location is not None and by_location.get(dest) is not None:
|
||||
return by_location[dest]
|
||||
dest = unique_path(dest)
|
||||
dest.parent.mkdir(parents=True, exist_ok=True)
|
||||
shutil.move(str(converted), str(dest))
|
||||
fields["size"] = dest.stat().st_size
|
||||
track = track_from_file(dest, fields, Path(source).stem)
|
||||
manager.add_track(track)
|
||||
if by_location is not None:
|
||||
by_location[Path(track.location)] = track
|
||||
return track
|
||||
|
||||
|
||||
def track_from_file(dest: Path, fields: dict, fallback_name: str) -> Track:
|
||||
"""A new Track for a file just imported. Date added is *now* — every
|
||||
individual import is new to this library, whatever the file carries;
|
||||
@@ -121,11 +143,9 @@ def track_from_file(dest: Path, fields: dict, fallback_name: str) -> Track:
|
||||
return track
|
||||
|
||||
|
||||
def import_paths(paths: list[Path], music_dir: Path, manager,
|
||||
playlist_pid: str = "",
|
||||
position: int | None = None) -> list[Track]:
|
||||
"""Import files and directories (recursively); optionally add to a
|
||||
playlist, inserting at `position` (None = append)."""
|
||||
def expand_paths(paths) -> list[Path]:
|
||||
"""The audio files named by ``paths``, directories walked recursively,
|
||||
in the order an import adds them."""
|
||||
files: list[Path] = []
|
||||
for path in paths:
|
||||
path = Path(path)
|
||||
@@ -133,7 +153,15 @@ def import_paths(paths: list[Path], music_dir: Path, manager,
|
||||
files.extend(sorted(p for p in path.rglob("*") if p.is_file() and is_audio_file(p)))
|
||||
elif path.is_file() and is_audio_file(path):
|
||||
files.append(path)
|
||||
return files
|
||||
|
||||
|
||||
def import_paths(paths: list[Path], music_dir: Path, manager,
|
||||
playlist_pid: str = "",
|
||||
position: int | None = None) -> list[Track]:
|
||||
"""Import files and directories (recursively); optionally add to a
|
||||
playlist, inserting at `position` (None = append)."""
|
||||
files = expand_paths(paths)
|
||||
imported = []
|
||||
by_location = location_index(manager)
|
||||
for file_path in files:
|
||||
|
||||
@@ -0,0 +1,220 @@
|
||||
"""Compress lossless imports to MP3 (Round 106).
|
||||
|
||||
A friend's library was mostly FLAC and her disk was filling up. With the
|
||||
``import_compress`` pref on, a lossless file being imported (FLAC, WAV,
|
||||
AIFF, Apple Lossless) is converted by the ``ffmpeg`` CLI to an MP3 at the
|
||||
chosen bitrate and *that* is what goes into the library. The original is
|
||||
only ever read: it stays exactly where it was.
|
||||
|
||||
Shaped like ``bpm_detect.py``: pure helpers first (what counts as lossless,
|
||||
the command, ffmpeg's progress lines), then the conversion, then
|
||||
``CompressWorker`` on a daemon thread. The worker walks the *whole* import in
|
||||
order, lossy files included (they pass straight through), so a batch keeps
|
||||
its order in a playlist and Cancel can take every song of it back.
|
||||
|
||||
Tags and cover art are copied with mutagen after ffmpeg has run, through the
|
||||
same ``tagging`` code every other import reads, rather than trusting
|
||||
ffmpeg's metadata mapping — WAV and AIFF tags and ALAC's atoms map
|
||||
unevenly, and the art must arrive as an ID3 front cover.
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
import logging
|
||||
import shutil
|
||||
import subprocess
|
||||
import tempfile
|
||||
import threading
|
||||
from pathlib import Path
|
||||
|
||||
from PyQt6.QtCore import QObject, pyqtSignal
|
||||
|
||||
from lintunes import tagging
|
||||
|
||||
log = logging.getLogger(__name__)
|
||||
|
||||
BITRATES = (320, 256, 192, 160, 128) # kbps, best first
|
||||
DEFAULT_BITRATE = 256
|
||||
LOSSLESS_SUFFIXES = {".flac", ".wav", ".aiff", ".aif"}
|
||||
|
||||
|
||||
def ffmpeg_available() -> bool:
|
||||
return shutil.which("ffmpeg") is not None
|
||||
|
||||
|
||||
def settings(prefs) -> tuple[bool, int]:
|
||||
"""(on, kbps) from preferences; an unknown bitrate falls back to the
|
||||
default rather than handing ffmpeg something odd."""
|
||||
on = bool(prefs.get("import_compress", False))
|
||||
kbps = prefs.get("import_compress_kbps", DEFAULT_BITRATE)
|
||||
return on, kbps if kbps in BITRATES else DEFAULT_BITRATE
|
||||
|
||||
|
||||
def is_lossless(path: Path) -> bool:
|
||||
"""FLAC/WAV/AIFF by suffix; an .m4a only when it holds Apple Lossless
|
||||
(the same container holds plain AAC, which must be left alone)."""
|
||||
suffix = Path(path).suffix.lower()
|
||||
if suffix in LOSSLESS_SUFFIXES:
|
||||
return True
|
||||
if suffix in (".m4a", ".mp4"):
|
||||
try:
|
||||
from mutagen.mp4 import MP4
|
||||
return str(MP4(str(path)).info.codec).startswith("alac")
|
||||
except Exception:
|
||||
return False
|
||||
return False
|
||||
|
||||
|
||||
def ffmpeg_command(src: Path, dest: Path, kbps: int) -> list[str]:
|
||||
"""Audio only (``-vn``: art is copied afterwards), LAME CBR at ``kbps``,
|
||||
ID3v2.3 so every player reads it, and machine-readable progress on
|
||||
stdout."""
|
||||
return ["ffmpeg", "-nostdin", "-hide_banner", "-loglevel", "error",
|
||||
"-nostats", "-progress", "pipe:1", "-y", "-i", str(src),
|
||||
"-vn", "-map", "0:a:0", "-c:a", "libmp3lame", "-b:a", f"{kbps}k",
|
||||
"-id3v2_version", "3", str(dest)]
|
||||
|
||||
|
||||
def parse_progress(line: str, duration_ms: int) -> int | None:
|
||||
"""Percent done from one ``-progress`` line, or None if it says nothing.
|
||||
``out_time_us`` is the position reached (``out_time_ms`` is microseconds
|
||||
too, despite its name, and is not used)."""
|
||||
key, _, value = line.strip().partition("=")
|
||||
if key == "progress" and value == "end":
|
||||
return 100
|
||||
if key != "out_time_us" or duration_ms <= 0:
|
||||
return None
|
||||
try:
|
||||
us = int(value)
|
||||
except ValueError:
|
||||
return None
|
||||
return max(0, min(99, us // 10 // duration_ms))
|
||||
|
||||
|
||||
def _image_mime(data: bytes) -> str:
|
||||
return "image/png" if data.startswith(b"\x89PNG") else "image/jpeg"
|
||||
|
||||
|
||||
def copy_tags(src: Path, dest: Path):
|
||||
"""The source's tags and cover art onto the converted file. Best effort:
|
||||
a source whose tags mutagen can't read still converts."""
|
||||
try:
|
||||
fields = tagging.read_tags(src)
|
||||
tagging.write_tags(dest, {k: v for k, v in fields.items()
|
||||
if k in tagging._TEXT_FIELDS or k in (
|
||||
"year", "track_number", "track_count",
|
||||
"disc_number", "disc_count", "bpm",
|
||||
"compilation")})
|
||||
except Exception as e:
|
||||
log.warning("Couldn't copy tags from %s: %s", src, e)
|
||||
try:
|
||||
art = tagging.read_embedded_artwork(src)
|
||||
if art:
|
||||
tagging.write_artwork(dest, art, _image_mime(art))
|
||||
except Exception as e:
|
||||
log.warning("Couldn't copy cover art from %s: %s", src, e)
|
||||
|
||||
|
||||
def convert(src: Path, dest: Path, kbps: int, duration_ms: int = 0,
|
||||
on_progress=None, cancel: threading.Event | None = None) -> bool:
|
||||
"""Convert ``src`` to an MP3 at ``dest``. True when done, False when
|
||||
cancelled (``dest`` removed); raises OSError when ffmpeg fails."""
|
||||
try:
|
||||
proc = subprocess.Popen(
|
||||
ffmpeg_command(src, dest, kbps), stdin=subprocess.DEVNULL,
|
||||
stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)
|
||||
except (OSError, ValueError) as e:
|
||||
raise OSError(f"couldn't run ffmpeg: {e}") from e
|
||||
# stderr is drained on its own thread so a chatty failure can't fill
|
||||
# the pipe and stall ffmpeg while we read stdout.
|
||||
errors: list[str] = []
|
||||
drain = threading.Thread(target=lambda: errors.extend(proc.stderr),
|
||||
daemon=True)
|
||||
drain.start()
|
||||
cancelled = False
|
||||
for line in proc.stdout:
|
||||
if cancel is not None and cancel.is_set():
|
||||
proc.kill()
|
||||
cancelled = True
|
||||
break
|
||||
percent = parse_progress(line, duration_ms)
|
||||
if percent is not None and on_progress is not None:
|
||||
on_progress(percent)
|
||||
proc.wait()
|
||||
drain.join(timeout=2)
|
||||
if cancelled:
|
||||
dest.unlink(missing_ok=True)
|
||||
return False
|
||||
if proc.returncode != 0:
|
||||
dest.unlink(missing_ok=True)
|
||||
detail = [e.strip() for e in errors if e.strip()]
|
||||
raise OSError(f"ffmpeg failed on {src.name}"
|
||||
+ (f": {detail[-1]}" if detail else ""))
|
||||
copy_tags(src, dest)
|
||||
return True
|
||||
|
||||
|
||||
class CompressWorker(QObject):
|
||||
"""Walks an import in order on a daemon thread: a lossless item is
|
||||
converted into a private temp folder, anything else passes straight
|
||||
through. Each item is reported as it's ready, so the GUI imports it at
|
||||
once; the GUI deletes ``tmp_dir`` once ``finished`` has arrived (it is
|
||||
queued after every ``ready``).
|
||||
|
||||
``items`` are ``(source path, convert?, duration ms)`` snapshots — the
|
||||
worker never reads the library."""
|
||||
|
||||
started = pyqtSignal(int) # item index
|
||||
progress = pyqtSignal(int, int) # item index, percent
|
||||
ready = pyqtSignal(int, str, bool) # item index, file to import, converted
|
||||
failed = pyqtSignal(int, str) # item index, reason
|
||||
finished = pyqtSignal(dict) # {"cancelled": bool}
|
||||
|
||||
def __init__(self, items: list[tuple[Path, bool, int]], kbps: int,
|
||||
parent=None, convert_fn=None):
|
||||
super().__init__(parent)
|
||||
self._items = list(items)
|
||||
self._kbps = kbps
|
||||
self._convert = convert_fn or convert
|
||||
self._busy = False
|
||||
self._cancel = threading.Event()
|
||||
self.tmp_dir = Path(tempfile.mkdtemp(prefix="lintunes-compress-"))
|
||||
|
||||
def busy(self) -> bool:
|
||||
return self._busy
|
||||
|
||||
def cancel(self):
|
||||
self._cancel.set()
|
||||
|
||||
def cancelled(self) -> bool:
|
||||
return self._cancel.is_set()
|
||||
|
||||
def start(self):
|
||||
if self._busy:
|
||||
return
|
||||
self._busy = True
|
||||
threading.Thread(target=self._run, daemon=True).start()
|
||||
|
||||
def _run(self):
|
||||
try:
|
||||
for index, (source, lossless, duration_ms) in enumerate(self._items):
|
||||
if self._cancel.is_set():
|
||||
break
|
||||
self.started.emit(index)
|
||||
if not lossless:
|
||||
self.ready.emit(index, str(source), False)
|
||||
continue
|
||||
dest = self.tmp_dir / f"{index:05d}" / (Path(source).stem + ".mp3")
|
||||
dest.parent.mkdir(parents=True, exist_ok=True)
|
||||
try:
|
||||
done = self._convert(
|
||||
Path(source), dest, self._kbps, duration_ms,
|
||||
lambda p, i=index: self.progress.emit(i, p),
|
||||
self._cancel)
|
||||
except Exception as e:
|
||||
self.failed.emit(index, str(e))
|
||||
continue
|
||||
if done:
|
||||
self.ready.emit(index, str(dest), True)
|
||||
finally:
|
||||
self._busy = False
|
||||
self.finished.emit({"cancelled": self._cancel.is_set()})
|
||||
@@ -303,7 +303,11 @@ class LibraryManager(QObject):
|
||||
# ---- playlist contents ----
|
||||
|
||||
def add_tracks_to_playlist(self, pid: str, track_ids: list[int],
|
||||
position: int | None = None):
|
||||
position: int | None = None,
|
||||
undoable: bool = True):
|
||||
"""``undoable=False`` is for a compressing import, whose Cancel is
|
||||
its undo: an undo step left behind would put back ids Cancel
|
||||
removed."""
|
||||
playlist = self.library.playlists.get(pid)
|
||||
if not playlist or playlist.playlist_type == PlaylistType.FOLDER \
|
||||
or playlist.is_smart:
|
||||
@@ -318,7 +322,8 @@ class LibraryManager(QObject):
|
||||
else:
|
||||
after[position:position] = track_ids
|
||||
self._set_track_ids(pid, after)
|
||||
self._push_track_ids("Add to Playlist", pid, before, after)
|
||||
if undoable:
|
||||
self._push_track_ids("Add to Playlist", pid, before, after)
|
||||
|
||||
def remove_tracks_from_playlist(self, pid: str, rows: list[int]):
|
||||
playlist = self.library.playlists.get(pid)
|
||||
@@ -471,6 +476,30 @@ class LibraryManager(QObject):
|
||||
undo=undo, redo=redo))
|
||||
return [t.track_id for t in tracks], failures
|
||||
|
||||
def discard_imports(self, track_ids: list[int], trash_files: set[int]):
|
||||
"""Take back tracks an import just added (Cancel on a compressing
|
||||
import). Files of the ids in ``trash_files`` — the copies and MP3s
|
||||
that import made — go to the desktop trash; any other file (one
|
||||
imported by reference from inside the music folder) is left alone.
|
||||
Not an undo step: adding them wasn't one either. Returns the
|
||||
``(name, error)`` of files that couldn't be trashed; those tracks
|
||||
are removed from the library anyway, since the file is a copy."""
|
||||
tracks = [self.library.tracks[tid] for tid in dict.fromkeys(track_ids)
|
||||
if tid in self.library.tracks]
|
||||
failures = []
|
||||
for track in tracks:
|
||||
if track.track_id not in trash_files:
|
||||
continue
|
||||
try:
|
||||
self._trash_file(track)
|
||||
except trash.TrashError as e:
|
||||
log.warning("Could not trash %s: %s", track.location, e)
|
||||
failures.append((track.name or str(track.location), str(e)))
|
||||
if tracks:
|
||||
self._remove_tracks(
|
||||
tracks, self._playlist_snapshots([t.track_id for t in tracks]))
|
||||
return failures
|
||||
|
||||
def _trash_file(self, track) -> Path:
|
||||
"""Send a track's file to the trash and tidy up the folders it emptied."""
|
||||
if not track.location:
|
||||
|
||||
@@ -87,6 +87,10 @@ DEFAULTS = {
|
||||
# Which machine runs friend sharing (Cassette): {machine_id, hostname}.
|
||||
# Synced so the other machine can name it; see lintunes/cassette/host.py.
|
||||
"cassette_host": None,
|
||||
# Importing: convert FLAC/WAV/AIFF/ALAC to an MP3 at this bitrate
|
||||
# (importers/lossless.py). The original file is never touched.
|
||||
"import_compress": False,
|
||||
"import_compress_kbps": 256,
|
||||
# The quiet "tink" when a sync, export or URL import finishes.
|
||||
"sound_effects": True,
|
||||
# The yearly Wrapped (storage/listen_history.py). Synced on purpose: one
|
||||
|
||||
Reference in New Issue
Block a user