国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Python > 正文

python 實現(xiàn)視頻流下載保存MP4的方法

2020-02-16 00:33:22
字體:
供稿:網(wǎng)友

如下所示:

# -*- coding:utf-8 -*-import sysimport osfrom glob import globimport requests  reload(sys)sys.setdefaultencoding("utf-8")file_path=os.getcwd()#獲取需要轉(zhuǎn)換的路徑def get_user_path(argv_dir):if os.path.isdir(argv_dir):return argv_direlif os.path.isabs(argv_dir):return argv_direlse:return False#對轉(zhuǎn)換的TS文件進(jìn)行排序	def get_sorted_ts(user_path): ts_list = glob(os.path.join(user_path,'*.ts')) boxer = []for ts in ts_list: if os.path.exists(ts):# print(os.path.splitext(os.path.basename(ts)))file,_ = os.path.splitext(os.path.basename(ts))print fileboxer.append(file)# boxer.append(int(file))boxer.sort()print(boxer)return boxer#文件合并	def convert_m3u8(file_path,boxer,o_file_name):print u"開始拼接視頻" new_path=file_path+"/"+u"視頻"try:os.chdir(new_path)except Exception, e:os.mkdir(new_path)tmp = []for ts in boxer:print tstmp.append(str(ts)+'.ts')cmd_str = '+'.join(tmp)exec_str = "copy /b "+cmd_str+' '+o_file_nameprint("copy /b "+cmd_str+' '+o_file_name)os.chdir(user_path)os.system(exec_str)print "go home path"import shutilshutil.move(o_file_name,new_path+"/"+o_file_name)os.chdir(file_path)     reload(sys)sys.setdefaultencoding('utf-8') # 功能:爬取m3u8格式的視頻 # 檢查存儲路徑是否正常def check_path(_path):# 判斷存儲路徑是否存在if os.path.isdir(_path) or os.path.isabs(_path):# 判斷存儲路徑是否為空if not os.listdir(_path):return _path else: print u'>>>[-] 目標(biāo)文件不為空,將清空目標(biāo)文件,是否更換路徑?'flag = raw_input('>>>[*] Yes:1 No:2 /n>>>[+] [2]') try:if flag == '1':_path = raw_input(unicode('>>>[+] 請輸入目標(biāo)文件路徑。/n>>>[+] ').encode('gbk'))check_path(_path)else:# 清空存儲路徑os.system('rd /S /Q ' + _path)os.system('mkdir ' + _path)return _pathexcept Exception as e:print eexit(0) else:os.makedirs(_path)return _path # 獲取ts視頻的爬取位置def get_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'})with open(url_next, 'wb') as m3u8_content:m3u8_content.write(m3u8_txt.content)# 提取ts視頻的urlmovies_url = []_urls = open(url_next, 'rb')for line in _urls.readlines():if '.ts' in line:movies_url.append(url_pre + line[:-1])else:continue _urls.close()return movies_url # 爬取ts視頻def download_movie(num,movie_url, _path):os.chdir(_path)print u'>>>[+] 第{}個視頻 downloading...'.format(num)print '-' * 60error_get = [] for _url in movie_url: # ts視頻的名稱movie_name = _url.split('/')[-1][-6:]movie_name1 = _url.split('_')[-1][-6:]# print movie_name2if len(str(movie_name1))<=4:movie_name1="00"+str(movie_name1)elif len(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)with open(movie_name1, 'wb') as movie_content:movie_content.writelines(movie)print u'>>>[+] 視頻片段 ' + movie_name1 + u' 下載完成'# 捕獲異常,記錄失敗請求except:error_get.append(_url)continue# 如果沒有不成功的請求就結(jié)束if error_get:print u'共有%d個請求失敗' % len(file_list)print '-' * 60download_movie(error_get, _path)else:print '>>>[+] Download successfully!!!' url=["http://video.renrenjiang.cn/record/alilive/7684990805-1512186274.m3u8"] if __name__ == '__main__':try: # _url = raw_input(unicode('>>>[+] 請輸入指定的[.m3u8]目標(biāo)URL。/n>>>[+] ').encode('gbk'))# _path = raw_input(unicode('>>>[+] 請輸入存儲目標(biāo)文件路徑。/n>>>[+] ').encode('gbk'))for i in range(len(url)):_url=url[i]_path=os.getcwd()+"/"+"ts"+str(i+5)try:os.chdir(_path)except Exception, 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 =_patho_file_name=str(i+5)+".mp4"boxer = get_sorted_ts(user_path)convert_m3u8(file_path,boxer,o_file_name)#拼接視頻 except Exception as e:print e            
發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 曲沃县| 都昌县| 安西县| 泸水县| 海晏县| 江达县| 浙江省| 墨竹工卡县| 福贡县| 香格里拉县| 米泉市| 凉城县| 永平县| 洛南县| 尼勒克县| 林口县| 桃园市| 台州市| 临潭县| 尉氏县| 保德县| 江城| 平邑县| 正定县| 长泰县| 东乡县| 北宁市| 孝昌县| 娄底市| 大邑县| 固始县| 农安县| 宜昌市| 祥云县| 新巴尔虎左旗| 兴国县| 珠海市| 湘西| 云林县| 湘西| 中方县|