最近聽了一個課程,視訊隻能線上看,播放聲音很小,機智的我決定把視訊抓下來。
首先在網頁視訊右鍵-稽核元素-把m3u8的路徑複制下來,然後路徑放到下面的python執行(python3以上版本的),最後再用格式工廠把視訊聲音增大,視訊大小限制(不要超過原視訊大小),完美解決問題。如果代碼改進可以運用到爬蟲抓取。
python示例代碼如下:# !/user/bin/env python
# -*- coding: utf-8 -*-
import os, shutil
import urllib.request, urllib.error, requests
# 打開并讀取網頁内容
def getUrlData(url):
try:
urlData = urllib.request.urlopen(url, timeout=20) # .read().decode('utf-8', 'ignore')
# urlData = requests.get(url, timeout=20) # .read().decode('utf-8', 'ignore')
return urlData
except Exception as err:
print('err getUrlData({url})\n'.format(url), err)
return -1
# 下載下傳檔案-requests
def getDown_reqursts(url, file_path):
try:
header = {"User-Agent":"Mozilla/5.0 (Windows NT 6.1; rv:2.0.1) Gecko/20100101 Firefox/4.0.1"}
response = requests.get(url, timeout=120, headers=header)
with open(file_path, mode='ab+') as f:
f.write(response.content)
# 下載下傳檔案較大時,使用循環下載下傳
print("down successful!")
except Exception as e:
print(e)
def getVideo_requests(url_m3u8, path, videoName):
urlData = getUrlData(url_m3u8)
tempName_video = os.path.join(path, '%s.ts'%videoName) # f'{}' 相當于'{}'.format() 或 '%s'%videoName
open(tempName_video, "wb").close() # 繼續下載下傳重複寫入
# print(urlData)
for line in urlData:
url_ts = str(line.decode("utf-8")).strip() # 清除字元串前後存在的空格符和換行符
if not '.ts' in url_ts:
continue
else:
if not url_ts.startswith('http'): # 判斷字元串是否以'http'開頭,如果不是則說明url連結不完整,需要拼接
# 拼接ts流視訊的url
url_ts = url_m3u8.replace(url_m3u8.split('/')[-1], url_ts)
print(url_ts)
getDown_reqursts(url=url_ts, file_path=tempName_video) # 下載下傳視訊流
filename = os.path.join(path, '%s.mp4'%videoName)
shutil.move(tempName_video, filename)
print('Great,%s.mp4 well down!'%videoName)
if __name__ == '__main__':
url_m3u8 = 'http://www.xxxx.com/video.m3u8' # 下載下傳視訊流位址
path = r'E:\123\'
videoName = url_m3u8.split('/')[-2]
getVideo_requests(url_m3u8, path, videoName)