From 852b698eda4444190f05a146e23d83b37b64f58a Mon Sep 17 00:00:00 2001 From: CitingNutria <37170862+CitingNutria@users.noreply.github.com> Date: Wed, 16 Jun 2021 15:51:50 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E9=98=9F=E5=88=97=E4=B8=8B?= =?UTF-8?q?=E8=BD=BD=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- main.py | 155 +++++++++++++++++++++++++++++--------------------------- 1 file changed, 79 insertions(+), 76 deletions(-) diff --git a/main.py b/main.py index 8180a1f..b267b7c 100644 --- a/main.py +++ b/main.py @@ -17,108 +17,111 @@ from merge import mergeMp4 from delete import deleteM3u8, deleteMp4 -# In[2]: +#选择器 +print("按1下载单个视频,按2进行多个队列下载") +selecter = input("输入1或2:") +if(selecter == '1'): + url = input('輸入jable網址:') +if(selecter == '2'): + downloadlist = [] + # print("输入q以结束输入:") + while(True): + url = input('輸入jable網址(輸入q以结束输入):') + if(url == 'q'): + break + downloadlist.append(url) +video_number = len(downloadlist) +print("一共"+str(video_number)+"个视频") -# Jable網址 -url = input('輸入jable網址:') +for url in downloadlist: + # In[2]: + # Jable網址 + # url = input('輸入jable網址:') -# In[3]: + # In[3]: + # 建立番號資料夾 + urlSplit = url.split('/') + dirName = urlSplit[-2] + if not os.path.exists(dirName): + os.makedirs(dirName) + folderPath = os.path.join(os.getcwd(), dirName) -# 建立番號資料夾 -urlSplit = url.split('/') -dirName = urlSplit[-2] -if not os.path.exists(dirName): - os.makedirs(dirName) -folderPath = os.path.join(os.getcwd(), dirName) + # In[4]: + # 得到 m3u8 網址 + # htmlfile = requests.get(url) + # soup = BeautifulSoup(htmlfile.text, 'lxml') + # needScript = str(soup.find_all('script')[7]) + # m3u8url = needScript.split('var hlsUrl = ')[1].split(';')[0] + # m3u8url = m3u8url[1:][:-1] -# In[4]: + htmlfile = requests.get(url) + result = re.search("https://.+m3u8", htmlfile.text) + m3u8url = result[0] + m3u8urlList = m3u8url.split('/') + m3u8urlList.pop(-1) + downloadurl = '/'.join(m3u8urlList) -# 得到 m3u8 網址 -# htmlfile = requests.get(url) -# soup = BeautifulSoup(htmlfile.text, 'lxml') -# needScript = str(soup.find_all('script')[7]) -# m3u8url = needScript.split('var hlsUrl = ')[1].split(';')[0] -# m3u8url = m3u8url[1:][:-1] + # 儲存 m3u8 file 至資料夾 + m3u8file = os.path.join(folderPath, dirName + '.m3u8') + urllib.request.urlretrieve(m3u8url, m3u8file) -htmlfile = requests.get(url) -result = re.search("https://.+m3u8", htmlfile.text) -m3u8url = result[0] + # In[5]: -m3u8urlList = m3u8url.split('/') -m3u8urlList.pop(-1) -downloadurl = '/'.join(m3u8urlList) + # 得到 m3u8 file裡的 URI和 IV + m3u8obj = m3u8.load(m3u8file) + m3u8uri = '' + m3u8iv = '' + for key in m3u8obj.keys: + if key: + m3u8uri = key.uri + m3u8iv = key.iv -# 儲存 m3u8 file 至資料夾 -m3u8file = os.path.join(folderPath, dirName + '.m3u8') -urllib.request.urlretrieve(m3u8url, m3u8file) + # 儲存 ts網址 in tsList + tsList = [] + for seg in m3u8obj.segments: + tsUrl = downloadurl + '/' + seg.uri + tsList.append(tsUrl) + # In[6]: -# In[5]: + # 有加密 + if m3u8uri: + m3u8keyurl = downloadurl + '/' + m3u8uri # 得到 key 的網址 + # 得到 key的內容 + response = requests.get(m3u8keyurl, headers=headers, timeout=10) + contentKey = response.content -# 得到 m3u8 file裡的 URI和 IV -m3u8obj = m3u8.load(m3u8file) -m3u8uri = '' -m3u8iv = '' + vt = m3u8iv.replace("0x", "")[:16].encode() # IV取前16位 -for key in m3u8obj.keys: - if key: - m3u8uri = key.uri - m3u8iv = key.iv + ci = AES.new(contentKey, AES.MODE_CBC, vt) # 建構解碼器 + else: + ci = '' -# 儲存 ts網址 in tsList -tsList = [] -for seg in m3u8obj.segments: - tsUrl = downloadurl + '/' + seg.uri - tsList.append(tsUrl) + # In[7]: + # 刪除m3u8 file + deleteM3u8(folderPath) -# In[6]: + # In[8]: + # 開始爬蟲並下載mp4片段至資料夾 + prepareCrawl(ci, folderPath, tsList) -# 有加密 -if m3u8uri: - m3u8keyurl = downloadurl + '/' + m3u8uri # 得到 key 的網址 + # In[9]: - # 得到 key的內容 - response = requests.get(m3u8keyurl, headers=headers, timeout=10) - contentKey = response.content + # 合成mp4 + mergeMp4(folderPath, tsList) - vt = m3u8iv.replace("0x", "")[:16].encode() # IV取前16位 + # In[10]: - ci = AES.new(contentKey, AES.MODE_CBC, vt) # 建構解碼器 -else: - ci = '' + # 刪除子mp4 + deleteMp4(folderPath) -# In[7]: - -# 刪除m3u8 file -deleteM3u8(folderPath) - - -# In[8]: - - -# 開始爬蟲並下載mp4片段至資料夾 -prepareCrawl(ci, folderPath, tsList) - - -# In[9]: - - -# 合成mp4 -mergeMp4(folderPath, tsList) - - -# In[10]: - - -# 刪除子mp4 -deleteMp4(folderPath)