前段時(shí)間,接到一個(gè)需求,要求下載某一個(gè)網(wǎng)站的視頻,然后自己從網(wǎng)上查閱了相關(guān)的資料,在這里做一個(gè)總結(jié)。
1. m3u8文件
m3u8是蘋果公司推出一種視頻播放標(biāo)準(zhǔn),是一種文件檢索格式,將視頻切割成一小段一小段的ts格式的視頻文件,然后存在服務(wù)器中(現(xiàn)在為了減少I/o訪問次數(shù),一般存在服務(wù)器的內(nèi)存中),通過m3u8解析出來路徑,然后去請(qǐng)求,是現(xiàn)在比較流行的一種加載方式。目前,很多新聞視頻網(wǎng)站都是采用這種模式去加載視頻。
M3U8文件是指UTF-8編碼格式的M3U文件。M3U文件是記錄了一個(gè)索引純文本文件,打開它時(shí)播放軟件并不是播放它,而是根據(jù)它的索引找到對(duì)應(yīng)的音視頻文件的網(wǎng)絡(luò)地址進(jìn)行在線播放。原視頻數(shù)據(jù)分割為很多個(gè)TS流,每個(gè)TS流的地址記錄在m3u8文件列表中。
下面就是m3u8文件的格式。
#EXTM3U
#EXT-X-VERSION:3
#EXT-X-MEDIA-SEQUENCE:0
#EXT-X-ALLOW-CACHE:YES
#EXT-X-TARGETDURATION:15
#EXTINF:6.916667,
out000.ts
#EXTINF:10.416667,
out001.ts
#EXTINF:10.416667,
out002.ts
#EXTINF:1.375000,
out003.ts
#EXTINF:1.541667,
out004.ts
#EXTINF:7.666667,
out005.ts
#EXTINF:10.416667,
2. ts文件處理
只有m3u8文件,需要下載ts文件
ts文件能正常播放,但太多而小,需要合并 有ts文件
但因?yàn)楸患用軣o法播放,需要解碼
在這里我只記錄下前兩個(gè)步驟,因?yàn)椋夷壳把芯康谋容^少,還沒有遇到ts被加密的情況。
3. 分析舉例
那么下面,我就正式舉一個(gè)網(wǎng)站,第一財(cái)經(jīng)網(wǎng)(直接點(diǎn)擊)跟大家正式的講解下。
這是該網(wǎng)站的視頻。如下圖:
點(diǎn)擊第一個(gè)視頻,這就是我們這次要爬取的視頻。
然后鼠標(biāo)右鍵點(diǎn)擊,選擇"檢查" 或者按F12鍵,進(jìn)入開發(fā)者模式,查看網(wǎng)頁代碼。
然后,點(diǎn)擊Network ,再點(diǎn)擊other,尋找請(qǐng)求地址中帶有m3u8和ts標(biāo)記的請(qǐng)求地址。
不懂,請(qǐng)看下圖。有一點(diǎn),很重要。網(wǎng)站通過切割后ts加載視頻,并不是沒有規(guī)律的,而是通過m3u8文件附帶的。也就說,網(wǎng)站一定是先加載m3u8文件,然后根據(jù)m3u8文件,去請(qǐng)求ts文件。所以,如果你找不到m3u8文件的話,你可以先找第一個(gè)ts文件,然后往上面翻,一定能找到m3u8文件。
再點(diǎn)擊這個(gè)m3u8文件,右側(cè)對(duì)應(yīng)的就是它的請(qǐng)求地址。
請(qǐng)求地址如下:
https://ycalvod.yicai.com/record/live/cbn/ca233887-1443-4bdf-b762-3b4b3a217085_LD.m3u8?auth_key=1575703722-0-0-6f09e9a156491f027a035e31c238c48cycfrom=yicaiwww
你可以把上面那個(gè)地址,輸入瀏覽器地址框內(nèi),下載下來。也可以通過查看源碼,找到該功能的對(duì)應(yīng)的html代碼。
這是下載下來的m3u8文件。
從圖片可以看出來,每一個(gè)ts文件都是相對(duì)的地址,所以下面我們就需要找到絕對(duì)地址。
ts文件地址如下:
https://ycalvod.yicai.com/record/live/cbn_yld/1575111614_3446078.ts
上面,我們已經(jīng)把這個(gè)網(wǎng)站的視頻加載模式分析的很透徹,下面就開始擼代碼了。
4. 獲取ts文件
def getTsUrl():
ts_url_list = []
baseUrl = "https://ycalvod.yicai.com/record/live"
with open("ca233887-1443-4bdf-b762-3b4b3a217085_LD.m3u8", "r", encoding="utf-8") as f:
m3u8Contents = f.readlines()
for content in m3u8Contents:
if content.endswith("ts\n"):
ts_Url = baseUrl + content.replace("\n", "").replace("..", "")
ts_url_list.append(ts_Url)
print(ts_Url)
return ts_url_list
5. 下載ts文件
def download_ts_video(download_path, ts_url_list):
download_path = r"C:\Users\Administrator\Desktop\AiShu\下載視頻\TS視頻"
for i in range(len(ts_url_list)):
ts_url = ts_url_list[i]
try:
response = requests.get(ts_url, stream=True, verify=False)
except Exception as e:
print("異常請(qǐng)求:%s" % e.args)
return
ts_path = download_path + "\{}.ts".format(i)
with open(ts_path, "wb+") as file:
for chunk in response.iter_content(chunk_size=1024):
if chunk:
file.write(chunk)
print("TS文件下載完畢?。?)
這就是我本地下載好的ts切割視頻
6. 合并TS視頻
def heBingTsVideo(download_path,hebing_path):
all_ts = os.listdir(download_path)
with open(hebing_path, 'wb+') as f:
for i in range(len(all_ts)):
ts_video_path = os.path.join(download_path, all_ts[i])
f.write(open(ts_video_path, 'rb').read())
print("合并完成?。?)
最后的結(jié)果如下:
7. 完整的代碼
有興趣的小伙伴,可以研究下。
import requests,os
def getTsUrl():
ts_url_list = []
baseUrl = "https://ycalvod.yicai.com/record/live"
with open("ca233887-1443-4bdf-b762-3b4b3a217085_LD.m3u8", "r", encoding="utf-8") as f:
m3u8Contents = f.readlines()
for content in m3u8Contents:
if content.endswith("ts\n"):
ts_Url = baseUrl + content.replace("\n", "").replace("..", "")
ts_url_list.append(ts_Url)
print(ts_Url)
return ts_url_list
def download_ts_video(download_path, ts_url_list):
download_path = r"C:\Users\Administrator\Desktop\AiShu\下載視頻\TS視頻"
for i in range(len(ts_url_list)):
ts_url = ts_url_list[i]
try:
response = requests.get(ts_url, stream=True, verify=False)
except Exception as e:
print("異常請(qǐng)求:%s" % e.args)
return
ts_path = download_path + "\{}.ts".format(i)
with open(ts_path, "wb+") as file:
for chunk in response.iter_content(chunk_size=1024):
if chunk:
file.write(chunk)
print("TS文件下載完畢??!")
def heBingTsVideo(download_path,hebing_path):
all_ts = os.listdir(download_path)
with open(hebing_path, 'wb+') as f:
for i in range(len(all_ts)):
ts_video_path = os.path.join(download_path, all_ts[i])
f.write(open(ts_video_path, 'rb').read())
print("合并完成!!")
if __name__ == '__main__':
download_path = r"C:\Users\Administrator\Desktop\AiShu\下載視頻\TS視頻"
hebing_path = r"C:\Users\Administrator\Desktop\AiShu\下載視頻\合并TS視頻\第一財(cái)經(jīng).mp4"
ts_url_list = getTsUrl()
download_ts_video(download_path, ts_url_list)
heBingTsVideo(download_path,hebing_path)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教。
您可能感興趣的文章:- Python解析m3u8拼接下載mp4視頻文件的示例代碼
- python將下載到本地m3u8視頻合成MP4的代碼詳解
- python3.6根據(jù)m3u8下載mp4視頻
- python實(shí)現(xiàn)m3u8格式轉(zhuǎn)換為mp4視頻格式
- Python合并ts文件至mp4格式及解密教程詳解