Skip to content

Commit

Permalink
Reduced the youtube processing time 90% (#391)
Browse files Browse the repository at this point in the history
* Reduced the youtube processing time 90%

* Updated youtube player

* Update

* Travis fix

* Update music_server.py

* Update auto_boot.sh

* Updated modules in requirements file

* Added vlc in install file

* Update music_server.py

* Update install.sh
  • Loading branch information
sansyrox committed Nov 3, 2018
1 parent a38b1f9 commit 71845a3
Show file tree
Hide file tree
Showing 6 changed files with 82 additions and 13 deletions.
10 changes: 10 additions & 0 deletions Deploy/Systemd/ss-susi-youtube.service
@@ -0,0 +1,10 @@
[Unit]
Description=Python Server for SUSI Linux
After=multi-user.target

[Service]
Type=simple
ExecStart=/usr/bin/python3 /home/pi/SUSI.AI/susi_linux/youtube_player/music_server.py

[Install]
WantedBy=multi-user.target
1 change: 1 addition & 0 deletions Deploy/auto_boot.sh
Expand Up @@ -10,3 +10,4 @@ cp $DIR_PATH/Systemd/ss-*.service /lib/systemd/system/
systemctl enable ss-update-daemon.service
systemctl enable ss-susi-server.service
systemctl enable ss-python-flask.service
systemctl enable ss-susi-youtube.service
2 changes: 1 addition & 1 deletion install.sh
Expand Up @@ -13,7 +13,7 @@ install_debian_dependencies()
{
sudo -E apt-get install -y python3-pip sox libsox-fmt-all flac \
libportaudio2 libatlas3-base libpulse0 libasound2 \
python3-cairo python3-flask mpv flite ca-certificates-java pixz udisks2
python3-cairo python3-flask flite ca-certificates-java pixz udisks2 vlc-nox \
# We specify ca-certificates-java instead of openjdk-(8/9)-jre-headless, so that it will pull the
# appropriate version of JRE-headless, which can be 8 or 9, depending on ARM6 or ARM7 platform.
# libatlas3-base is to provide libf77blas.so, liblapack_atlas.so for snowboy.
Expand Down
14 changes: 2 additions & 12 deletions main/states/busy_state.py
Expand Up @@ -8,6 +8,7 @@
from ..hotword_engine.stop_detection import StopDetector
import signal
from ..speech import TTS
import requests


class BusyState(State):
Expand All @@ -20,14 +21,6 @@ def detection(self):
"""
# subprocess.call(['killall', 'play'])
# subprocess.call(['killall', 'mpv']
if hasattr(self, 'video_process'):
self.video_process.send_signal(signal.SIGSTOP) # nosec #pylint-disable type: ignore
lights.off()
lights.wakeup()
subprocess.Popen(['play', str(self.components.config['detection_bell_sound'])]) # nosec #pylint-disable type: ignore
lights.off()
self.transition(self.allowedStateTransitions.get('recognizing'))
self.video_process.send_signal(signal.SIGCONT) # nosec #pylint-disable type: ignore
if hasattr(self, 'audio_process'):
self.audio_process.send_signal(signal.SIGSTOP) # nosec #pylint-disable type: ignore
lights.off()
Expand Down Expand Up @@ -70,10 +63,7 @@ def on_enter(self, payload=None):
stopAction = StopDetector(self.detection)
if classifier[:3] == 'ytd':
video_url = reply['identifier']
video_process = subprocess.Popen(['mpv', '--no-video', 'https://www.youtube.com/watch?v=' + video_url[4:], '--really-quiet']) # nosec #pylint-disable type: ignore
self.video_process = video_process
stopAction.run()
stopAction.detector.terminate()
requests.get('http://localhost:7070/song/' + video_url)
else:
audio_url = reply['identifier']
audio_process = subprocess.Popen(['play', audio_url[6:], '--no-show-progress']) # nosec #pylint-disable type: ignore
Expand Down
2 changes: 2 additions & 0 deletions requirements-hw.txt
Expand Up @@ -12,6 +12,8 @@ pyalsaaudio
spidev
RPi.GPIO
youtube-dl>2018
python-vlc
pafy

# Repo for wheel packages, pre-compiled for ARM
--extra-index-url https://repo.fury.io/fossasia/
66 changes: 66 additions & 0 deletions youtube_player/music_server.py
@@ -0,0 +1,66 @@
from flask import Flask , render_template , request
from flask import jsonify
import pafy
import vlc

app = Flask(__name__)
Instance = vlc.Instance('--no-video')
player = Instance.media_player_new()
url = ''

@app.route('/')
def index():
return render_template('index.html')

@app.route('/song', methods=['GET'])
def youtube():
vid = request.args.get('vid')
url = 'https://www.youtube.com/watch?v=' + vid
video = pafy.new(url)
streams = video.audiostreams
best = streams[3]
playurl = best.url
Media = Instance.media_new(playurl)
Media.get_mrl()
player.set_media(Media)
player.play()
display_message = {"song":"started"}
resp = jsonify(display_message)
resp.status_code = 200
return resp

@app.route('/pause')
def pause():
player.pause()
display_message = {"song":"paused"}
resp = jsonify(display_message)
resp.status_code = 200
return resp

@app.route('/stop')
def stop():
player.stop()
display_message = {"song":"stopped"}
resp = jsonify(display_message)
resp.status_code = 200
return resp

@app.route('/restart')
def restart():
player.stop()
player.player()
display_message = {"song":"restarted"}
resp = jsonify(display_message)
resp.status_code = 200
return resp

@app.route('/resume')
def play():
player.play()
display_message = {"song":"played"}
resp = jsonify(display_message)
resp.status_code = 200
return resp

if __name__ == '__main__':
app.run(debug=false, port=7070, host='0.0.0.0')

0 comments on commit 71845a3

Please sign in to comment.