-
Notifications
You must be signed in to change notification settings - Fork 0
/
helpers.py
389 lines (326 loc) · 13.1 KB
/
helpers.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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# chcp 65001 #转换为utf-8代码页
# chcp 936 #转换为默认的gb
from download_file import download_file
from pytube import YouTube
from pytube.helpers import uniqueify
from pytube.helpers import safe_filename
import sys
import os
import tempfile
import logging
import subprocess
import shutil
import platform
import zipfile
import contextlib
import lzma
import tarfile
import stat
import argparse
import ntpath
PY3K = sys.version_info >= (3, 0)
if PY3K:
import urllib.request as urllib2
import urllib.parse as urlparse
else:
import urllib2
import urlparse
logger = logging.getLogger(__name__)
def get_terminal_size_windows():
try:
from ctypes import windll, create_string_buffer
import struct
# stdin handle is -10
# stdout handle is -11
# stderr handle is -12
h = windll.kernel32.GetStdHandle(-12)
csbi = create_string_buffer(22)
res = windll.kernel32.GetConsoleScreenBufferInfo(h, csbi)
if res:
(bufx, bufy, curx, cury, wattr,
left, top, right, bottom,
maxx, maxy) = struct.unpack("hhhhHhhhhhh", csbi.raw)
sizex = right - left + 1
sizey = bottom - top + 1
return sizex, sizey
except:
pass
def get_terminal_size_stty():
try:
return map(int, subprocess.check_output(['stty', 'size']).split())
except:
pass
def get_terminal_size_tput():
try:
return map(int, [subprocess.check_output(['tput', 'lines']), subprocess.check_output(['tput', 'rows'])])
except:
pass
def get_terminal_size():
return get_terminal_size_windows() or get_terminal_size_stty() or get_terminal_size_tput() or (25, 80)
def detect_platform():
is_64bit = platform.machine().endswith('64')
arch = '64bit' if is_64bit else '32bit'
logger.info(platform.system())
logger.info(platform.release())
logger.info(platform.version())
logger.info(arch)
# logger.info("Your system is %s %s" % (platform.system(), arch))
if platform.system().lower() == "windows":
logger.info("Your system is windows %s" % arch)
elif platform.system().lower() == "linux":
logger.info("Your system is Linux %s" % arch)
logger.info(platform.linux_distribution)
elif platform.system().lower() == "darwin":
logger.info("Your system is MacOS %s" % arch)
logger.info(platform.mac_ver)
else:
logger.info("Unidentified system")
return platform.system(), arch
def fib(n):
if n == 0:
return 0
elif n == 1:
return 1
else:
return fib(n-1)+fib(n-2)
def symlink(source, link_name):
os_symlink = getattr(os, "symlink", None)
try:
os_symlink(source, link_name)
except:
try:
import ctypes
csl = ctypes.windll.kernel32.CreateSymbolicLinkW
csl.argtypes = (ctypes.c_wchar_p,
ctypes.c_wchar_p, ctypes.c_uint32)
csl.restype = ctypes.c_ubyte
flags = 1 if os.path.isdir(source) else 0
if csl(link_name, source, flags) == 0:
raise ctypes.WinError()
except:
try:
import win32file
win32file.CreateSymbolicLink(fileSrc, fileTarget, 1)
except:
print('unable to create symbolic link from [{src}] to [{dst}]'.format(
src=source, dst=link_name))
def copyfile(source, destination, skip=True):
if skip == True and os.path.isfile(source) and os.path.isfile(destination) and (os.path.getsize(source) == os.path.getsize(destination)):
pass
else:
shutil.copyfile(source, destination)
st = os.stat(source)
shutil.copymode(source, destination)
os.chown(destination, st[stat.ST_UID], st[stat.ST_GID])
# return True if skip == True and os.path.isfile(source) and os.path.isfile(destination) and (os.path.getsize(source) == os.path.getsize(destination)) else shutil.copyfile(source, destination)
def median(lst):
# sortedLst = sorted(lst)
lstLen = len(lst)
index = (lstLen - 1) // 2
if (lstLen % 2):
return lst[index]
else:
return lst[index]
def unzip_without_overwrite(src_path, dst_dir, pwd=None):
with zipfile.ZipFile(src_path) as zf:
members = zf.namelist()
for member in members:
arch_info = zf.getinfo(member)
arch_name = arch_info.filename.replace('/', os.path.sep)
dst_path = os.path.join(dst_dir, arch_name)
dst_path = os.path.normpath(dst_path)
if not os.path.exists(dst_path):
zf.extract(arch_info, dst_dir, pwd)
def filename_fix_existing(filename):
"""Expands name portion of filename with numeric ' (x)' suffix to
return filename that doesn't exist already.
"""
head, tail = ntpath.split(filename)
base = os.path.basename(filename)
name, ext = os.path.splitext(base)
if not head:
head = u'.'
try:
name, ext = tail.rsplit('.', 1)
except:
# handle those filename without extention name
name = tail.rsplit(os.sep, 1)[0]
ext = None
names = [x for x in os.listdir(head) if x.startswith(name)]
if ext:
names = [x.rsplit('.', 1)[0] for x in names]
else:
names = [x.rsplit(os.sep, 1)[0] for x in names]
suffixes = [x.replace(name, '') for x in names]
# filter suffixes that match ' (x)' pattern
suffixes = [x[2:-1] for x in suffixes
if x.startswith('_(') and x.endswith(')')]
indexes = [int(x) for x in suffixes
if set(x) <= set('0123456789')]
idx = 1
if indexes:
idx += sorted(indexes)[-1]
if ext:
out = '{0}_({1}).{2}'.format(name, idx, ext)
else:
out = '{0}_({1})'.format(name, idx)
out = os.path.join(head, out)
return out
def to_unicode(filename):
""":return: filename decoded from utf-8 to unicode"""
if PY3K:
# [ ] test this on Python 3 + (Windows, Linux)
# [ ] port filename_from_headers once this works
# [ ] add test to repository / Travis
return filename
else:
if isinstance(filename, unicode):
return filename
else:
return unicode(filename, 'utf-8')
def str2bool(v):
if v.lower() in ('yes', 'true', 't', 'y', '1'):
return True
elif v.lower() in ('no', 'false', 'f', 'n', '0'):
return False
else:
raise argparse.ArgumentTypeError('Boolean value expected.')
def download_ffmpeg(out=os.getcwd()):
platform, arch = detect_platform()
if platform.lower() == "windows":
if arch.lower() == '32bit':
ffmpeg_url = "https://ffmpeg.zeranoe.com/builds/win32/static/ffmpeg-latest-win32-static.zip"
elif arch.lower() == '64bit':
ffmpeg_url = "https://ffmpeg.zeranoe.com/builds/win64/static/ffmpeg-latest-win64-static.zip"
ffmpeg = download_file(url=ffmpeg_url, out=out)
logger.info("%s downloaded" % ffmpeg)
#unzip_without_overwrite(src_path=ffmpeg, dst_dir=out)
with zipfile.ZipFile(ffmpeg, 'r') as zip_ref:
# zip_ref.extractall(out)
for file in zip_ref.filelist:
if not os.path.exists(file.filename):
zip_ref.extract(file, out)
if file.filename.endswith("ffmpeg.exe") and (not file.is_dir()) and int(file.file_size) > 0:
ffmpeg_binary = file.filename
break
elif platform.lower() == "linux":
if arch.lower() == '32bit':
ffmpeg_url = "https://johnvansickle.com/ffmpeg/releases/ffmpeg-release-amd64-static.tar.xz"
elif arch.lower() == '64bit':
ffmpeg_url = "https://johnvansickle.com/ffmpeg/releases/ffmpeg-release-amd64-static.tar.xz"
ffmpeg = download_file(url=ffmpeg_url, out=out)
logger.info("%s downloaded" % ffmpeg)
with contextlib.closing(lzma.LZMAFile(ffmpeg)) as xz:
with tarfile.open(fileobj=xz) as f:
# f.extractall(out)
for member in f.members:
if not os.path.exists(member.name):
f.extractfile(member)
if member.name.endswith('ffmpeg') and int(member.size) > 0 and int(member.mode) == 493:
ffmpeg_binary = member.name
break
elif platform.lower() == "darwin":
ffmpeg_url = "https://ffmpeg.zeranoe.com/builds/macos64/static/ffmpeg-latest-macos64-static.zip"
ffmpeg = download_file(url=ffmpeg_url, out=out)
logger.info("%s downloaded" % ffmpeg)
#unzip_without_overwrite(src_path=ffmpeg, dst_dir=out)
with zipfile.ZipFile(ffmpeg, 'r') as zip_ref:
# zip_ref.extractall(out)
for file in zip_ref.filelist:
if not os.path.exists(file.filename):
zip_ref.extract(file, out)
if file.filename.endswith("ffmpeg") and (not file.is_dir()) and int(file.file_size) > 0:
ffmpeg_binary = file.filename
break
else:
ffmpeg_url = False
logger.error("Unsupported system")
return False
filesize = os.path.getsize(ffmpeg_binary)
logger.info("ffmpeg location on [{path}], size = [{size}]".format(
path=ffmpeg_binary, size=filesize))
return ffmpeg_binary
def ffmpeg_join_audio_video(video_path: str, audio_path: str, target: str = None, ffmpeg: str = None, skip: bool = True) -> str:
final_path = None
target = target or os.getcwd()
ffmpeg = ffmpeg or "ffmpeg"
if video_path and os.path.exists(video_path) and audio_path and os.path.exists(audio_path):
base = os.path.basename(video_path)
name, ext = os.path.splitext(base)
filename = to_unicode(safe_filename(name))
final_path = os.path.join(
target, f"{filename}_HQ{ext}"
)
if not all([os.path.exists(final_path), skip]):
if ext.lower() == '.webm':
cmd = [ffmpeg, "-i", video_path, "-i", audio_path,
"-c:v", "copy", "-c:a", "libvorbis", "-strict experimental", final_path, "-y", ]
else:
cmd = [ffmpeg, "-i", video_path, "-i", audio_path,
"-codec", "copy", final_path, "-y", ]
subprocess.call(cmd)
return final_path
def ffmpeg_aac_convert_mp3(aac: str, sampling: str = None, abr: str = None, target: str = None, ffmpeg: str = None, skip: bool = True) -> str:
final_path = None
sampling = sampling or "44100"
abr = abr or "192k"
target = target or os.getcwd()
ffmpeg = ffmpeg or "ffmpeg"
if aac and os.path.exists(aac):
base = os.path.basename(aac)
name, _ = os.path.splitext(base)
final_path = os.path.join(
target, f"{name}.mp3"
)
if not all([os.path.exists(final_path), skip]):
subprocess.call( # nosec
[ffmpeg, "-i", aac, "-vn", "-ar",
sampling, "-ac", "2", "-b:a", abr, final_path, "-y", ]
)
return final_path
def ffmpeg_join_audio_video_ex(youtube: YouTube, resolution: str, target: str = None, ffmpeg: str = None) -> None:
"""
Decides the correct video stream to download, then calls _ffmpeg_downloader.
:param YouTube youtube:
A valid YouTube object.
:param str resolution:
YouTube video resolution.
:param str target:
Target directory for download
"""
target = target or os.getcwd()
ffmpeg = ffmpeg or "ffmpeg"
video_stream = audio_stream = None
if resolution == "best":
highest_quality_stream = (
youtube.streams.filter(progressive=False).order_by(
"resolution").last()
)
mp4_stream = (
youtube.streams.filter(progressive=False, subtype="mp4")
.order_by("resolution")
.last()
)
if highest_quality_stream.resolution == mp4_stream.resolution:
video_stream = mp4_stream
else:
video_stream = highest_quality_stream
else:
video_stream = youtube.streams.filter(
progressive=False, resolution=resolution, subtype="mp4"
).first()
if not video_stream:
video_stream = youtube.streams.filter(
progressive=False, resolution=resolution
).first()
audio_stream = youtube.streams.get_audio_only(video_stream.subtype)
if not audio_stream:
audio_stream = youtube.streams.filter(
only_audio=True).order_by("abr").last()
video_path = video_stream.download()
audio_path = audio_stream.download()
final_path = ffmpeg_join_audio_video(
video_path, audio_path, target, ffmpeg)
return video_path, audio_path, final_path