forked from Taysser-Gherfal/YouTube-Download
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathModel.py
More file actions
111 lines (92 loc) · 4.21 KB
/
Copy pathModel.py
File metadata and controls
111 lines (92 loc) · 4.21 KB
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
import sys
import os
from PyQt5 import QtWidgets as qtw
from PyQt5 import QtGui as qtg
from PyQt5 import QtCore as qtc
from pytube import YouTube
import youtube_dl
#import mpv
class Model(qtc.QObject):
previewing = qtc.pyqtSignal(dict)
status = qtc.pyqtSignal(float)
items = []
def preview(self, e):
self.items.clear()
try:
youtube = YouTube(str(e))
stream = youtube.streams[0]
for i in youtube.streams.filter(progressive=True).all():
self.items.append("Type: Video & Audio" + " - " + "Size: " + str(round(int(i.filesize) / 1000000, 2)) + "MB")
for i in youtube.streams.filter(adaptive=True).all():
self.items.append("Type: " + str(i.type) + " only" + " - " + "Size: " + str(round(int(i.filesize) / 1000000, 2)) + "MB")
video_data = {
"title": stream.title,
"length": round(int(youtube.length) /60, 2),
"description": youtube.description,
"items": self.items,
"status": "pytube"
}
except:
try:
ydl_opts = {}
with youtube_dl.YoutubeDL(ydl_opts) as ydl:
meta = ydl.extract_info(str(e), download=False)
for i in meta['formats']:
try:
if i['filesize']:
size = str(round(int(i['filesize']) / 1000000, 2))
else:
size = "Unknown!"
self.items.append("Type: " + i['acodec'] + " - " + i['format'] + " - " + i['ext'] + " - " + "Size: " + size + "MB" + " / " + i['format_id'])
#self.items.append("Type: " + i['acodec'] + " - " + i['format'] + " - " + i['ext'] + " - " + "Size: " + str(round(int(i['filesize']) / 1000000, 2)) + "MB" + " / " + i['format_id'])
except:
continue
video_data = {
"title": meta['title'],
"length": round(int(meta['duration'])/60, 2),
"description": meta['description'],
"items": self.items,
"status": "youtube_dl"
}
except:
video_data = {
"title" : "Error!",
"length" : 0,
"description" : "Wasn't able to preview data from this URL.",
"items": ["Not supported for this video!"],
"status" : "Error!"
}
self.previewing.emit(video_data)
def setProgressVal(self, chunk, file_handle, bytes_remaining):
percentage = 1 - bytes_remaining / self.size
self.status.emit(percentage*100)
def my_hook(self, d):
if d['status'] == 'finished':
file_tuple = os.path.split(os.path.abspath(d['filename']))
if d['status'] == 'downloading':
p = d['_percent_str']
p = p.replace('%','')
size = round(int(d['total_bytes']) / 1000000, 2)
self.status.emit(float(p))
def download(self, url, folder, library, selected=0):
# removing attached list if found
result = url.find("&list=")
if result != -1:
url = url[:result]
if library == "pytube":
self.size = YouTube(url).streams[selected].filesize
YouTube(url, on_progress_callback=self.setProgressVal).streams[selected].download(folder)
elif library == "youtube_dl":
select = self.items[selected]
left, right = select.split('/')
ydl_opts = {'progress_hooks': [self.my_hook], 'outtmpl': folder + '/' + '%(title)s.%(ext)s', 'format': right.lstrip(),}
#ydl_opts = {'progress_hooks': [self.my_hook], 'outtmpl': folder + '/' + '%(title)s', 'format': 'worst',}
with youtube_dl.YoutubeDL(ydl_opts) as ydl:
ydl.download([url])
def play(self, url):
os.system("mpv "+url)
'''
player = mpv.MPV(ytdl=True)
player.play(url)
player.wait_for_playback()
'''