Adding Audio Streaming from Youtube in SUSI Linux
In this blog post we will describe how the youtube streaming works in the
SUSI smart speaker and how audio is streamed directly from youtube videos.
To achieve this process, we have used an amazing Open-Source project called MPV music Player along with python libraries like Subprocess.
1.Processing a Query to the server
Firstly , the user asks the smart speaker to play the youtube audio by simply adding a ‘play’ word before his/her favorite song. eg. I’ll say ‘play despacito’ and then the command is recognized and a query is sent to the server which sends the following response as a JSON object.
“actions”: [ { “type”: “answer”, “expression”: “Playing Luis Fonsi – Despacito ft. Daddy Yankee” }, { “identifier”: “kJQP7kiw5Fk”, “identifier_type”: “youtube”, “type”: “video_play” }] |
2.Parsing the response
Then the speaker parses the response in the following way.
The Speaker traverses through all the actions returned in the response and checks for all the “identifier” by assigning a custom class to it.
class VideoAction(BaseAction): def __init__(self, identifier , identifier_type): super().__init__() self.identifier = identifier self.identifier_type = identifier_type |
Now we check whether the query is the type of a custom class VideoAction and then the client processes the query as the response.
elif isinstance(action, VideoAction): result[‘identifier’] = action.identifier audio_url = result[‘identifier’] |
3.Implementing the Actions
Now that we have identified that the response contains a Video Action, we can finally implement a way to play the audio from the URL.
We use a music player called MPV Music Player and the library Subprocess to make it run asynchronously.
if ‘identifier’ in reply.keys(): classifier = reply[‘identifier’] if classifier[:3] == ‘ytd’: video_url = reply[‘identifier’] video_pid = subprocess.Popen(‘mpv –no-video https://www.youtube.com/watch?v={} –really-quiet &’.format(video_url[4:]), shell=True) # nosec #pylint-disable type: ignore self.video_pid = video_pid.pid |
This is how audio is streamed from youtube videos in SUSI Smart Speaker.
Resources
- https://github.com/mpv-player/mpv
- https://docs.python.org/2/library/subprocess.html
- https://github.com/fossasia/susi_linux
- https://github.com/fossasia/susi_api_wrapper
Tags
fossasia, gsoc’18, susi, susi.ai, youtube, music, mp3 , mpv, audio stream
You must be logged in to post a comment.