pythonraspberry-pisubprocessos.systemomxplayer

Python not playing Omxplayer video


I am developing a system where a Raspberry Pi plays a YouTube video on top of a PyQt GUI (I think the PyQt bit is irrelevant) (like casting - people have done this before) using Omxplayer.

Instead of playing the video, the PyQt program hangs and the video is not played. The process is explained in more detail below:

A YouTube link (e.g: "https://www.youtube.com/watch?v=ciASlzZCQOU") is sent to my Python script (running on a headless RPi), via a POST request and the script should launch omxplayer on a Raspberry Pi with the following command: "omxplayer -o both --orientation 0 `youtube-dl -g -f best \"{}\"`".format(youtube_link) and display the video on top of a PyQt program that is running. This does work if I manually execute the commmand manually in a shell (both via ssh and physically).

This entire process is taken care of by the following block of code on the RPi:

elif self.path.endswith("/playVideo"):
        content_length = int(self.headers['Content-Length']) # <--- Gets the size of data

        youtube_link = self.rfile.read(content_length).decode('utf-8')
        print(youtube_link)
        play_command = "omxplayer -o both --orientation 0 `youtube-dl -g -f best \"{}\"`".format(youtube_link)
        print(play_command)
        os.system(play_command)

What the block does is decode the POST data, gets the YouTube link and executes the command with the link in a subshell (using os.system()).

This block outputs:

https://www.youtube.com/watch?v=ciASlzZCQOU
omxplayer -o both --orientation 0 `youtube-dl -g -f best "https://www.youtube.com/watch?v=ciASlzZCQOU"`

And after a few seconds causes the main program to hang. After that nothing happens, the video isn't played and I have to force the Python process to exit.

I have been able to make this work before but I seem to have broken it and I think it is a simple issue but I just can't seem to see it. I have tried using subprocess.Popen() instead of os.system() and even created a shell script to do it among other things, but nothing seems to solve the problem.

Any help would be appreciated.


Solution

  • Mark's suggestion in the comments was the solution: Appending a trailing & to the command fixed the problem - it had less to do with os.system.