生活随笔
收集整理的這篇文章主要介紹了
下载在线播放的电影,一个下载TS文件的工具,python小白。
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
在線播放的電影大多數(shù)都是可以下載的,因為懶的找資源,寫了一個下載TS文件的工具
可以用pyinstaller -F 文件名.py來生成可獨立運行的文件。
代碼如下:
import time
,asyncio
,aiohttp
,re
,os
,requests
,threading
from alive_progress
import alive_bar
class download_video():def __init__(self
,dir_name
):self
.dir_name
= dir_nameself
.headers_list
= []while True:self
.headers
= input('\n請輸入headers的信息(輸入“ok”結(jié)束輸入):')self
.headers_list
.append
(self
.headers
)if 'ok' == self
.headers
:breakself
.url
= input('\n請輸入m3u8文件的地址(必須):')self
.key_url
= input('\n請輸入key文件的地址(可選):')def str_chans_headers(self
):global headersheaders
= {}header
= self
.headers_list
for i
in header
:if 'origin'in i
:dict_h
= {'origin':i
.replace
('origin:','').strip
()}headers
.update
(dict_h
)elif 'referer'in i
:dict_h
= {'referer':i
.replace
('referer:','').strip
()}headers
.update
(dict_h
)elif i
=='ok':continueelif i
.strip
() == '':continue elif ':'in i
:dict_h
= {i
.split
(':')[0]:i
.split
(':')[-1].strip
()}headers
.update
(dict_h
)else:continuereturn headers
def get_ts_list(self
,url
,dir_name
):name
= dir_name
.split
('\\')[-1]ts_list
= []ts_html
= requests
.get
(url
=url
,headers
=headers
)ts_html
.encoding
= ts_html
.apparent_encodingts_all
= re
.findall
(r
'(.*?).ts',ts_html
.text
)for i
in ts_all
:if '/'in i
:ts_list
.append
(i
.strip
().split
('/')[-1])else:ts_list
.append
(i
)if not dir_name
+'.txt' in os
.listdir
(dir_name
):with open(dir_name
+'\\'+name
+'ts列表.txt','w')as f1
,open(dir_name
+'\\'+name
+'.m3u8','wb')as f2
:for i
in ts_list
:f1
.write
(i
+'\n')f2
.write
(ts_html
.content
)return ts_list
def get_key_file(self
,url
,dir_name
):a
= requests
.get
(url
=url
,headers
=headers
)with open(dir_name
+'\\'+'key.key','wb')as f
:f
.write
(a
.content
)async def get_ts_data(self
,ts_url
,dir_name
):async with aiohttp
.ClientSession
()as s
:async with await s
.get
(url
=ts_url
,headers
=headers
)as response
:ts_data
= await response
.read
()ts_name
= ts_url
.split
('/')[-1]with open(dir_name
+'\\'+ts_name
,'wb')as f
:f
.write
(ts_data
) def start_task(self
):headers
= self
.str_chans_headers
()print(headers
)dir_name
= self
.dir_name
print('下載的文件保存到',dir_name
,'\n')if not os
.path
.exists
(dir_name
):os
.mkdir
(dir_name
)passurl
= self
.urlkey_url
= self
.key_url
if 'http' in key_url
:self
.get_key_file
(url
=key_url
,dir_name
=dir_name
)ts_url
= key_url
.replace
('key.key','')else:ts_url
= re
.sub
(r
'/([0-9A-z]*?)\.m3u8,'',url')ts_list
= self
.get_ts_list
(url
=url
,dir_name
=dir_name
)while True:tasks
= []current_file
= os
.listdir
(dir_name
)print('\n')with alive_bar
(len(ts_list
))as bar
:loop
= asyncio
.get_event_loop
()for i
in ts_list
:if i
+'.ts' in current_file
:bar
()continuec
= self
.get_ts_data
(ts_url
=ts_url
+i
+'.ts',dir_name
=dir_name
)task
= asyncio
.ensure_future
(c
)tasks
.append
(task
)try:loop
.run_until_complete
(asyncio
.wait
(tasks
))except:print('10秒后繼續(xù)下載\n如果下載已完成,請直接關(guān)閉dos窗口\n?請不要連續(xù)Ctrl+C,否則會直接退出?')time
.sleep
(10)if __name__
== '__main__':print('***********************************************.*** 電影下載器DOS版V1.2 ***.***********************************************\n','\n')while True:dir_name
= os
.getcwd
()+'\\'+input('\n請輸入電影名稱(必須,否則文件會亂)\n如果不滿意輸入的內(nèi)容,可以按ctrl+c退出,重新打開程序:')if dir_name
.strip
() == str(os
.getcwd
()+'\\'):print('請輸入電影名稱(文件夾的名稱)')else:breakdownload_video
(dir_name
=dir_name
).start_task
()
當然,這個版本要求你至少會看瀏覽器的F12里的內(nèi)容(過兩天在更新更簡單的)。
對了,下載下來還需要合并文件。
合并ts文件的工具
總結(jié)
以上是生活随笔為你收集整理的下载在线播放的电影,一个下载TS文件的工具,python小白。的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。