Helper to import listens of a music player app to ListenBrainz.
Written for Muzio Player, but can maybe be adapted for other players.
I backed up the player's data via adb (Android debug bridge).
adb backup com.shaiban.audioplayer.mplayer
and set a password for the backup and remebered it.
i used the Android Backup Processor to transform the backup to a .tar file and extracted it.
the Backup for muzio player contained various SQLite database files, among them the file history.db
.
Exploring the file via DB Browser for SQLite revealed a table mapping song IDs to timestamps in millisecond scale Unix Timestamps.
But where are the paths to the audio files???
I could only find a mapping between song IDs and paths inside another DB file: muzio.db
.
There the table playlist_song
mapping songs to their playlists contained the actual paths to the song files.
This means: to import all listens, you seem to need to create a playlist in Muzio and add all songs on your device to it before backing up.
i used the following query after opening the main database (muzio.db) and attaching the history database (history.db):
select recent_history.song_id, substr(data, 27) as path, time_played from history.recent_history
left join playlist_song on recent_history.song_id = playlist_song.song_id
the substring is for removing the /storage/emulated/0/Music/
form the path, where all my music files are on my phone.
I simply exported the data from SQLite Studio by marking the complete result output, pressed 'copy with headers' and pasted to a text file.
You now have a tab-separated file of your listening history.
This is where this python file comes into play.
It reads the saved data file and tries the following:
- if the file is found, metadata is tried to be read: artist name, track title, artists MBIDs and track MBID if available
- if file is not found, could not be read or no tags were found in the file, prompt the user and try to prefill the data from the file name.
To use, you need Python 3 with pandas, pylistenbrainz, audio-metadata pip packages installed.
Before usage, adapt at least the code lines marked with TODO:
comments.
It may be necessary to adapt further, e.g. if you use it for another music player or your file names have a different format to adapt guessing artist names and track titles from the filename.