Skip to content

Instantly share code, notes, and snippets.

@t1m0thyj
Last active February 4, 2023 09:10
Show Gist options
  • Star 15 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save t1m0thyj/ddf6aaa6b3d37f602fedfd38c9af9d49 to your computer and use it in GitHub Desktop.
Save t1m0thyj/ddf6aaa6b3d37f602fedfd38c9af9d49 to your computer and use it in GitHub Desktop.
Play music from YouTube in Google Assistant on Raspberry Pi
#!/usr/bin/env python3
# Copyright 2017 Google Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Run a recognizer using the Google Assistant Library.
The Google Assistant Library has direct access to the audio API, so this Python
code doesn't need to record audio. Hot word detection "OK, Google" is supported.
The Google Assistant Library can be installed with:
env/bin/pip install google-assistant-library==0.0.2
It is available for Raspberry Pi 2/3 only; Pi Zero is not supported.
"""
import logging
import re
import subprocess
import sys
import aiy.assistant.auth_helpers
import aiy.assistant.device_helpers
import aiy.audio
import aiy.voicehat
from google.assistant.library import Assistant
from google.assistant.library.event import EventType
import vlc
import youtube_dl
logging.basicConfig(
level=logging.INFO,
format="[%(asctime)s] %(levelname)s:%(name)s:%(message)s"
)
ydl_opts = {
'default_search': 'ytsearch1:',
'format': 'bestaudio/best',
'noplaylist': True,
'quiet': True
}
vlc_instance = vlc.get_default_instance()
vlc_player = vlc_instance.media_player_new()
def power_off_pi():
aiy.audio.say('Good bye!')
subprocess.call('sudo shutdown now', shell=True)
def reboot_pi():
aiy.audio.say('See you in a bit!')
subprocess.call('sudo reboot', shell=True)
def say_ip():
ip_address = subprocess.check_output("hostname -I | cut -d' ' -f1", shell=True)
aiy.audio.say('My IP address is %s' % ip_address.decode('utf-8'))
def play_music(name):
try:
with youtube_dl.YoutubeDL(ydl_opts) as ydl:
meta = ydl.extract_info(name, download=False)
except Exception:
aiy.audio.say('Sorry, I can\'t find that song.')
return
if meta:
info = meta['entries'][0]
vlc_player.set_media(vlc_instance.media_new(info['url']))
aiy.audio.say('Now playing ' + re.sub(r'[^\s\w]', '', info['title']))
vlc_player.play()
def process_event(assistant, event):
status_ui = aiy.voicehat.get_status_ui()
if event.type == EventType.ON_START_FINISHED:
status_ui.status('ready')
if sys.stdout.isatty():
print('Say "OK, Google" then speak, or press Ctrl+C to quit...')
elif event.type == EventType.ON_CONVERSATION_TURN_STARTED:
status_ui.status('listening')
elif event.type == EventType.ON_RECOGNIZING_SPEECH_FINISHED and event.args:
print('You said:', event.args['text'])
text = event.args['text'].lower()
if text == 'stop':
if vlc_player.get_state() == vlc.State.Playing:
vlc_player.stop()
elif text == 'power off':
assistant.stop_conversation()
power_off_pi()
elif text == 'reboot':
assistant.stop_conversation()
reboot_pi()
elif text == 'ip address':
assistant.stop_conversation()
say_ip()
elif text == 'pause':
assistant.stop_conversation()
vlc_player.set_pause(True)
elif text == 'resume':
assistant.stop_conversation()
vlc_player.set_pause(False)
elif text.startswith('play '):
assistant.stop_conversation()
play_music(text[5:])
elif event.type == EventType.ON_END_OF_UTTERANCE:
status_ui.status('thinking')
elif event.type == EventType.ON_CONVERSATION_TURN_FINISHED:
status_ui.status('ready')
elif event.type == EventType.ON_ASSISTANT_ERROR and event.args and event.args['is_fatal']:
sys.exit(1)
def main():
credentials = aiy.assistant.auth_helpers.get_assistant_credentials()
device_id, model_id = aiy.assistant.device_helpers.get_ids(credentials)
with Assistant(credentials, model_id) as assistant:
for event in assistant.start():
process_event(assistant, event)
if __name__ == '__main__':
main()
@i621148
Copy link

i621148 commented Jul 15, 2018

pi@raspberrypi:~/AIY-projects-python/src/examples/voice $ python t1m0thyj_music.py
Traceback (most recent call last):
File "t1m0thyj_music.py", line 32, in
import aiy.assistant.auth_helpers
ImportError: No module named aiy.assistant.auth_helpers

@khayamgondal
Copy link

khayamgondal commented Aug 17, 2018

use python3 to run your script.

@jemaldonado
Copy link

Do you have any examples of music to test?
Thanks for sharing, amazing work.

Copy link

ghost commented Sep 22, 2018

@t1m0thyj Thanks for the code, this turns my AIY from box that you can speak to into awesome DJ that can play any song you can think of.

Do you know how to make the song restart without downloading it again? I tried vlc_player.play() but that just unpauses it if it's playing and does nothing if it has already fiished.

Copy link

ghost commented Sep 22, 2018

@t1m0thyj Also, is there a way to tell when the player has finished playing a song?

@AADARSHTRIPATHI
Copy link

AADARSHTRIPATHI commented Oct 2, 2018

@J0hnL0cke please help !!! how to add these codes in raspberry pi !! i am a beginner and i don't know how to do this.I am done with the google assistant but i don't know who to add this one
please email me n tell me how to do this: tripathi.adi007@gmail.com

@surak
Copy link

surak commented May 22, 2020

@J0hnL0cke please help !!! how to add these codes in raspberry pi !! i am a beginner and i don't know how to do this.I am done with the google assistant but i don't know who to add this one
please email me n tell me how to do this: tripathi.adi007@gmail.com

pip3 install youtube_dl
pip3 install python_vlc

@lordjoo
Copy link

lordjoo commented Nov 27, 2020

Awesome work but I'm wondering if i said anything else like " what's the weather " will it respond with the weather ?

@t1m0thyj
Copy link
Author

Awesome work but I'm wondering if i said anything else like " what's the weather " will it respond with the weather ?

@lordjoo Yes it should, the commands defined in the code above are not the only commands supported, they are overrides that replace Google Assistant's default behavior.

@RubyGamerX600
Copy link

I really need help! I'm terrible at python, so please excuse me if this is just a dumb mistake, but first of all, I had to remove aiy.voicehat and replace aiy.audio with aiy.board (I think that's the right API?), but every time that I run it, it reads everything until line 130, and gives me the error:

Traceback (most recent call last): File "./assistant.py", line 137, in <module> main() File "./assistant.py", line 130, in main device_id, model_id = aiy.assistant.device_helpers.get_ids(credentials) AttributeError: module 'aiy.assistant.device_helpers' has no attribute 'get_ids'. PLEASE HELP, because I spent $60 on a voice kit, thinking that YouTube Music was built in and the Assistant was just like the one on my Chromebook, but apparently not D:

@RubyGamerX600
Copy link

Also, I renamed the file to assistant.py so that's what that is

@t1m0thyj
Copy link
Author

@RubyGamerX600 This script used to work for me with google-assistant-library==0.0.2 as mentioned at the top of the file, but that version is now several years old and I don't know if it still works. Since then I no longer use my VoiceKit and it looks like google-assistant-library has been deprecated (see here) so you may need to find an alternative.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment