用pytube實現python下載YT影片

使用條件

電腦要裝以下軟體

1.python

2.pytube

將底下代碼貼到終端機

1
pip install pytube

程式主體

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
#程式主體和錯誤修正是由我設計、解決,chatgpt負責加入功能
from pytube import YouTube
import ssl

from pytube.innertube import _default_clients
from pytube import cipher
import re
#400錯誤解決代碼 (https://github.com/pytube/pytube/issues/1973)
_default_clients["ANDROID"]["context"]["client"]["clientVersion"] = "19.08.35"
_default_clients["IOS"]["context"]["client"]["clientVersion"] = "19.08.35"
_default_clients["ANDROID_EMBED"]["context"]["client"]["clientVersion"] = "19.08.35"
_default_clients["IOS_EMBED"]["context"]["client"]["clientVersion"] = "19.08.35"
_default_clients["IOS_MUSIC"]["context"]["client"]["clientVersion"] = "6.41"
_default_clients["ANDROID_MUSIC"] = _default_clients["ANDROID_CREATOR"]

def get_throttling_function_name(js: str) -> str:
"""Extract the name of the function that computes the throttling parameter.

:param str js:
The contents of the base.js asset file.
:rtype: str
:returns:
The name of the function used to compute the throttling parameter.
"""
function_patterns = [
r'a\.[a-zA-Z]\s*&&\s*\([a-z]\s*=\s*a\.get\("n"\)\)\s*&&\s*'
r'\([a-z]\s*=\s*([a-zA-Z0-9$]+)(\[\d+\])?\([a-z]\)',
r'\([a-z]\s*=\s*([a-zA-Z0-9$]+)(\[\d+\])\([a-z]\)',
]
#logger.debug('Finding throttling function name')
for pattern in function_patterns:
regex = re.compile(pattern)
function_match = regex.search(js)
if function_match:
#logger.debug("finished regex search, matched: %s", pattern)
if len(function_match.groups()) == 1:
return function_match.group(1)
idx = function_match.group(2)
if idx:
idx = idx.strip("[]")
array = re.search(
r'var {nfunc}\s*=\s*(\[.+?\]);'.format(
nfunc=re.escape(function_match.group(1))),
js
)
if array:
array = array.group(1).strip("[]").split(",")
array = [x.strip() for x in array]
return array[int(idx)]

raise RegexMatchError(
caller="get_throttling_function_name", pattern="multiple"
)

#ssl
cipher.get_throttling_function_name = get_throttling_function_name
ssl._create_default_https_context = ssl._create_stdlib_context
ssl._create_default_https_context = ssl._create_stdlib_context

yt_url = input("輸入網址: ")
name = input("輸入檔名: ")
file_type = input("輸入要下載 mp3 還是 mp4? ")
print("如果選擇mp3,畫質隨便填一個")
video_resolution = input("輸入畫質 (例如 1080p): ")
output_path = input("輸入下載路徑: ")

yt = YouTube(yt_url)

print("下載影片中")

if file_type == "mp4":
stream = yt.streams.filter(res=video_resolution, file_extension='mp4').first()
if stream:
stream.download(output_path=output_path, filename=f"{name}.mp4")
print("完成")
else:
print(f"沒有找到 {video_resolution} 的影片")
elif file_type == "mp3":
stream = yt.streams.filter(only_audio=True).first()
if stream:
stream.download(output_path=output_path, filename=f"{name}.mp3")
print("完成")
else:
print("未找到指定的mp3")
else:
print("無效的文件類型,請選擇 mp3 或 mp4")

可以從這下載.py檔

執行

只須在終端輸入

1
python3 YT.py 

📎 参考

  • 一些程式來自chatgpt
  • YT

https://www.youtube.com/watch?v=koq5pwvY3ww&t=15s

(注意此程式我認為過於簡單,所以我決定此程式沒有任何著作權限制)

這篇文章有幫助到你?幫我訂閱我的yt頻道吧!

贊助我?!(bitcoin:bc1q02d6mzgrm7alc7m62fw0nuj3ndx0h4mtsp6j8z)

leaftech (leaftech)

0%