LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 06-14-2020, 10:06 AM   #1
CheeseEBoi
LQ Newbie
 
Registered: Jun 2020
Posts: 5

Rep: Reputation: Disabled
Best Way to Interface with the MPD Server in Python


Hello, so I would like to incorporate MPD into the media player plugin for the liferea feed aggregator. The plugin is written in Python and it seems like a pretty simple task, but I can't find and MPD server (not client) modules for it online. I found a module called python-mpd-server but it's still in beta. Is there a native module for this or another way I can add this functionality?
 
Old 06-15-2020, 03:37 PM   #2
ondoho
LQ Addict
 
Registered: Dec 2013
Posts: 19,872
Blog Entries: 12

Rep: Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053
What exactly are you trying to achieve?

Looking at the AUR, there's a few results for "mpd python". check out the corresponding web pages (usually github repos).
 
Old 06-15-2020, 10:19 PM   #3
pwalden
Member
 
Registered: Jun 2003
Location: Washington
Distribution: Raspbian, Ubuntu, Chrome/Crouton
Posts: 374

Rep: Reputation: 50
I use https://github.com/Mic92/python-mpd2
 
Old 06-15-2020, 11:56 PM   #4
dugan
LQ Guru
 
Registered: Nov 2003
Location: Canada
Distribution: distro hopper
Posts: 11,219

Rep: Reputation: 5309Reputation: 5309Reputation: 5309Reputation: 5309Reputation: 5309Reputation: 5309Reputation: 5309Reputation: 5309Reputation: 5309Reputation: 5309Reputation: 5309
I'd use this:
https://www.musicpd.org/libs/python-musicpd/
 
Old 06-16-2020, 08:36 AM   #5
CheeseEBoi
LQ Newbie
 
Registered: Jun 2020
Posts: 5

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by dugan View Post
Quote:
Originally Posted by pwalden View Post
Both of these are for MPD clients. I need to integrate MPD into an application so that an MPD client can be used with it.
 
Old 06-16-2020, 11:03 AM   #6
dugan
LQ Guru
 
Registered: Nov 2003
Location: Canada
Distribution: distro hopper
Posts: 11,219

Rep: Reputation: 5309Reputation: 5309Reputation: 5309Reputation: 5309Reputation: 5309Reputation: 5309Reputation: 5309Reputation: 5309Reputation: 5309Reputation: 5309Reputation: 5309
Quote:
Originally Posted by CheeseEBoi View Post
I need to integrate MPD into an application so that an MPD client can be used with it.
Oh, in that case?

Just write BASH scripts that call mpc.
 
Old 06-16-2020, 01:16 PM   #7
CheeseEBoi
LQ Newbie
 
Registered: Jun 2020
Posts: 5

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by dugan View Post
Oh, in that case?

Just write BASH scripts that call mpc.
The problem is that this is actual source code for a plugin. Using a bash script would be pretty unprofessional and inefficient. I'd need it to be like how Rhythmbox or GNOME MPV integrates with MPD.
 
Old 06-16-2020, 01:57 PM   #8
dugan
LQ Guru
 
Registered: Nov 2003
Location: Canada
Distribution: distro hopper
Posts: 11,219

Rep: Reputation: 5309Reputation: 5309Reputation: 5309Reputation: 5309Reputation: 5309Reputation: 5309Reputation: 5309Reputation: 5309Reputation: 5309Reputation: 5309Reputation: 5309
You're talking about this, right?

https://github.com/lwindolf/liferea/...edia-player.py

I'm sorry, but I think we need to take a step back here. What exactly do you want to use the "server libraries" for? "Incorporate MPD" and "I need to integrate MPD into an application so that an MPD client can be used with it." (which is as specific as you got) isn't giving any of us an adequate picture, as I'm sure you've noticed by now.

As MPD is a server, there is normally no reason for "server libraries".

