You are not logged in.

#1 2017-09-29 10:04:25

geo909
Member
Registered: 2008-09-07
Posts: 309

Mpd: add song to existing playlist with mpc

Hi all,

I'm using mpd to manage my music, along with mpc and vimpc. I don't know if I'm missing something, but I haven't found a way to add a song to an existing playlist. Is there any way to do that? I've been looking at the man pages for mpc but I can't find anything.

Thanks a lot in advance.

Offline

#2 2017-09-29 13:01:06

seth
Member
Registered: 2012-09-03
Posts: 51,029

Re: Mpd: add song to existing playlist with mpc

[[clear playlist]
add existing playlist]
add file
save playlist

Offline

#3 2017-09-29 13:50:21

geo909
Member
Registered: 2008-09-07
Posts: 309

Re: Mpd: add song to existing playlist with mpc

seth wrote:

[[clear playlist]
add existing playlist]
add file
save playlist

Makes sense, I guess I can write a script to streamline something like this.

Offline

#4 2017-11-26 09:44:16

siavash119
Member
Registered: 2017-11-26
Posts: 1

Re: Mpd: add song to existing playlist with mpc

This will add currently playing song to a defined playlist.

requires libmpdclient
1) edit below code with your defines and save to file e.g. add-to-mpd-playlist.c
2) gcc or clang add-to-mpd-playlist.c -o add-to-mpd-playlist -lmpdclient
3) run binary

further improvements include allowing arguments and/or a config file for host,port,pass,playlist
libmpdclient doc is your friend.

#include <stdio.h>
#include <mpd/client.h>

//#define DEBUG
#ifdef DEBUG
#define D(x) do { x; } while(0)
#else
#define D(x) do { } while(0)
#endif

#define HOST "YOUR_HOSTNAME"
#define PORT YOUR_PORTNUMBER //usually it's 6600
#define PASS "YOUR_PASSWORD" //comment out if no password
#define PLAYLIST "PLAYLIST_NAME_TO_ADD_CURRENT_SONG_TO"

struct mpd_connection* conn(){
	D(printf("%s %s\n","Connecting to",HOST));
	const char* host = HOST;
	unsigned port = PORT;
	struct mpd_connection* c = mpd_connection_new(host,port,0);

	enum mpd_error err = mpd_connection_get_error(c);
	if(err != 0){
		printf("Error code: %u. View error codes here: https://www.musicpd.org/doc/libmpdclient/error_8h.html\n",err);
		return 0;
	}

	#ifdef PASS
	const char* pass = PASS;
	if(mpd_run_password(c,pass) == false){
		printf("%s\n","Bad password");
		return 0;
	}
	#endif

	D(printf("%s %s\n","Connected to",HOST));
	return c;
}

int main(){
	struct mpd_connection* c = conn();
	if(c == 0) return -1;
	
	struct mpd_song* curr = mpd_run_current_song(c);
	const char* curr_uri = mpd_song_get_uri(curr);
	D(printf("Currently playing: %s\n",curr_uri));
	
	if(mpd_run_playlist_add(c,PLAYLIST,curr_uri)){
		printf("%s %s %s %s\n","Added",curr_uri,"to playlist",PLAYLIST);
	}
	else{
		printf("%s\n","Some error");
		return -1;
	}

	return 0;
}

I put a few checks and a bit of debug as well; do with the code as you wish.

Last edited by siavash119 (2017-11-26 09:46:16)

Offline

#5 2017-12-09 18:06:01

geo909
Member
Registered: 2008-09-07
Posts: 309

Re: Mpd: add song to existing playlist with mpc

siavash119 wrote:

This will add currently playing song to a defined playlist.

requires libmpdclient
1) edit below code with your defines and save to file e.g. add-to-mpd-playlist.c
2) gcc or clang add-to-mpd-playlist.c -o add-to-mpd-playlist -lmpdclient
3) run binary

further improvements include allowing arguments and/or a config file for host,port,pass,playlist
libmpdclient doc is your friend.

#include <stdio.h>
#include <mpd/client.h>

//#define DEBUG
#ifdef DEBUG
#define D(x) do { x; } while(0)
#else
#define D(x) do { } while(0)
#endif

#define HOST "YOUR_HOSTNAME"
#define PORT YOUR_PORTNUMBER //usually it's 6600
#define PASS "YOUR_PASSWORD" //comment out if no password
#define PLAYLIST "PLAYLIST_NAME_TO_ADD_CURRENT_SONG_TO"

struct mpd_connection* conn(){
	D(printf("%s %s\n","Connecting to",HOST));
	const char* host = HOST;
	unsigned port = PORT;
	struct mpd_connection* c = mpd_connection_new(host,port,0);

	enum mpd_error err = mpd_connection_get_error(c);
	if(err != 0){
		printf("Error code: %u. View error codes here: https://www.musicpd.org/doc/libmpdclient/error_8h.html\n",err);
		return 0;
	}

	#ifdef PASS
	const char* pass = PASS;
	if(mpd_run_password(c,pass) == false){
		printf("%s\n","Bad password");
		return 0;
	}
	#endif

	D(printf("%s %s\n","Connected to",HOST));
	return c;
}

int main(){
	struct mpd_connection* c = conn();
	if(c == 0) return -1;
	
	struct mpd_song* curr = mpd_run_current_song(c);
	const char* curr_uri = mpd_song_get_uri(curr);
	D(printf("Currently playing: %s\n",curr_uri));
	
	if(mpd_run_playlist_add(c,PLAYLIST,curr_uri)){
		printf("%s %s %s %s\n","Added",curr_uri,"to playlist",PLAYLIST);
	}
	else{
		printf("%s\n","Some error");
		return -1;
	}

	return 0;
}

I put a few checks and a bit of debug as well; do with the code as you wish.

Many thanks!!

Offline

Board footer

Powered by FluxBB