librespot icon indicating copy to clipboard operation
librespot copied to clipboard

feat: Basic local file support

Open SapiensAnatis opened this issue 3 months ago • 5 comments

Discussed in #1525. Adds a basic level of support for playing back local files from a connect device. The librespot binary must be launched with --local-file-dir / -l flags specifying search paths for local files. If a request is received to play a local file, and a file match is made based on the file's artist, album, track title, and duration, then librespot will now be able to play it.

This support has the following caveats:

  • Unlike the official client, we cannot play local files if they have a sample rate != 44,100 Hz. This is the sample rate used by the player - without adjusting it, playback of local files with differing sample rates will sound either slowed or sped up. Adjusting the sample rate of the player on the fly is a non-trivial task which may be implemented later.
  • The dedicated 'Local Files' playlist cannot be used to play a local file. This is a special playlist with a URI of spotify:local-files which currently causes an error in the context handling code, as librespot attempts to fetch it from the API and appears to get a 404. I tried to make this work, but it feels like an abstraction for context would be beneficial so we could generate context from the filesystem (currently I think we use the proto models which are obviously tightly coupled to the Spotify API). Also, I do not understand enough about the context handling code to do a good job :sweat_smile:

SapiensAnatis avatar Sep 22 '25 20:09 SapiensAnatis

Wait, if the tracks that will be found in the path added by --local-file-dir are actually tracks from the LocalFIles playlist, then in the future this could be improved by handling local-files/watch-sources directly from the files of spotify itself, I think that would be a nice addition.

SuisChan avatar Oct 12 '25 13:10 SuisChan

Wait, if the tracks that will be found in the path added by --local-file-dir are actually tracks from the LocalFIles playlist, then in the future this could be improved by handling local-files/watch-sources directly from the files of spotify itself, I think that would be a nice addition.

Each device will have its own Local Files playlist based on what folders have been configured. I don't think we can get a list of sources from Spotify since a connect play request could come from an entirely different device. Even if there is file path information in the request, it may not be valid on the current device.

In terms of live-updating the local file content, maybe we could use something like https://github.com/notify-rs/notify to update the lookup in the background, or only update it if we get a request to play a file we fail to find a path for.

SapiensAnatis avatar Oct 12 '25 15:10 SapiensAnatis

@roderickvd @photovoltex thanks for all the reviews so far, I was just wondering if you had any more feedback?

I may make a start on adding some of the extra functionality in subsequent PRs, namely lifting the sample rate restriction and making the local files playlist work. Would you like me to create issues for these tasks?

SapiensAnatis avatar Oct 25 '25 16:10 SapiensAnatis

I have squashed all my smaller commits so I didn't have to fix merge conflicts with the changelog across multiple commits.

SapiensAnatis avatar Nov 09 '25 13:11 SapiensAnatis

Should we update some documentation or how to use the feature? Or is it maybe enough to link to some general instructions from spotify how it's supposed to work?

I'd be happy to do this, would the wiki be the best place? I get the impression the docs/ folder in the repo is mainly for documentation on the Spotify protocol and API rather than the usage of librespot.

SapiensAnatis avatar Nov 09 '25 13:11 SapiensAnatis

Yeah, the wiki is probably the right place. Probably adding it to the options section and then explaining it a bit in detail further below will do the job.

Even tho I personally do rarely if ever look into it.

photovoltex avatar Nov 09 '25 14:11 photovoltex

I have updated this page on the wiki with the new option and some brief usage instructions:

https://github.com/librespot-org/librespot/wiki/Options#local-files

SapiensAnatis avatar Nov 09 '25 14:11 SapiensAnatis

Looks good. Let's get this merged then :D

photovoltex avatar Nov 09 '25 14:11 photovoltex

Thanks for the addition and sorry for the long wait on resolving the topic^^;

photovoltex avatar Nov 09 '25 15:11 photovoltex

Thanks @photovoltex and @roderickvd for your help :smile:

Not to worry about the wait, I recognise this is just a thing you guys do in your spare time and it's the same for me.

I will probably work on the sample rate issue next, so that all local files can be played. It could be quite tricky to get it working across all the backends though.

SapiensAnatis avatar Nov 09 '25 15:11 SapiensAnatis