Commands to play songs and playlists from terminal

Solved!
Reply
1 person liked this

Commands to play songs and playlists from terminal

Fraxav
Regular

Hello,

so I want to wake up listening to some music and I've found an app that lets me execute a command at a given time. I want that app to start spotify and automatically play a song or a playlist but I can't figure out a command to do that from the terminal (so that I can paste it on my app).

I've tried spotify --playpause but it doesn't play anything. Typing spotify "playlist URI" seems to open spotify on the given playlist but I don't know how to make it start. Do you know any way to operate Spotify via terminal? What are the commands and syntax?

Any help is very appreciated.

1 ACCEPTED SOLUTION

Accepted Solutions
Solution! 1 person liked this

Re: Commands to play songs and playlists from terminal

olejon
Roadie

@Fraxav wrote:

Thank you olejon, both your command and the script worked great. I wonder if the result of the script (open spotify and play URI) could be obtained with a single command from terminal


So you have two options. Either create and save the script I mentioned, and then run that script, or use a single command that combines all of them.

 

Your example won't work, because the two last commands won't run before you quit Spotify. Adding & puts Spotify in the background, while 1>/dev/null 2>&1 ignores any output. We must use paranthesis to daemonize (put in background) when combining multiple commands. && is used to run a command after the previous succeeds. So & and && are two completely different things.

 

This should work:

 

(spotify 1>/dev/null 2>&1 &) && sleep 3 && qdbus org.mpris.MediaPlayer2.spotify / org.freedesktop.MediaPlayer2.OpenUri spotify:album:4m2880jivSbbyEGAKfITCa

 Remember, all on one line.

 

Depending on how long time Spotify uses to cold start (reboot to check), you can increase or decrease the sleep time (in seconds).

 


The shell is very powerful. You can run a second command if the first one fails also, like this:

 

command1 || command2

 Or run the second regardless:

 

command1; command2

 

SpotCommander - The most elegant, intuitive, feature-rich & universal remote control for Spotify, exclusive for Linux users!
19 Replies

Re: Commands to play songs and playlists from terminal

Liam
Community Legend

I don't believe there is a way to operate Spotify from the terminal... I think there is some sort of command that can be sent to it (Toastify must use it), but I'm unsure as to what it is.

 

Actually, thinking about it, I'm sure I remember seeing a post about this somewhere. I'll see if I can dig it out (or work out the commands myself).

 

Liam

3 people liked this

Re: Commands to play songs and playlists from terminal

olejon
Roadie

This is indeed possible on Linux. You can start playing any URI by running this command, with Spotify already running:

 

qdbus org.mpris.MediaPlayer2.spotify / org.freedesktop.MediaPlayer2.OpenUri URI

 Replace that last URI with the actual URI. It should be on one line.

 

Example script to start Spotify and start playing an album:

 

#!/bin/bash

spotify 1>/dev/null 2>&1 &

sleep 3

qdbus org.mpris.MediaPlayer2.spotify / org.freedesktop.MediaPlayer2.OpenUri spotify:album:4m2880jivSbbyEGAKfITCa

 

SpotCommander - The most elegant, intuitive, feature-rich & universal remote control for Spotify, exclusive for Linux users!
1 person liked this

Re: Commands to play songs and playlists from terminal

Fraxav
Regular

Thank you olejon, both your command and the script worked great. I wonder if the result of the script (open spotify and play URI) could be obtained with a single command from terminal, i.e:

spotify && qdbus org.mpris.MediaPlayer2.spotify / org.freedesktop.MediaPlayer2.OpenUri URI

 I tried all the connectors I could find but none of them worked. I'm not good with commands...

Solution! 1 person liked this

Re: Commands to play songs and playlists from terminal

olejon
Roadie

@Fraxav wrote:

Thank you olejon, both your command and the script worked great. I wonder if the result of the script (open spotify and play URI) could be obtained with a single command from terminal


So you have two options. Either create and save the script I mentioned, and then run that script, or use a single command that combines all of them.

 

Your example won't work, because the two last commands won't run before you quit Spotify. Adding & puts Spotify in the background, while 1>/dev/null 2>&1 ignores any output. We must use paranthesis to daemonize (put in background) when combining multiple commands. && is used to run a command after the previous succeeds. So & and && are two completely different things.

 

This should work:

 

(spotify 1>/dev/null 2>&1 &) && sleep 3 && qdbus org.mpris.MediaPlayer2.spotify / org.freedesktop.MediaPlayer2.OpenUri spotify:album:4m2880jivSbbyEGAKfITCa

 Remember, all on one line.

 

Depending on how long time Spotify uses to cold start (reboot to check), you can increase or decrease the sleep time (in seconds).

 


The shell is very powerful. You can run a second command if the first one fails also, like this:

 

command1 || command2

 Or run the second regardless:

 

command1; command2

 

SpotCommander - The most elegant, intuitive, feature-rich & universal remote control for Spotify, exclusive for Linux users!

Re: Commands to play songs and playlists from terminal

Fraxav
Regular

And it works indeed. I had to increase the sleep time a little to play my playlist, but for some reason 3 was enough when playing your album. Anyway, that's exactly what I needed. Thanks so much for your help!

Re: Commands to play songs and playlists from terminal

olejon
Roadie

You're welcome :-)

 

If this is for playing music when you wake up, setting the sleep to a high value, like 10, can be a good idea, to make sure Spotify is up and running and ready to receive commands. One could of course create a script that loops to check if D-Bus is ready, and then play the URI, but that is not necessary in this case, just complicated.

SpotCommander - The most elegant, intuitive, feature-rich & universal remote control for Spotify, exclusive for Linux users!

Re: Commands to play songs and playlists from terminal

Fraxav
Regular

Yes 10 seems to work fine right now. I'm going to test the whole thing on startup just to make sure.

On a side note, it seems that the playlist I'm playing always starts (of course) with the first song. I was wondering if I could add one of the spotify commands ('next') to make it skip to the next song (which would be a random one since I have shuffling enabled), and I found this:

 

org.mpris.MediaPlayer2.Player.Next() -> ()

 

I tried to add that at the end of your magic formula but it didn't work (didn't even start playing). Do you mind a last tip about this one?

1 person liked this

Re: Commands to play songs and playlists from terminal

olejon
Roadie

I tried to add that at the end of your magic formula but it didn't work (didn't even start playing). Do you mind a last tip about this one?


This should do it:

 

(spotify 1>/dev/null 2>&1 &) && sleep 3 && qdbus org.mpris.MediaPlayer2.spotify / org.freedesktop.MediaPlayer2.OpenUri spotify:album:4m2880jivSbbyEGAKfITCa && qdbus org.mpris.MediaPlayer2.spotify / org.freedesktop.MediaPlayer2.Next

 You may have to put a sleep between the OpenUri and Next. You'll see. For me the above works.

 

Replace the album URI with your playlist URI.

SpotCommander - The most elegant, intuitive, feature-rich & universal remote control for Spotify, exclusive for Linux users!

Re: Commands to play songs and playlists from terminal

Fraxav
Regular

It works for me too, without the sleep. I was trying to do it without all the necessary code, of course...

Thank you again!

SUGGESTED POSTS