您好,登錄后才能下訂單哦!
如下所示:
# -*- coding:utf-8 -*- import sys import os from glob import glob import requests reload(sys) sys.setdefaultencoding("utf-8") file_path=os.getcwd() #獲取需要轉換的路徑 def get_user_path(argv_dir): if os.path.isdir(argv_dir): return argv_dir elif os.path.isabs(argv_dir): return argv_dir else: return False #對轉換的TS文件進行排序 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 file boxer.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 ts 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 "go home path" import shutil shutil.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'>>>[-] 目標文件不為空,將清空目標文件,是否更換路徑?' flag = raw_input('>>>[*] Yes:1 No:2 \n>>>[+] [2]') try: if flag == '1': _path = raw_input(unicode('>>>[+] 請輸入目標文件路徑。\n>>>[+] ').encode('gbk')) check_path(_path) else: # 清空存儲路徑 os.system('rd /S /Q ' + _path) os.system('mkdir ' + _path) return _path except Exception as e: print e exit(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視頻的url movies_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 '-' * 60 error_get = [] for _url in movie_url: # ts視頻的名稱 movie_name = _url.split('/')[-1][-6:] movie_name1 = _url.split('_')[-1][-6:] # print movie_name2 if 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 # 如果沒有不成功的請求就結束 if error_get: print u'共有%d個請求失敗' % len(file_list) print '-' * 60 download_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]目標URL。\n>>>[+] ').encode('gbk')) # _path = raw_input(unicode('>>>[+] 請輸入存儲目標文件路徑。\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 =_path o_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
以上這篇python 實現視頻流下載保存MP4的方法就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持億速云。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。