youtube: Started code for playback from youtube. Not functionnal, yet

This commit is contained in:
Manuel Cortez 2018-02-16 17:58:57 -06:00
parent d4dadfcdc7
commit 5ff5a4a6d2
5 changed files with 80 additions and 18 deletions

View File

@ -28,5 +28,9 @@ def convert_soundcloud (url):
else: else:
raise TypeError('%r is not streamable' % url) raise TypeError('%r is not streamable' % url)
@matches_url ('https://www.youtube.com/watch')
def convert_long_youtube(url):
return "youtube-url"
def convert_generic_audio(url): def convert_generic_audio(url):
return url return url

View File

@ -17,6 +17,7 @@ import config
import sound import sound
import languageHandler import languageHandler
import logging import logging
import youtube_utils
from twitter import compose, utils from twitter import compose, utils
from mysc.thread_utils import call_threaded from mysc.thread_utils import call_threaded
from twython import TwythonError from twython import TwythonError
@ -623,6 +624,8 @@ class baseBufferController(bufferController):
def audio(self, url='', *args, **kwargs): def audio(self, url='', *args, **kwargs):
if hasattr(sound.URLPlayer,'stream') and sound.URLPlayer.stream.is_playing == True: if hasattr(sound.URLPlayer,'stream') and sound.URLPlayer.stream.is_playing == True:
return sound.URLPlayer.stop_audio(delete=True) return sound.URLPlayer.stop_audio(delete=True)
elif sound.URLPlayer.mode == "youtube":
return sound.URLPlayer.stop_audio()
tweet = self.get_tweet() tweet = self.get_tweet()
if tweet == None: return if tweet == None: return
urls = utils.find_urls(tweet) urls = utils.find_urls(tweet)

View File

@ -2,6 +2,7 @@
import platform import platform
system = platform.system() system = platform.system()
import application import application
import youtube_utils
if system == "Windows": if system == "Windows":
from update import updater from update import updater
from wxUI import (view, dialogs, commonMessageDialogs, sysTrayIcon) from wxUI import (view, dialogs, commonMessageDialogs, sysTrayIcon)
@ -655,6 +656,7 @@ class Controller(object):
self.exit_() self.exit_()
def exit_(self, *args, **kwargs): def exit_(self, *args, **kwargs):
youtube_utils.stop()
for i in self.buffers: i.save_positions() for i in self.buffers: i.save_positions()
log.debug("Exiting...") log.debug("Exiting...")
log.debug("Saving global configuration...") log.debug("Saving global configuration...")

View File

@ -10,6 +10,7 @@ import sound_lib
import subprocess import subprocess
import platform import platform
import output import output
import youtube_utils
system = platform.system() system = platform.system()
from mysc.repeating_timer import RepeatingTimer from mysc.repeating_timer import RepeatingTimer
from mysc.thread_utils import call_threaded from mysc.thread_utils import call_threaded
@ -99,6 +100,7 @@ class URLStream(object):
self.url = url self.url = url
self.prepared = False self.prepared = False
log.debug("URL Player initialized") log.debug("URL Player initialized")
self.mode = "generic"
def prepare(self, url): def prepare(self, url):
log.debug("Preparing URL: %s" % (url,)) log.debug("Preparing URL: %s" % (url,))
@ -109,11 +111,19 @@ class URLStream(object):
log.debug("Expanded URL: %s" % (self.url,)) log.debug("Expanded URL: %s" % (self.url,))
if self.url != None: if self.url != None:
transformer = audio_services.find_url_transformer(self.url) transformer = audio_services.find_url_transformer(self.url)
self.url = transformer(self.url) transformed_url = transformer(self.url)
if transformed_url == "youtube-url":
self.url = youtube_utils.get_video_url(url)
self.mode = "youtube"
else:
self.url = transformed_url
self.mode = "generic"
log.debug("Transformed URL: %s. Prepared" % (self.url,)) log.debug("Transformed URL: %s. Prepared" % (self.url,))
self.prepared = True self.prepared = True
def seek(self,step): def seek(self,step):
if self.mode == "youtube":
return
pos=self.stream.get_position() pos=self.stream.get_position()
pos=self.stream.bytes_to_seconds(pos) pos=self.stream.bytes_to_seconds(pos)
pos+=step pos+=step
@ -123,6 +133,11 @@ class URLStream(object):
self.stream.set_position(pos) self.stream.set_position(pos)
def playpause(self): def playpause(self):
if self.mode == "youtube":
if youtube_utils.player != None:
youtube_utils.player.kill()
self.mode = "generic"
youtube_utils.player = None
if self.stream.is_playing==True: if self.stream.is_playing==True:
self.stream.pause() self.stream.pause()
else: else:
@ -137,28 +152,37 @@ class URLStream(object):
elif stream != None: elif stream != None:
self.stream=stream self.stream=stream
if self.prepared == True: if self.prepared == True:
self.stream = sound_lib.stream.URLStream(url=self.url) if self.mode == "youtube":
if hasattr(self,'stream'): youtube_utils.play_video(self.url)
self.stream.volume = float(volume) else:
self.stream.play() self.stream = sound_lib.stream.URLStream(url=self.url)
log.debug("played") if hasattr(self,'stream'):
self.prepared=False self.stream.volume = float(volume)
self.stream.play()
log.debug("played")
self.prepared=False
def stop_audio(self,delete=False): def stop_audio(self,delete=False):
if hasattr(self, "stream"): if self.mode == "youtube":
youtube_utils.stop()
self.prepared = False
output.speak(_(u"Stopped."), True) output.speak(_(u"Stopped."), True)
try:
self.stream.stop()
log.debug("Stopped audio stream.")
except:
log.exception("Exception while stopping stream.")
if delete:
del self.stream
log.debug("Deleted audio stream.")
self.prepared=False
return True return True
else: else:
return False if hasattr(self, "stream"):
output.speak(_(u"Stopped."), True)
try:
self.stream.stop()
log.debug("Stopped audio stream.")
except:
log.exception("Exception while stopping stream.")
if delete:
del self.stream
log.debug("Deleted audio stream.")
self.prepared=False
return True
else:
return False
@staticmethod @staticmethod
def delete_old_tempfiles(): def delete_old_tempfiles():

29
src/youtube_utils.py Normal file
View File

@ -0,0 +1,29 @@
# -*- coding: utf-8 -*-
import subprocess
import youtube_dl
player = None # Let's control player from here
def get_video_url(url):
ydl = youtube_dl.YoutubeDL({'quiet': True, 'format': '251', 'outtmpl': u'%(id)s%(ext)s'})
with ydl:
result = ydl.extract_info(url, download=False)
if 'entries' in result:
video = result['entries'][0]
else:
video = result
return video["url"]
def play_video(url):
global player
if player != None:
player.kill()
player = None
player = subprocess.Popen(["ffplay.exe", "-i", url, "-nodisp", "-vn", "-hide_banner"], stderr=subprocess.PIPE, stdout=subprocess.PIPE, stdin=subprocess.PIPE)
return player
def stop():
global player
if player != None:
player.kill()
player = None