備忘4
https://github.com/orestonce/m3u8d
https://github.com/orestonce/m3u8d/releases
#! /usr/bin/python3
# hostname = 'https://media.finger66.com'
# tsParamUrl = hostname + '/posts/84222300000/MTU1NjEwNDU0ODI2Nw==.mp4.m3u8'
def xiazai():
? ? #? 下載ts文件到本地
? ? #? 放置ts文件所在目錄
? ? tsPath = input('請輸入需要存放ts文件的路徑:') + '/'
? ? # 獲取ts文件參數(shù)
? ? hostname = input('請輸入視頻主頁網(wǎng)址:')
? ? m3u8_url = input('請輸入m3u8網(wǎng)址:')
? ? tsParamUrl = hostname + m3u8_url
? ? import urllib.request
? ? tsParamData = urllib.request.urlopen(tsParamUrl).read().decode('utf-8')
? ? # print(tsParamData)
? ? import re
? ? pat = '/(.*?).ts'
? ? tsParamArr = re.compile(pat).findall(tsParamData)
? ? #爬取ts文件到本地
? ? for n in range(0,len(tsParamArr)):
? ? ? ? strArr = tsParamArr[n].split('/')
? ? ? ? localTSFileName = strArr[len(strArr)-1]+'.ts'
? ? ? ? tsLink = hostname + '/' + tsParamArr[n] + '.ts'
? ? ? ? urllib.request.urlretrieve(tsLink, tsPath+localTSFileName )
? ? ? ? #print(tsLink)
? ? print("下載完成,開始合并ts文件?。。?#34;)
? ? import os
? ? # 獲取所有的ts文件
? ? path_list = os.listdir(tsPath)
? ? # 對文件進(jìn)行排序并將排序后的ts文件路徑放入列表中
? ? path_list.sort()
? ? li = [os.path.join(tsPath, filename) for filename in path_list]
? ? # 將ts路徑并合成一個字符參數(shù)
? ? tsfiles = '|'.join(li)
? ? #print(tsfiles)
? ? # 指定輸出文件名稱
? ? saveMp4file = tsPath + '下載完成.mp4'
? ? # 調(diào)取系統(tǒng)命令使用ffmpeg將ts合成mp4文件
? ? cmd = 'ffmpeg -i "concat:%s" -acodec copy -vcodec copy -absf aac_adtstoasc %s' % (tsfiles, saveMp4file)
? ? os.system(cmd)
? ? print("合并ts文件完成?。?!")
def hecheng():
? ? #? 將下載到本地的ts文件合成mp4文件
? ? #
? ? import os
? ? tsPath = input('請輸入需要存放ts文件的路徑:') + '/'
? ? #獲取所有的ts文件
? ? path_list = os.listdir(tsPath)
? ? #對文件進(jìn)行排序并將排序后的ts文件路徑放入列表中
? ? path_list.sort()
? ? li = [os.path.join(tsPath,filename) for filename in path_list]
? ? #將ts路徑并合成一個字符參數(shù)
? ? tsfiles = '|'.join(li)
? ? #print(tsfiles)
? ? #指定輸出文件名稱
? ? saveMp4file = tsPath + '下載完成.mp4'
? ? #調(diào)取系統(tǒng)命令使用ffmpeg將ts合成mp4文件
? ? cmd = 'ffmpeg -i "concat:%s" -acodec copy -vcodec copy -absf aac_adtstoasc %s' % (tsfiles, saveMp4file)
? ? os.system(cmd)
while True:
? ? print("*" * 80)
? ? print("")
? ? print("\t\t\t歡迎使用 【視頻工具】 V 1.0")
? ? print("")
? ? print('\t簡介:直接下載或是合成已經(jīng)下載的ts 文件。。。。。"')
? ? print("")
? ? print("")
? ? print("\t\t\t\t選擇? [1]? 下載視頻文件")
? ? print("")
? ? print("\t\t\t\t選擇? [2]? 把已經(jīng)下載的ts文件合成MP4文件")
? ? print("")
? ? print("\t\t\t\t選擇? [0]? 退出程序")
? ? print("")
? ? print("*" * 80)
? ? action_str = input("\t\t\t請選擇要操作的程序:")
? ? print("\t\t\t您選擇的操作是 【%s】" % action_str)
? ? if action_str in ["1", "2"]:
? ? ? ? if action_str == "1":
? ? ? ? ? ? xiazai()
? ? ? ? ? ? print('主人下載完成,請到文件目錄查看?。?!')
? ? ? ? elif action_str == "2":
? ? ? ? ? ? hecheng()
? ? elif action_str == "0":
? ? ? ? print("\t\t\t歡迎再次使用【視頻工具系統(tǒng)】")
? ? ? ? break
? ? else:
? ? ? ? print("\t\t\t您輸入的不正確請重新輸入!")