Giter Site home page Giter Site logo

spotrec's Introduction

SpotRec

Python script to record the audio of the Spotify desktop client using FFmpeg and PulseAudio

AUR: https://aur.archlinux.org/packages/spotrec/

Usage

If you use the AUR package, you can simply run:

spotrec

If you have a GNU/Linux distribution with a different package manager system, run:

python3 spotrec.py

Example

First of all run spotify.

Then you can run the python script which will record the music:

./spotrec.py -o ./my_song_dir --skip-intro

Check the pulseaudio configuration:

pavucontrol

Pay attention to the red circles, everything else is muted and with volume set to 0%

playback tab

Note: actually "Lavf..." will appear after you start playing a song

recording tab

output devices tab

input devices tab

configuration tab

Finally start playing whatever you want

Hints

  • Disable volume normalization in the Spotify Client

  • Do not change the volume during recording

  • Use Audacity for post processing

    • because SpotRec records a little longer at the end to ensure that nothing is missing of the song. But sometimes this also includes the beginning of the next song. So you should use Audacity to cut the audio to what you want. From Audacity you can also export it to the format you like (ogg/mp3/...).

Troubleshooting

Start the script with the debug flag:

./spotrec.py --debug

If one of the following scenarios happens:

  • you do not see something like the ffmpeg output, which should appear right few seconds after the song start
# what you should see when ffmpeg is recording ...
size=56400kB time=00:00:04.15 bitrate= 130.7kbits/s speed=1x
  • you do not see any "Lavf..." in the pavucontrol recording tab
  • you get a stacktrace ending with:
ValueError: invalid literal for int() with base 10: 'nput'

I would suggest you to:

  • quickly press the "next song button" and then the "previous song button" in the spotify client
  • stop everything and start over, after some tries it usually works :)

Note: sometimes spotify detects when the user does not interact with the application for a long time (more or less an hour) and starts looping over a song, to avoid this scenario I would suggest to keep interacting with the spotify client.

spotrec's People

Contributors

bleuzen avatar nokyan avatar rijul-a avatar thejena avatar thescooby avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

spotrec's Issues

Failed to connect to Spotify

what is the intended way to run spotify? I mean I have the web player running as I am not aware of any native spotify client on linux. I even tried spotify-qt which also gets identified by pavucontrol as "Chrome". Pulseaudio is running ofc.. What do I miss here?
Package has been installed from AUR (running Manjaro Linux here)

