Conor Thompson
Conor Thompson

Reputation: 308

Python Subprocess - mps-Youtube

I am currently working on a small project that will take in a voice command, like that you would to the Amazon echo for spotify, for YouTube on a Raspberry Pi. Essentially I call out the song or artist, and the script will run the first result (for now).

The library I'm using for streaming the Youtube video to audio back is mps-Youtube, I have the bash automation done for it, which looks like this:

(echo -e "$1"; echo -e "1") | mpsyt 

The first argument being the query for the song and the second is selecting the first result.

The problem I'm having is, so far I can run this in Python using the subprocess module, which so far looks like this (testing out in python shell):

cmd = "/home/pi/bash/play_youtube_song.sh 'tears for fears'"
ps = subprocess.Popen(cmd,shell=True,stdout=subprocess.PIPE,stderr=subprocess.STDOUT)
output = ps.communicate()[0]
print output

however, the script simply prints the return code 0, and the program seems to not fully execute.

Is there anything I can do that will ensure python will run the script fully? I've tried running it in the background using the & in bash, but it doesn't seem to work either, the songs doesn't play.

Thanks in advance.

Upvotes: 2

Views: 379

Answers (1)

Conor Thompson
Conor Thompson

Reputation: 308

Solved it!

So it turns out by default that subprocess uses /bin/sh, so there is a parameter that allows you to change the executable to /bin/bash.

I discovered this from running the bash in sh, and it wasn't echoing all the arguments into mps-youtube, whereas in the gnome-terminal it was achieving this.

Credit: https://www.saltycrane.com/blog/2011/04/how-use-bash-shell-python-subprocess-instead-binsh/

Upvotes: 1

Related Questions