python 实现视频流下载保存MP4的方法
如下所示:
#-*-coding:utf-8-*- importsys importos fromglobimportglob importrequests reload(sys) sys.setdefaultencoding("utf-8") file_path=os.getcwd() #获取需要转换的路径 defget_user_path(argv_dir): ifos.path.isdir(argv_dir): returnargv_dir elifos.path.isabs(argv_dir): returnargv_dir else: returnFalse #对转换的TS文件进行排序 defget_sorted_ts(user_path): ts_list=glob(os.path.join(user_path,'*.ts')) boxer=[] fortsints_list: ifos.path.exists(ts): #print(os.path.splitext(os.path.basename(ts))) file,_=os.path.splitext(os.path.basename(ts)) printfile boxer.append(file) #boxer.append(int(file)) boxer.sort() print(boxer) returnboxer #文件合并 defconvert_m3u8(file_path,boxer,o_file_name): printu"开始拼接视频" new_path=file_path+"/"+u"视频" try: os.chdir(new_path) exceptException,e: os.mkdir(new_path) tmp=[] fortsinboxer: printts tmp.append(str(ts)+'.ts') cmd_str='+'.join(tmp) exec_str="copy/b"+cmd_str+''+o_file_name print("copy/b"+cmd_str+''+o_file_name) os.chdir(user_path) os.system(exec_str) print"gohomepath" importshutil shutil.move(o_file_name,new_path+"/"+o_file_name) os.chdir(file_path) reload(sys) sys.setdefaultencoding('utf-8') #功能:爬取m3u8格式的视频 #检查存储路径是否正常 defcheck_path(_path): #判断存储路径是否存在 ifos.path.isdir(_path)oros.path.isabs(_path): #判断存储路径是否为空 ifnotos.listdir(_path): return_path else: printu'>>>[-]目标文件不为空,将清空目标文件,是否更换路径?' flag=raw_input('>>>[*]Yes:1No:2\n>>>[+][2]') try: ifflag=='1': _path=raw_input(unicode('>>>[+]请输入目标文件路径。\n>>>[+]').encode('gbk')) check_path(_path) else: #清空存储路径 os.system('rd/S/Q'+_path) os.system('mkdir'+_path) return_path exceptExceptionase: printe exit(0) else: os.makedirs(_path) return_path #获取ts视频的爬取位置 defget_url(_url,_path): all_url=_url.split('/') url_pre='/'.join(all_url[:-1])+'/' url_next=all_url[-1] os.chdir(_path) #获取m3u8文件 m3u8_txt=requests.get(_url,headers={'Connection':'close'}) withopen(url_next,'wb')asm3u8_content: m3u8_content.write(m3u8_txt.content) #提取ts视频的url movies_url=[] _urls=open(url_next,'rb') forlinein_urls.readlines(): if'.ts'inline: movies_url.append(url_pre+line[:-1]) else: continue _urls.close() returnmovies_url #爬取ts视频 defdownload_movie(num,movie_url,_path): os.chdir(_path) printu'>>>[+]第{}个视频downloading...'.format(num) print'-'*60 error_get=[] for_urlinmovie_url: #ts视频的名称 movie_name=_url.split('/')[-1][-6:] movie_name1=_url.split('_')[-1][-6:] #printmovie_name2 iflen(str(movie_name1))<=4: movie_name1="00"+str(movie_name1) eliflen(str(movie_name1))<=5: movie_name1="0"+str(movie_name1) else: movie_name1=str(movie_name1) try: #'Connection':'close'防止请求端口占用 #timeout=30防止请求时间超长连接 movie=requests.get(_url,headers={'Connection':'close'},timeout=60) withopen(movie_name1,'wb')asmovie_content: movie_content.writelines(movie) printu'>>>[+]视频片段'+movie_name1+u'下载完成' #捕获异常,记录失败请求 except: error_get.append(_url) continue #如果没有不成功的请求就结束 iferror_get: printu'共有%d个请求失败'%len(file_list) print'-'*60 download_movie(error_get,_path) else: print'>>>[+]Downloadsuccessfully!!!' url=["http://video.renrenjiang.cn/record/alilive/7684990805-1512186274.m3u8"] if__name__=='__main__': try: #_url=raw_input(unicode('>>>[+]请输入指定的[.m3u8]目标URL。\n>>>[+]').encode('gbk')) #_path=raw_input(unicode('>>>[+]请输入存储目标文件路径。\n>>>[+]').encode('gbk')) foriinrange(len(url)): _url=url[i] _path=os.getcwd()+"/"+"ts"+str(i+5) try: os.chdir(_path) exceptException,e: os.mkdir(_path) storage_path=check_path(_path) movie_url=get_url(_url,storage_path) download_movie(i+1,movie_url,storage_path)#下载视频 user_path=_path o_file_name=str(i+5)+".mp4" boxer=get_sorted_ts(user_path) convert_m3u8(file_path,boxer,o_file_name)#拼接视频 exceptExceptionase: printe
以上这篇python实现视频流下载保存MP4的方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持毛票票。