Python 网络爬虫 多线程下载 m3u8视频文件
你们都懂的,m3u8 事实上是一个视频列表文件,类似于直播流。不太好下载。里面是包含了全部视频碎片的地址。 我们可以将这些视频小文件下下来,合并后就成一个完整的视频。
下面是 down.py 的代码,略粗糙,权当大家一起学习研究了。
当然现在有很多可以下 m3u8的,发出这个,只是学习python之用。
from bs4 import BeautifulSoup
from urllib.request import urlopen
from urllib.error import HTTPError
import requests
import chardet
import sys
import pymysql
import random
import time
import os
import _thread
def savefile(fname,fcontent):
with open("video4/"+fname,"wb") as f:
f.write(fcontent)
return 1
def getnewname(tmp):
if tmp<=9:
strtmp="v_00{0}.ts".format(tmp)
elif tmp<=99 and tmp>9:
strtmp="v_0{0}.ts".format(tmp)
else:
strtmp="v_{0}.ts".format(tmp)
return strtmp
def getrooturl(result):
data=result.split('\n')
ds=data[-3].split('.ts')[0]
maxlen=int(ds.split('v_')[1])
rooturl=mainurl.split('v3.m3u8')[0]
return maxlen,rooturl
def downthread(tname,id,init,max,root):
eachmax=int((max-ini