Python 网络爬虫 多线程下载 m3u8视频文件

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-init)/30)+1
	
	
	if id==1:
		startpos=init
		endpos=init+eachmax
	else:
		startpos=init+(id-1)*eachmax
		endpos=startpos+eachmax
	print(tname," 正在尝试 ",startpos,endpos)
	
	i=startpos
	
	if endpos>max:
		endpos=max 
		
	while i<=endpos:
		tmp=i
		tmpstr=getnewname(tmp)
		i=i+1
		newurl=root+tmpstr 
		
		fpath="video4/{0}".format(tmpstr)
		if os.path.exists(fpath):
			print(tmpstr,' 文件已经下载,忽略 ',)
			continue
		else:
			new=requests.get(newurl)
			newok=savefile(tmpstr,new.content)

			print(tname,'正在下载文件',tmpstr)
	
	print(tname,' 工作已完成 ')
	return i
		


if __name__=="__main__":
	url=[
		'https:\/\/www.demosite.com\/20190708\/2\/1562567835\/v3\/v3.m3u8' 
	]
	#上面这个地址是m3u8地址。将其替换为真实地址即可
	
	mainurl=url[0]
	
	mainurl=mainurl.replace('\\','')
	
	r=requests.get(mainurl)
	result=r.content.decode('gbk')
	#保存到本地
	isok=savefile('t1.txt',r.content)
	maxlen,rooturl=getrooturl(result)
	print("总共需要下载的文件总数为:",maxlen)
	init=0
	maxt=31

	
	nr=range(1,maxt)
	
	#同步创建30个线程试一下
	for item in nr:
		mname="线程{0}".format(item)
		try:
		   _thread.start_new_thread(downthread, (mname,item,init,maxlen,rooturl) )
		except:
		   print("出错了,无法启动线程")
	 
	print("工作完成了吗?")
	while 1:
	   pass

		


		

你可能感兴趣的:(Python,Web技术,Web编程)