forked from trav/lintunes
v0.76.0: Convert to MP3 for songs already in the library, and for Cassette arrivals
- right-click › Convert to MP3…: lossless songs become MP3s at a chosen bitrate; same tracks (plays, ratings, playlists kept), originals to the trash, one undo step brings them back; Cancel changes nothing - Preferences › Importing › Convert songs from Cassette friends too - the bitrate list is just the numbers - conversions stage in ~/.cache, not /tmp
This commit is contained in:
@@ -688,6 +688,90 @@ class LibraryManager(QObject):
|
||||
redo=lambda: [self._revert_track_fields(tid, new)
|
||||
for tid, new, _old in changes]))
|
||||
|
||||
_FILE_FIELDS = ("location", "kind", "bit_rate", "sample_rate", "size",
|
||||
"total_time")
|
||||
|
||||
def replace_track_files(self, changes: list[tuple[int, Path]]) -> list:
|
||||
"""Convert to MP3 (Round 107): each track now plays ``new_path``, a
|
||||
converted copy of its file, and the old file goes to the desktop
|
||||
trash. Same track — play counts, playlists and ratings don't move.
|
||||
The library's own tags are written into the new file, since it is
|
||||
the source of truth. ONE undoable command for the batch: undo brings
|
||||
the originals back from the trash and trashes the copies.
|
||||
|
||||
Returns ``(name, error)`` per track left as it was (its new file is
|
||||
trashed again, so nothing is left lying around)."""
|
||||
records = [] # [track, {"old": fields, "new": fields, "trashed": path}]
|
||||
failures = []
|
||||
for track_id, new_path in changes:
|
||||
track = self.library.tracks.get(track_id)
|
||||
if track is None or not track.location:
|
||||
continue
|
||||
new_path = Path(new_path)
|
||||
writable = {k: getattr(track, k) for k in tagging.EDITABLE_FIELDS
|
||||
if hasattr(track, k)}
|
||||
try:
|
||||
tagging.write_tags(new_path, writable)
|
||||
except Exception as e:
|
||||
log.warning("Couldn't write tags to %s: %s", new_path, e)
|
||||
fields = tagging.read_tags(new_path)
|
||||
new = {"location": str(new_path),
|
||||
**{k: fields[k] for k in self._FILE_FIELDS[1:] if k in fields}}
|
||||
old = {k: getattr(track, k) for k in new}
|
||||
try:
|
||||
trashed = trash.send_to_trash(Path(track.location))
|
||||
except trash.TrashError as e:
|
||||
log.warning("Could not trash %s: %s", track.location, e)
|
||||
failures.append((track.name or track.location, str(e)))
|
||||
try:
|
||||
trash.send_to_trash(new_path)
|
||||
except trash.TrashError:
|
||||
pass
|
||||
continue
|
||||
records.append([track, {"old": old, "new": new, "trashed": trashed,
|
||||
"trashed_new": None}])
|
||||
self._apply_track_fields(track_id, new)
|
||||
if not records:
|
||||
return failures
|
||||
|
||||
def swap(track, r, put_away: str, bring_back: str) -> bool:
|
||||
"""Trash the file the track plays now (``put_away`` side) and
|
||||
restore the other one; on failure, leave both as they were."""
|
||||
here = Path(r[put_away]["location"])
|
||||
stash = "trashed" if bring_back == "old" else "trashed_new"
|
||||
other = "trashed_new" if stash == "trashed" else "trashed"
|
||||
if r[stash] is None:
|
||||
return False
|
||||
try:
|
||||
r[other] = trash.send_to_trash(here)
|
||||
except trash.TrashError as e:
|
||||
self.file_move_failed.emit(track.name or "", str(e))
|
||||
return False
|
||||
try:
|
||||
trash.restore_from_trash(r[stash], Path(r[bring_back]["location"]))
|
||||
except trash.TrashError as e:
|
||||
self.file_move_failed.emit(track.name or "", str(e))
|
||||
try:
|
||||
trash.restore_from_trash(r[other], here)
|
||||
except trash.TrashError:
|
||||
log.warning("%s is left in the trash", here)
|
||||
r[other] = None
|
||||
return False
|
||||
r[stash] = None
|
||||
self._apply_track_fields(track.track_id, r[bring_back])
|
||||
return True
|
||||
|
||||
def undo():
|
||||
for track, r in records:
|
||||
swap(track, r, "new", "old")
|
||||
|
||||
def redo():
|
||||
for track, r in records:
|
||||
swap(track, r, "old", "new")
|
||||
|
||||
self.undo_stack.push(Command("Convert to MP3", undo=undo, redo=redo))
|
||||
return failures
|
||||
|
||||
def _write_track_tags(self, track, field_map: dict) -> bool:
|
||||
# Library-only fields (rating, size, start/stop times) have no tag
|
||||
# representation; don't rewrite the audio file for them.
|
||||
|
||||
Reference in New Issue
Block a user