spotrec --debug -s -u -o /home/foo/Musik/
2020-09-25 17:24:25,840 - DEBUG - Logger initialized
2020-09-25 17:24:25,842 - ERROR - Failed to connect to Spotify. (Maybe it's not running yet?)

2020-09-25_17-27

as you can see it looks like there is no recording device which would be spotrec I guess. but as that cannot be started.. :

2020-09-25_17-27_1

realized that maybe I need to install the package spotifyd and I did that as well - looking correctly connected:

image

but even now -- nothing changed at all. everything still the same. same error by spotrec and screens look identical.

Allow to skip existing titles

At the moment, it just overwrites a file if it already exists.
I think it would be cool to have a command line option to skip these songs entirely and resume with the next song in the queue.

@Bleuzen if you agree with the feature request, I would give it a try to implement this by myself.
In my opinion, this is probably a pretty trivial fix, since we'd just have to use send_dbus_cmd("Next") in case the file exist, right?

Additional file formats as option

While I like the flac format, for some cases, MP3 oder M4A would be great options. Shouldn't ffmpeg be capable to record to other formats easily?

Coverart error when using --output-directory

When using -a and -c together the cover will not saved.

Excample command:
spotrec -o ./spotify --skip-intro -a --debug

gives this error:

2021-10-27 22:36:33,655 - DEBUG - [FFmpeg] [2075] Successfully renamed .04 - RAINASH - Dark Blue Sky.flac
2021-10-27 22:36:33,656 - DEBUG - [FFmpeg] Cover art is on server for ./spotify/04 - RAINASH - Dark Blue Sky.flac
2021-10-27 22:36:33,658 - DEBUG - Starting new HTTPS connection (1): i.scdn.co:443
2021-10-27 22:36:33,949 - DEBUG - https://i.scdn.co:443 "GET /image/ab67616d0000b2734eeb84104895cc348c822c44 HTTP/1.1" 200 127803
Exception in thread Thread-12:
Traceback (most recent call last):
  File "/usr/lib/python3.9/threading.py", line 973, in _bootstrap_inner
    self.parent.add_cover_art()
    self.run()
  File "/usr/bin/spotrec", line 528, in run
  File "/usr/bin/spotrec", line 577, in add_cover_art
    with open(cover_file, "wb") as fd:
FileNotFoundError: [Errno 2] No such file or directory: './spotify/./spotify/04 - RAINASH - Dark Blue Sky.jpeg'

It looks like the script is messing up the path as it adds ./spotify/ , that is provided by the --output-directory, twice.

repeating same song

I have tried both the original and updated version, and the recording just loops around one or two songs.
By the way, have you looked at Audio Recorder as when the source is set to spotify it does what you are trying to achieve.

Recording starts too late

The recordings start a fraction of a second too late. This is especially noticeable in songs that start directly with a clear sound. Part of the sound is then missing from the recording, so that it is noticeable when listening to it. An example is the following song: https://open.spotify.com/track/6b7WHomwS4t8uNo9zAVqfr?si=c9e1ce747484405c ... There are several such songs on the album that start directly with a sound. Therefore, the problem can be reproduced.

My system is a Manjaro Linux with KDE Plasma Version 5.23.4, spotrec 0.15.1 and Spotify 1.1.72.439

Qobuz instead of spotify

I'd like to use this script with qobuz instead of spotify.
Would You be so kind to modify it for me (and maybe others)?

Disable longer recording at the end?

Hi,
I just tested SpotRec with a few songs and everything works fine. Thanks for that!

It's more a question but an issue:
Is it possible to disable the function that SpotRec records a little longer at the end?
If I'm recording a whole album, I sometimes have the problem, that the first chords of the song are in the flac-file of the previous song.

Best regards
J.

ffmpeg - Unknown input format: 'pulse' (EndlessOS, pavucontrol flatpak)

when I try to use SpotRec under EndlessOS (a OSTree/flatpak based Debian derivate), the ffmpeg command issued by by spotrec fails miserably with error: Unknown input format: 'pulse' . I tried it with the static ffmpeg executable provided by John van Sickle, apparently it does not work with it, but I don't know if this is the reason for the failure or the cause is by insufficient rights in the flatpak sandbox. I guess that I might be able to use ffmpeg from your FFaudioConverter flatpak, but I don't know how to do that. So any hints are welcome. A --debug output is here: spotrec.log.

Add requirements.txt or similar to define dependencies

Let me start off by saying that I love your tool, honestly!

I ran into an issue though when I tried to clone this repo and run it in a debugger, because I can't figure out which libraries I need to install. The Deps: list in the script itself isn't that helpful to be honest:

  • python-dbus is actually called dbus-python
  • does naming python in this list just mean that the interpreter has to be installed?
  • bash: Does this mean we rely on running the script in a surrounding that can be controlled via bash syntax or is that a pyhton library with the same name? pulseaudio analogously.
  • requests is a library, I happen to know that at least

All of this could be solved with a requirements.txt file or whatever sort of dependency management appeals to you.
Keep up the good work, I appreciate it!

Hacky solution for more automatic/accurate track separation

To allow greater automation for people who want to just set it and go, like myself. It's possible to make a playlist of all the songs you want with a silent track separating them.

You could then have an optional mode where you would read this as an end of track message and close ffmpeg.

This would also eliminate the pause > previous > play commands between each song. Simply start the next recording then send the next song command.

A nice 10 second example titled "Silent Track" from artist "Silent Track" is here:
http://open.spotify.com/track/5XSKC4d0y0DfcGbvDOiL93?si=V6UuIOkYRl2XrJOzhVHlIA

However, since this would then just shift the tedium from post processing to playlist creation, I've found a lovely playlist creator with conditionals, combiners and other goodies:
http://smarterplaylists.playlistmachinery.com/examples.html

I created a massive playlist of the silent track by continually copying its tracks into itself. I stopped at 8192. Then I used the "alternate" combiner to combine that playlist with my saved tracks. It stops once one list is exhausted. When run, it quickly spits out a nice silence gapped playlist =D.

What do you think?

Issue when running spotrec ( AUR edition )

This is the output when I run the command 'spotrec' in terminal ( endeavour os )

output:

Traceback (most recent call last):
File "/usr/bin/spotrec", line 22, in
import requests
ModuleNotFoundError: No module named 'requests'

How do I solve that issue?

add option for continuous recording

It would be fabulous if there was an option to turn off automatic pausing between recording different tracks. So that, for example, a playlist or an album is recorded in one piece. The background is that there are albums that are divided into individual titles but are actually related. The automatic pauses between the titles then create an untypical silence between the sections.

Suddenly no recording possible anymore

After the change to pipewire (ubuntu 22.04) everything worked as expected! But suddenly - maybe because of a general ubuntu update - spotrec only generates empty files. When running with -d debug messages I get:

2022-10-03 19:12:39,508 - INFO - [Spotify] Song changed: Amongst Liars - Amongst Liars/01 - Amongst Liars - Cut It
2022-10-03 19:12:44,510 - INFO - [SpotRec] Starting recording
2022-10-03 19:12:44,511 - DEBUG - [Shell] run: dbus-send --print-reply --dest=org.mpris.MediaPlayer2.spotify /org/mpris/MediaPlayer2 org.mpris.MediaPlayer2.Player.Pause
method return time=1664817164.562944 sender=:1.543 -> destination=:1.550 serial=95 reply_serial=2
2022-10-03 19:12:44,563 - DEBUG - [Shell] run: dbus-send --print-reply --dest=org.mpris.MediaPlayer2.spotify /org/mpris/MediaPlayer2 org.mpris.MediaPlayer2.Player.Previous
method return time=1664817164.567402 sender=:1.543 -> destination=:1.551 serial=96 reply_serial=2
2022-10-03 19:12:44,568 - DEBUG - [Shell] Popen: ffmpeg -hide_banner -y -f pulse -ac 2 -ar 44100 -fragment_size 8820 -i spotrec.monitor -metadata artist='Amongst Liars' -metadata album='Amongst Liars' -metadata track=1 -metadata title='Cut It' -acodec mp3 -vn -b:a 192k './Amongst Liars - Amongst Liars/.01 - Amongst Liars - Cut It.mp3'
2022-10-03 19:12:44,568 - INFO - [FFmpeg] [115717] Recording started
2022-10-03 19:12:44,634 - INFO - [Spotify] State changed: Paused
Guessed Channel Layout for Input Stream #0.0 : stereo
Input #0, pulse, from 'spotrec.monitor':
  Duration: N/A, bitrate: 1411 kb/s
  Stream #0:0: Audio: pcm_s16le, 44100 Hz, stereo, s16, 1411 kb/s
Stream mapping:
  Stream #0:0 -> #0:0 (pcm_s16le (native) -> mp3 (libmp3lame))
Press [q] to stop, [?] for help
2022-10-03 19:12:44,819 - DEBUG - [Shell] run: dbus-send --print-reply --dest=org.mpris.MediaPlayer2.spotify /org/mpris/MediaPlayer2 org.mpris.MediaPlayer2.Player.Play
method return time=1664817164.822670 sender=:1.543 -> destination=:1.552 serial=101 reply_serial=2
2022-10-03 19:12:44,857 - INFO - [Spotify] State changed: Playing
spotrec.monitor: Generic error in an external library
Output #0, mp3, to './Amongst Liars - Amongst Liars/.01 - Amongst Liars - Cut It.mp3':
  Metadata:
    TPE1            : Amongst Liars
    TALB            : Amongst Liars
    TRCK            : 1
    TIT2            : Cut It
    TSSE            : Lavf58.76.100
  Stream #0:0: Audio: mp3, 44100 Hz, stereo, s16p, 192 kb/s
    Metadata:
      encoder         : Lavc58.134.100 libmp3lame
size=       1kB time=00:00:00.00 bitrate=N/A speed=   0x    
video:0kB audio:0kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: unknown
Output file is empty, nothing was encoded (check -ss / -t / -frames parameters if used)

Unfortunately the color get lost here. There is the line "spotrec.monitor: Generic error in an external library" that is marked red. I can also NOT find a recording stream in pavucontrol, but I guess this is a subsequent issue because of the generic error above.

Any glue how to debug deeper?

ffmpeg options

Hi, first of all thanks for this gem.

Would it be possible to select output options of ffmpeg, or even simpler just a general selection of the output quality. e.g. for Audiobooks the quality could be mp3/96, not neccessary to have flac.

Cheers.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.