importrequests,reimportsys,timeimportosimportnumpy as npimportglob
work_dir=os.getcwd()#print(work_dir)
#用来保存ts文件
file_dir = os.path.join(work_dir,'file_tmp')if notos.path.exists(file_dir):
os.mkdir(file_dir)defsavefile(file_url):#配置headers防止被墙,一般问题不大
headers ={'user-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.116 Safari/537.36'}
r= requests.get(file_url, headers=headers)#合成带有hls的m3u8地址
if r.text.split('\n')[-1] == '':
hls_mark= r.text.split('\n')[-2] #以防\n结尾
else:
hls_mark= r.text.split('\n')[-1]
url_m3u8_hls= file_url.replace('index.m3u8', hls_mark)
file_m3u8= url_m3u8_hls.split('/')[-1]
duqu(url_m3u8_hls,file_m3u8)print(url_m3u8_hls)'''if r.status_code == 200:
with open(file_name, 'wb') as f:
f.write(r.content)'''
defduqu(index,file_m3u8):#配置headers防止被墙,一般问题不大
headers ={'user-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.116 Safari/537.36'}
r= requests.get(index, headers=headers)
with open('file_tmp/'+file_m3u8, 'wb') as f:
f.write(r.content)
f.close()
xieru(file_m3u8,index)defxieru(file_m3u8,index):
with open('file_tmp/'+file_m3u8,'r') as f:
r=f.read()
f.close()#合成带有hls的m3u8地址
#iter_lines得到的是bytesstring
text_bytes = r.split('\n')#转化成正常string
#text_string = [i.decode('utf-8') for i in text_bytes]
#筛选以.ts结尾的行
#有些情况下可能是以其他格式的文件,比如png,下载后修改后缀即可
#ts_name = [i for i in text_string if i.endswith('.ts')]
ts_name = [i for i in text_bytes if i.startswith('#EXTINF')]
ts_neirong= [i for i in text_bytes if not i.startswith('#')]
ts_neirong.pop()#xiazai(ts_neirong,index)
#print(ts_neirong)
if '#' ints_name:#部分ts文件名中带有路径信息,只保留文件名即可
#ts_name = [i.split('/')[-1] for i in ts_name]
#筛选带有时间的行
url_xiazai = ts_name.replace('#EXTINF:', '')print(ts_time[:3])defxiazai(ts_neirong,index):#配置headers防止被墙,一般问题不大
headers ={'user-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.116 Safari/537.36'}for i inrange(len(ts_neirong)):
hls_mark= index.split('/')[-1]
url_xiazai=index.replace(hls_mark, ts_neirong[i])print(url_xiazai)r = requests.get(url_xiazai, headers=headers)
with open('file_tmp/'+ts_neirong[i], 'wb') as f:
f.write(r.content)
f.close()
url= 'http://iqiyi.cdn9-okzy.com/20200907/15137_ed25d8c5/index.m3u8' #输入m3u8
savefile(url)