I mean, do you want the server library to:
  • start and stop MPD? (sorry, you'll have to do that yourself)
  • provide an API for clients to use to send commands to MPD? (that's what client libraries are for, and that includes what you linked in the top post)

Last edited by dugan; 06-16-2020 at 02:33 PM.
 
Old 06-16-2020, 02:53 PM   #9
CheeseEBoi
LQ Newbie
 
Registered: Jun 2020
Posts: 5

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by dugan View Post
You're talking about this, right?

https://github.com/lwindolf/liferea/...edia-player.py

I'm sorry, but I think we need to take a step back here. What exactly do you want to use the "server libraries" for? "Incorporate MPD" and "I need to integrate MPD into an application so that an MPD client can be used with it." (which is as specific as you got) isn't giving any of us an adequate picture, as I'm sure you've noticed by now.

As MPD is a server, there is normally no reason for "server libraries".

I mean, do you want the server library to:
  • start and stop MPD? (sorry, you'll have to do that yourself)
  • provide an API for clients to use to send commands to MPD? (that's what client libraries are for, and that includes what you linked in the top post)
Sorry for being vague. I am trying to add play/pause/next/previous key functionality to the plugin. From what I know that's an MPD thing.
I guess I'll need to do more research on the subject.
 
Old 06-16-2020, 03:35 PM   #10
dugan
LQ Guru
 
Registered: Nov 2003
Location: Canada
Distribution: distro hopper
Posts: 11,219

Rep: Reputation: 5309Reputation: 5309Reputation: 5309Reputation: 5309Reputation: 5309Reputation: 5309Reputation: 5309Reputation: 5309Reputation: 5309Reputation: 5309Reputation: 5309
Then you use any of the three client libraries that have already been linked: the one you linked to in the top post, or the two that were linked later.
 
Old 06-16-2020, 03:54 PM   #11
dugan
LQ Guru
 
Registered: Nov 2003
Location: Canada
Distribution: distro hopper
Posts: 11,219

Rep: Reputation: 5309Reputation: 5309Reputation: 5309Reputation: 5309Reputation: 5309Reputation: 5309Reputation: 5309Reputation: 5309Reputation: 5309Reputation: 5309Reputation: 5309
Here's a demo of using python-musicpd to play a add a song from the MPD library to the playlist, play it, stop playing, and remove it from the playlist:

Code:
>>> from musicpd import MPDClient
>>> from pprint import pprint
>>> client = MPDClient()
>>> client.connect('localhost', 6600)
>>> song = client.listallinfo()[-1]
>>> pprint(song)
{'album': 'Unearthed',
 'artist': 'Johnny Cash',
 'duration': '219.306',
 'file': 'unearthed/79 - Hurt.flac',
 'format': '44100:16:2',
 'label': 'American Recordings',
 'last-modified': '2018-05-24T14:57:25Z',
 'musicbrainz_albumid': '4321855e-8e8e-4786-8506-28e6d69633b9',
 'musicbrainz_artistid': 'd43d12a1-2dc9-4257-a2fd-0a3bb1081b86',
 'time': '219',
 'title': 'Hurt',
 'track': '79'}
>>> songid = client.addid(song['file'])
>>> songid
'2'
>>> client.playid(songid)
>>> # I can hear the song playing now
>>> client.stop()
>>> # The music stops
>>> client.deleteid(songid)
>>> client.disconnect()
In MPD, playing a song is done in two steps: adding it to the playlist, and playing it from the playlist.

Also, you need to understand how MPD works and what the D means. It runs as a server, and it makes itself available either at a port that you can connect a socket to, or as a UNIX socket. Clients then connect to that by opening the appropriate type of socket, sending it commands through the socket, and receiving the results through the socket. If you have MPD running at port 6600, then you can actually telnet to port 6600 and just issue it commands from the keyboard.

If you're familiar with databases: that's how a lot of them work too.

And if I'm coming across as condescending by explaining something this "obvious": I'm currently writing my second MPD client, and let me tell you, this wasn't obvious to me for a long time.

Last edited by dugan; 06-16-2020 at 04:04 PM.
 
1 members found this post helpful.
Old 06-16-2020, 04:04 PM   #12
CheeseEBoi
LQ Newbie
 
Registered: Jun 2020
Posts: 5

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by dugan View Post
Here's a demo of using python-musicpd to play a add a song from the MPD library to the playlist, play it, stop playing, and remove it from the playlist:

Code:
>>> from musicpd import MPDClient
>>> from pprint import pprint
>>> client = MPDClient()
>>> client.connect('localhost', 6600)
>>> song = client.listallinfo()[-1]
>>> pprint(song)
{'album': 'Unearthed',
 'artist': 'Johnny Cash',
 'duration': '219.306',
 'file': 'unearthed/79 - Hurt.flac',
 'format': '44100:16:2',
 'label': 'American Recordings',
 'last-modified': '2018-05-24T14:57:25Z',
 'musicbrainz_albumid': '4321855e-8e8e-4786-8506-28e6d69633b9',
 'musicbrainz_artistid': 'd43d12a1-2dc9-4257-a2fd-0a3bb1081b86',
 'time': '219',
 'title': 'Hurt',
 'track': '79'}
>>> songid = client.addid(song['file'])
>>> songid
'2'
>>> client.playid(songid)
>>> # I can hear the song playing now
>>> client.stop()
>>> # The music stops
>>> client.deleteid(songid)
>>> client.disconnect()
In MPD, playing a song is done in two steps: adding it to the playlist, and playing it from the playlist.

Also, you need to understand how MPD works and what the D means. It runs as a server, and it makes itself available either at a port that you can connect a socket to, or as a UNIX socket. Clients then connect to that by opening the appropriate type of socket, sending it commands through the socket, and receiving the results through the socket. If you have MPD running at port 6600, then you can actually telnet to port 6600 and just issue it commands from the keyboard.

If you're familiar with databases: that's how a lot of them work too.

Thank you so much for the help. I was pretty confused about this, clearly, but now I think I understand. I'll see if I can implement this!
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
I got error while installing python-tk python-psycopg2 python-twisted saili kadam Linux - Newbie 1 09-05-2015 03:03 AM
Configuring mpd (music player daemon) for best performance rburra Linux - Software 0 08-13-2012 09:10 PM
LXer: Python Python Python (aka Python 3) LXer Syndicated Linux News 0 08-05-2009 08:30 PM
What distro to use for home nfs/rsync/mpd/apache server? hollywoodb Linux - General 2 03-23-2007 12:02 PM
How can I enter php interface like "python[enter]" in python backpacker Linux - Software 1 06-20-2005 07:38 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:30 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration