-
Notifications
You must be signed in to change notification settings - Fork 2
/
Youtube.py
292 lines (258 loc) · 12.2 KB
/
Youtube.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
import urllib.request
import re
import PySimpleGUI as sg
import vlc
import os
from sys import platform as PLATFORM
import speech_recognition as sr
import playsound2 as playsound # to play saved mp3 file
from gtts import gTTS # google text to speech
import os # to save/open files
from pytube import YouTube
sg.theme('SystemDefault')
def btn1(name):
return sg.Button(name, size=(20, 3), pad=(1, 1))
layout1 = [[sg.Text(' Choose your preferred interface type', size=(30, 1), font=("Helvetica", 25), text_color='black')],
[btn1('Voice'), btn1('Mouse')],
]
window1 = sg.Window('Ad-free Youtube Player', layout=layout1, element_justification='center', finalize=True, resizable=True, keep_on_top = True)
window1.Normal()
window1.Minimize()
window1.Normal()
window1.TKroot.focus_force()
def get_subtitles(link):
if os.path.exists("Captions.srt"):
os.remove("Captions.srt")
source = YouTube(link)
en_caption = source.captions.get_by_language_code('en')
try:
en_caption_convert_to_srt =(en_caption.generate_srt_captions())
text_file = open("Captions.srt", "w")
text_file.write(en_caption_convert_to_srt)
text_file.close()
except:
pass
def download_video_youtube(link,path,search):
name =search #Name to be given to the downloaded file
try:
yt_obj = YouTube(link)
filters = yt_obj.streams.filter(progressive=True, file_extension='mp4')
#print("Downloading...")
filters.get_highest_resolution().download(filename=name)
#print('Video Downloaded Successfully')
except Exception as e:
pass
while True:
event, values = window1.read(timeout=1000)
if(event=='Voice'):
window1.close()
num = 1
def assistant_speaks(output):
global num
num += 1
toSpeak = gTTS(text = output, lang ='en', slow = False)
file = str(num)+".mp3"
toSpeak.save(file)
playsound.playsound(file, True)
os.remove(file)
def get_audio(window):
rObject = sr.Recognizer()
audio = ''
with sr.Microphone() as source:
audio = rObject.listen(source, phrase_time_limit = 5)
try:
text = rObject.recognize_google(audio, language ='en-US')
return text
except:
return 0
#------- GUI definition & setup --------#
def btn(name):
return sg.Button(name, size=(7, 1), pad=(1, 1))
layout = [[sg.Text('Speak Something')],
[sg.Input(default_text='', size=(30, 1), key='-VIDEO_LOCATION-')],
[sg.Image('', size=(300, 170), key='-VID_OUT-')],
[sg.Text('Commands- play, pause, stop, fullcreen')],
[sg.Text('Normal, mute, unmute, subtitles, download')],
[sg.Text('Load media to start', key='-MESSAGE_AREA-')]]
window = sg.Window('Ad-free Youtube Player', layout, element_justification='center', finalize=True, resizable=True, keep_on_top = True)
window.Normal()
window.Minimize()
window.Normal()
window.TKroot.focus_force()
window.Element('-VIDEO_LOCATION-').focus=True
window.Element('-VIDEO_LOCATION-').SetFocus()
window['-VID_OUT-'].expand(True, True)
#------------ Media Player Setup ---------#
inst = vlc.Instance()
list_player = inst.media_list_player_new()
media_list = inst.media_list_new([])
media_player = inst.media_player_new()
list_player.set_media_list(media_list)
player = list_player.get_media_player()
if PLATFORM.startswith('linux'):
player.set_xwindow(window['-VID_OUT-'].Widget.winfo_id())
else:
player.set_hwnd(window['-VID_OUT-'].Widget.winfo_id())
t=1
link=''
que1=''
#------------ The Event Loop ------------#
while True:
if player.is_playing():
window['-MESSAGE_AREA-'].update("{:02d}:{:02d} / {:02d}:{:02d}".format(*divmod(player.get_time()//1000, 60),
*divmod(player.get_length()//1000, 60)))
else:
window['-MESSAGE_AREA-'].update('Load media to start' if media_list.count() == 0 else 'Ready to play media' )
if(t==1):
assistant_speaks("Speak Something")
t=0
event, values = window.read(timeout=1000)
search = get_audio(window)
if event == sg.WIN_CLOSED or search == "exit" or event=='Cancel':
list_player.pause()
list_player.stop()
break
# buttons
if(search!=0):
assistant_speaks("I Heard " + search + '.')
search = search.lower()
if event=='play' or (search.find("play") != -1):
list_player.play()
if event=='download' or (search.find("download") != -1):
if(link!=''):
download_video_youtube(link,'',que1)
if (search.find("full screen") != -1):
window.Maximize()
if (search.find("normal") != -1):
window.Normal()
if (search.find("minimize") != -1 or search.find("minimise") != -1):
window.Minimize()
if (search.find("unmute") != -1) or event=='unmute':
if player.audio_get_mute() == True:
player.audio_set_mute(False)
if ((search.find('unmute') == -1)and(search.find("mute") != -1)) or event=='mute':
if player.audio_get_mute() == False:
player.audio_set_mute(True)
if event=='pause' or (search.find("pause") != -1):
list_player.pause()
if (search.find("stop") != -1) or event=='stop':
list_player.stop()
if (search.find("subtitle") != -1):
player.video_set_subtitle_file('Captions.srt')
player.video_set_spu(0)
if(search==0):
continue
if ((search.find("play") != -1) or (search.find("pause") != -1) or (search.find("stop") != -1) or (search.find("mute") != -1)):
continue
txt = search
query = ""
txt = txt.strip()
if len(txt)>7:
res = txt.find("search")
if res == 0:
que = txt[7:]
que1=que
que = que.replace(" ", "+")
searchlink = "https://www.youtube.com/results?search_query="+que
#WEB SCRAPING
html = urllib.request.urlopen(searchlink)
video_ids = re.findall(r"watch\?v=(\S{11})", html.read().decode())
link="https://www.youtube.com/watch?v=" + video_ids[0]
values['-VIDEO_LOCATION-']=link
list_player.stop()
player.stop()
get_subtitles(link)
if values['-VIDEO_LOCATION-'] and not 'Video URL' in values['-VIDEO_LOCATION-']:
a = values['-VIDEO_LOCATION-']
if(media_list.count()==1):
media_list.remove_index(0)
media_list.add_media(values['-VIDEO_LOCATION-'])
list_player.set_media_list(media_list)
window['-VIDEO_LOCATION-'].update(que1)
list_player.play()
window.close()
break
if(event=='Mouse'):
window1.close()
def btn(name): # a PySimpleGUI "User Defined Element" (see docs)
return sg.Button(name, size=(7, 1), pad=(1, 1))
layout = [[sg.Text('Enter Youtube Search', font=("Helvetica", 10), text_color='black')],
[sg.Input(default_text='', size=(30, 1), key='-VIDEO_LOCATION-'), sg.Button('load')],
[sg.Image('', size=(300, 170), key='-VID_OUT-')],
[btn('play'), btn('pause'), btn('stop')],
[btn('mute'), btn('subtitles'), btn('download')],
[sg.Text('Load media to start', key='-MESSAGE_AREA-')]]
window = sg.Window('Ad-free Youtube Player', layout, element_justification='center', finalize=True, resizable=True, keep_on_top = True)
window.Normal()
#window.Maximize()
window.Minimize()
window.Normal()
window.TKroot.focus_force()
window.Element('-VIDEO_LOCATION-').focus=True
window.Element('-VIDEO_LOCATION-').SetFocus()
window['-VID_OUT-'].expand(True, True) # type: sg.Element
#------------ Media Player Setup ---------#
inst = vlc.Instance()
list_player = inst.media_list_player_new()
media_list = inst.media_list_new([])
media_player = inst.media_player_new()
list_player.set_media_list(media_list)
player = list_player.get_media_player()
if PLATFORM.startswith('linux'):
player.set_xwindow(window['-VID_OUT-'].Widget.winfo_id())
else:
player.set_hwnd(window['-VID_OUT-'].Widget.winfo_id())
link=''
que=' '
#------------ The Event Loop ------------#
while True:
event, values = window.read(timeout=1000) # run with a timeout so that current location can be updated
if event == sg.WIN_CLOSED or event=='Cancel':
list_player.pause()
list_player.stop()
break
if event == 'play':
list_player.play()
if event == 'pause':
list_player.pause()
if event == 'stop':
list_player.stop()
print('stop')
if event == 'mute':
if player.audio_get_mute() == False:
player.audio_set_mute(True)
else:
player.audio_set_mute(False)
if event == 'download':
if(link!=''):
download_video_youtube(link,'',que1)
if event == 'subtitles':
player.video_set_subtitle_file('Captions.srt')
player.video_set_spu(0)
if event == 'load':
list_player.stop()
player.stop()
if values['-VIDEO_LOCATION-'] and not 'Video URL' in values['-VIDEO_LOCATION-']:
que = values['-VIDEO_LOCATION-']
que1 = que.replace(" ", "+")
searchlink = "https://www.youtube.com/results?search_query="+que1
#WEB SCRAPING
html = urllib.request.urlopen(searchlink)
video_ids = re.findall(r"watch\?v=(\S{11})", html.read().decode())
link="https://www.youtube.com/watch?v=" + video_ids[0]
values['-VIDEO_LOCATION-']=link
if(media_list.count()==1):
media_list.remove_index(0)
media_list.add_media(values['-VIDEO_LOCATION-'])
list_player.set_media_list(media_list)
window['-VIDEO_LOCATION-'].update(que) # only add a legit submit
get_subtitles(link)
if player.is_playing():
window['-MESSAGE_AREA-'].update("{:02d}:{:02d} / {:02d}:{:02d}".format(*divmod(player.get_time()//1000, 60),
*divmod(player.get_length()//1000, 60)))
else:
window['-MESSAGE_AREA-'].update('Load media to start' if media_list.count() == 0 else 'Ready to play media' )
window.close()
break
if(event == sg.WIN_CLOSED):
break