diff --git a/CLAUDE.md b/CLAUDE.md index ca82644..327a8b2 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -581,6 +581,27 @@ persistence) → GUI (Qt widgets that read the manager and connect to its signal and hand over nothing. Every paste attempt is logged at INFO (`lintunes.gui.info_dialog`), so it shows up in the journal. +- **`lintunes/importers/lossless.py`** + **`gui/compress_import.py`** — + Preferences › General › Importing, "Compress lossless imports to MP3" + (Round 106, a friend's request; **off by default**, pref + `import_compress` + `import_compress_kbps` ∈ 320/256/192/160/128). With it + on and a lossless file (FLAC/WAV/AIFF by suffix, `.m4a` only when its + codec is ALAC) in an import, `MainWindow.import_files` hands the **whole** + batch to `CompressImport` and returns `[]`: `CompressWorker` walks it in + order on a daemon thread (lossy files pass straight through, so a + playlist drop keeps its order), the `ffmpeg` CLI converts into a private + temp dir, tags and art are copied with mutagen (`copy_tags`, not + ffmpeg's mapping), and `file_importer.import_converted` *moves* the MP3 + into the tree under the source's stem. **The source is only ever read.** + One file → the status-bar progress; more → `CompressProgress` (a bar per + song, Cancel/Done; closing while working only hides it). **Cancel takes + back every song the batch added, finished ones too** + (`LibraryManager.discard_imports`: the copies it made go to the trash, a + file imported in place from the music folder is never trashed). So the + batch's playlist inserts use `add_tracks_to_playlist(undoable=False)` — + an undo step left behind would put back ids Cancel removed. A lossless + file already in the library isn't converted again. + - **`lintunes/fingerprint.py`** — "Identify Track…": Chromaprint's `fpcalc` CLI (detected with `shutil.which`, the `ffmpeg_available()` pattern — a runtime tool, never a pip dep) fingerprints the file, and AcoustID's web API diff --git a/lintunes/__init__.py b/lintunes/__init__.py index aa567c7..4fee84d 100644 --- a/lintunes/__init__.py +++ b/lintunes/__init__.py @@ -1,3 +1,3 @@ """LinTunes — iTunes-style music library manager and player for Linux.""" -__version__ = "0.74.1" +__version__ = "0.75.0" diff --git a/lintunes/gui/compress_import.py b/lintunes/gui/compress_import.py new file mode 100644 index 0000000..85b950a --- /dev/null +++ b/lintunes/gui/compress_import.py @@ -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) diff --git a/lintunes/gui/main_window.py b/lintunes/gui/main_window.py index eef2f50..6246bc3 100644 --- a/lintunes/gui/main_window.py +++ b/lintunes/gui/main_window.py @@ -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 diff --git a/lintunes/gui/preferences_dialog.py b/lintunes/gui/preferences_dialog.py index d7ab031..1dc15fc 100644 --- a/lintunes/gui/preferences_dialog.py +++ b/lintunes/gui/preferences_dialog.py @@ -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) diff --git a/lintunes/importers/file_importer.py b/lintunes/importers/file_importer.py index f2299eb..b7aed10 100644 --- a/lintunes/importers/file_importer.py +++ b/lintunes/importers/file_importer.py @@ -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: diff --git a/lintunes/importers/lossless.py b/lintunes/importers/lossless.py new file mode 100644 index 0000000..d3a6702 --- /dev/null +++ b/lintunes/importers/lossless.py @@ -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()}) diff --git a/lintunes/library_manager.py b/lintunes/library_manager.py index 2608be7..d06ab00 100644 --- a/lintunes/library_manager.py +++ b/lintunes/library_manager.py @@ -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: diff --git a/lintunes/preferences.py b/lintunes/preferences.py index d394329..ef0ad44 100644 --- a/lintunes/preferences.py +++ b/lintunes/preferences.py @@ -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 diff --git a/tests/test_round106.py b/tests/test_round106.py new file mode 100644 index 0000000..739fb86 --- /dev/null +++ b/tests/test_round106.py @@ -0,0 +1,284 @@ +"""Round 106: Compress lossless imports to MP3 (importers/lossless.py, +gui/compress_import.py).""" +import hashlib +import shutil +import subprocess +import time +from pathlib import Path + +import pytest + +from lintunes import tagging +from lintunes.importers import lossless +from lintunes.library_manager import LibraryManager +from lintunes.models import Library, Playlist +from lintunes.preferences import Preferences +from tests.conftest import _generate_audio + + +@pytest.fixture +def isolated_config(tmp_path, monkeypatch): + monkeypatch.setenv("XDG_CONFIG_HOME", str(tmp_path / "config")) + return tmp_path / "config" + + +@pytest.fixture +def fake_trash(tmp_path, monkeypatch): + """Cancel trashes the copies it made; never into the real trash.""" + bin_ = tmp_path / "trash-bin" + bin_.mkdir() + + def send(path): + dest = bin_ / f"{len(list(bin_.iterdir()))}-{Path(path).name}" + shutil.move(str(path), dest) + return dest + monkeypatch.setattr("lintunes.library_manager.trash.send_to_trash", send) + return bin_ + + +def _digest(path: Path) -> str: + return hashlib.sha256(path.read_bytes()).hexdigest() + + +def _tagged_flac(path: Path, title: str, art: bytes | None = None, + seconds: int = 1) -> Path: + if shutil.which("ffmpeg") is None: + pytest.skip("ffmpeg not available") + subprocess.run(["ffmpeg", "-y", "-f", "lavfi", "-i", + f"anoisesrc=d={seconds}:a=0.3", "-ac", "2", "-c:a", "flac", + str(path)], capture_output=True, check=True) + tagging.write_tags(path, {"name": title, "artist": "Nissa", + "album": "Big Files", "year": 2019, + "track_number": 3, "track_count": 9}) + if art: + tagging.write_artwork(path, art) + return path + + +def _manager(tmp_path, pid_tracks=None): + (tmp_path / "media").mkdir(exist_ok=True) # or the window asks for one + library = Library(music_folder=str(tmp_path / "media")) + if pid_tracks is not None: + library.playlists["P"] = Playlist(name="Mix", persistent_id="P", + track_ids=list(pid_tracks)) + return LibraryManager(library, tmp_path / "data") + + +def _wait(qapp, batch, seconds=20): + deadline = time.time() + seconds + while batch.worker.busy() and time.time() < deadline: + qapp.processEvents() + time.sleep(0.01) + for _ in range(5): + qapp.processEvents() + + +# ---- pure parts ---- + +def test_off_by_default(tmp_path): + prefs = Preferences(tmp_path / "data") + assert lossless.settings(prefs) == (False, 256) + prefs.set("import_compress_kbps", 999) + assert lossless.settings(prefs)[1] == lossless.DEFAULT_BITRATE + + +def test_the_five_bitrates(): + assert lossless.BITRATES == (320, 256, 192, 160, 128) + + +def test_what_counts_as_lossless(tmp_path, mp3_file, m4a_file, flac_file): + assert lossless.is_lossless(flac_file) + assert not lossless.is_lossless(mp3_file) + assert not lossless.is_lossless(m4a_file) # plain AAC + alac = _generate_audio(tmp_path / "alac.m4a", ["-c:a", "alac"]) + assert lossless.is_lossless(alac) + for name in ("a.wav", "a.aiff", "a.AIF"): + assert lossless.is_lossless(Path(name)) + + +def test_progress_lines(): + assert lossless.parse_progress("out_time_us=5000000", 10_000) == 50 + assert lossless.parse_progress("out_time_us=99000000", 10_000) == 99 + assert lossless.parse_progress("progress=end", 10_000) == 100 + assert lossless.parse_progress("out_time_us=N/A", 10_000) is None + assert lossless.parse_progress("bitrate=1.0kbits/s", 10_000) is None + assert lossless.parse_progress("out_time_us=5", 0) is None + + +# ---- the conversion, through the real ffmpeg ---- + +def test_convert_keeps_tags_and_art_and_never_touches_the_original( + tmp_path, jpeg_bytes): + src = _tagged_flac(tmp_path / "song.flac", "Huge", jpeg_bytes, seconds=3) + before = _digest(src) + dest = tmp_path / "out" / "song.mp3" + dest.parent.mkdir() + seen = [] + assert lossless.convert(src, dest, 128, 3000, seen.append) + assert _digest(src) == before + fields = tagging.read_tags(dest) + assert fields["kind"] == "MPEG audio file" + assert fields["name"] == "Huge" and fields["artist"] == "Nissa" + assert fields["album"] == "Big Files" and fields["year"] == 2019 + assert (fields["track_number"], fields["track_count"]) == (3, 9) + assert 120 <= fields["bit_rate"] <= 136 + assert tagging.read_embedded_artwork(dest) == jpeg_bytes + assert seen and seen[-1] == 100 + assert dest.stat().st_size < src.stat().st_size + + +def test_a_failed_conversion_says_why_and_leaves_nothing(tmp_path): + bad = tmp_path / "broken.flac" + bad.write_bytes(b"not audio at all") + dest = tmp_path / "broken.mp3" + with pytest.raises(OSError, match="ffmpeg failed on broken.flac"): + lossless.convert(bad, dest, 256) + assert not dest.exists() + + +# ---- a batch ---- + +def _batch(qapp, tmp_path, files, pid="", position=None, manager=None): + from lintunes.gui.compress_import import CompressImport, plan_items + manager = manager or _manager(tmp_path) + music = tmp_path / "media" / "Music" + items = plan_items(files, manager, music) + batch = CompressImport(manager, music, items, 192, pid, position) + return manager, batch + + +def test_a_batch_imports_in_order_into_the_playlist(qapp, tmp_path, mp3_file): + a = _tagged_flac(tmp_path / "a.flac", "Alpha") + b = _tagged_flac(tmp_path / "b.flac", "Bravo") + sums = {a: _digest(a), b: _digest(b)} + manager = _manager(tmp_path) + old = manager.add_track(tagging_track(tmp_path)) + manager.library.playlists["P"] = Playlist(name="Mix", persistent_id="P", + track_ids=[old, old]) + manager, batch = _batch(qapp, tmp_path, [a, mp3_file, b], "P", 1, manager) + assert batch.window is not None + over = [] + batch.over.connect(over.append) + batch.start() + _wait(qapp, batch) + + playlist = manager.library.playlists["P"] + names = [manager.library.tracks[t].name for t in playlist.track_ids] + assert names == ["Old", "Alpha", "test", "Bravo", "Old"] + alpha = manager.library.tracks[playlist.track_ids[1]] + assert alpha.location.endswith(".mp3") and "media/Music/Nissa" in alpha.location + assert alpha.kind == "MPEG audio file" + assert all(_digest(p) == d for p, d in sums.items()) + assert batch.window.done_button.isEnabled() + assert not batch.worker.tmp_dir.exists() + # Not undoable piecemeal: Cancel is this import's undo. + assert not manager.undo_stack.can_undo() + batch.window.done_button.click() + assert over and "3 songs" in over[0] and "2 converted to 192 kbps" in over[0] + + +def tagging_track(tmp_path): + from lintunes.models import Track + return Track(name="Old", location=str(tmp_path / "old.mp3")) + + +def test_cancel_after_finishing_takes_every_song_back( + qapp, tmp_path, mp3_file, fake_trash): + a = _tagged_flac(tmp_path / "a.flac", "Alpha") + a_sum = _digest(a) + manager, batch = _batch(qapp, tmp_path, [a, mp3_file], "P", + manager=_manager(tmp_path, pid_tracks=[])) + batch.start() + _wait(qapp, batch) + assert len(manager.library.tracks) == 2 + batch.request_cancel(confirm=False) + assert manager.library.tracks == {} + assert manager.library.playlists["P"].track_ids == [] + assert list((tmp_path / "media").rglob("*.mp3")) == [] + assert len(list(fake_trash.iterdir())) == 2 # copies, not originals + assert _digest(a) == a_sum and mp3_file.exists() + + +def test_cancel_never_trashes_a_file_imported_in_place(qapp, tmp_path, fake_trash): + music = tmp_path / "media" / "Music" + music.mkdir(parents=True) + in_place = _generate_audio(music / "already.mp3", + ["-codec:a", "libmp3lame", "-b:a", "64k"]) + a = _tagged_flac(tmp_path / "a.flac", "Alpha") + manager, batch = _batch(qapp, tmp_path, [in_place, a]) + batch.start() + _wait(qapp, batch) + batch.request_cancel(confirm=False) + assert manager.library.tracks == {} + assert in_place.exists() + assert len(list(fake_trash.iterdir())) == 1 + + +def test_one_song_has_no_window(qapp, tmp_path): + a = _tagged_flac(tmp_path / "a.flac", "Alpha") + manager, batch = _batch(qapp, tmp_path, [a]) + assert batch.window is None + statuses, over = [], [] + batch.status.connect(lambda *s: statuses.append(s)) + batch.over.connect(over.append) + batch.start() + _wait(qapp, batch) + assert statuses[0][0] == "Converting a.flac" + assert over == ["Imported 1 song (1 converted to 192 kbps MP3)"] + + +def test_a_lossless_file_already_in_the_library_is_not_converted_again( + qapp, tmp_path): + from lintunes.gui.compress_import import plan_items + from lintunes.importers import file_importer + music = tmp_path / "media" / "Music" + music.mkdir(parents=True) + kept = _tagged_flac(music / "kept.flac", "Kept") + manager = _manager(tmp_path) + file_importer.import_paths([kept], music, manager) + [(source, convert, _)] = plan_items([kept], manager, music) + assert not convert + + +# ---- the window ---- + +def test_import_files_hands_lossless_files_to_the_converter( + qapp, tmp_path, isolated_config, mp3_file): + from lintunes.gui.main_window import MainWindow + a = _tagged_flac(tmp_path / "a.flac", "Alpha") + manager = _manager(tmp_path) + prefs = Preferences(tmp_path / "data") + window = MainWindow(manager, prefs) + try: + # Off: the old synchronous import, FLAC as it is. + [track] = window.import_files([a]) + assert track.location.endswith(".flac") + manager.delete_tracks([track.track_id]) + # On, but nothing lossless: unchanged too. + prefs.set("import_compress", True) + assert len(window.import_files([mp3_file])) == 1 + # On, with a FLAC: in the background, into the status bar. + assert window.import_files([tmp_path / "a.flac"]) == [] + batch = window._compress + assert batch is not None and window._busy_worker() is batch.worker + assert window._sync_label.isVisibleTo(window) + _wait(qapp, batch) + assert window._compress is None + assert not window._sync_label.isVisibleTo(window) + assert any(t.location.endswith("a.mp3") + for t in manager.library.tracks.values()) + finally: + window.close() + + +def test_preferences_has_the_setting(qapp, tmp_path): + from unittest.mock import MagicMock + from lintunes.gui.preferences_dialog import PreferencesDialog + prefs = Preferences(tmp_path / "data") + dialog = PreferencesDialog(prefs, MagicMock()) + assert not dialog._compress_check.isChecked() + assert not dialog._compress_kbps.isEnabled() + dialog._compress_check.setChecked(True) + assert prefs.get("import_compress") is True + dialog._compress_kbps.setCurrentIndex(lossless.BITRATES.index(160)) + assert prefs.get("import_compress_kbps") == 160