1. 程式人生 > >修正名師雲課的視頻片頭

修正名師雲課的視頻片頭

eol dir 頭部 mmu .cn lena grep class 修正

# pip3 install -i https://mirrors.aliyun.com/pypi/simple/ m3u8
# pip3 install --upgrade pip

# 在Centos中安裝ffmpeg和aria2c
# https://www.cnblogs.com/littlehb/p/9347978.html

import m3u8
import os, subprocess

# 從哪裏下載回來,如果是內網,應該寫成 http://dsideal_yy.oss-cn-qingdao-internal.aliyuncs.com
downloadPrefixUrl = 
http://video.edusoa.com/down/M3u8/ # 獲取視頻的時間長度 def GetVideoLength(fileName): cmd = "ffmpeg -i " + fileName + " 2>&1 | grep ‘Duration‘ | cut -d ‘ ‘ -f 4 | sed s/,//" p = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE) out, err = p.communicate() for line in out.splitlines():
return (line.decode()[0:8]) def t2s(t): h,m,s = t.strip().split(":") return int(h) * 3600 + int(m) * 60 + int(s) # # # 合計秒數 # print(t2s(getReturn)) # # # 去掉後5秒 # seconds=t2s(getReturn)-5 # m, s = divmod(seconds, 60) # h, m = divmod(m, 60) # print ("%02d:%02d:%02d" % (h, m, s)) # 下載m3u8的所有ts文件
def downM3u8(m3u8_url): # m3u8文件的真實名稱 m3u8FileName = m3u8_url.split(/)[-1] print(M3u8真實文件名稱: + m3u8FileName) # 構造兩個文本文件 m3u8List = [] m3u8FileList = [] m3u8_obj = m3u8.load(m3u8_url) for l in m3u8_obj.segments: m3u8List.append(downloadPrefixUrl + l.uri[0:2] + / + l.uri) m3u8FileList.append("file ‘" + l.uri + "") # 形成文本文件,這個用於調用aria2c進行批量下載使用 tempFile = WorkingPath + /url.txt result = map(lambda x: x.strip() + \n, m3u8List) with open(tempFile, w) as f: f.writelines(result) # 下載回來轉碼後的視頻ts cmd = aria2c -c -s 4 -d + WorkingPath + -j 8 -i + tempFile os.system(cmd) # 形成文本文件,用於將ts文件拼接成大的ts文件時使用 tempFile = WorkingPath + /mylist.txt result = map(lambda x: x.strip() + \n, m3u8FileList) with open(tempFile, w) as f: f.writelines(result) # 使用ffmpeg 拼成大的ts tempAllTs = WorkingPath + /output.ts if os.path.exists(tempAllTs): os.remove(tempAllTs) tempAllMp4 = WorkingPath + /output.mp4 if os.path.exists(tempAllMp4): os.remove(tempAllMp4) tempAllMp4_new = WorkingPath + /output_new.mp4 if os.path.exists(tempAllMp4_new): os.remove(tempAllMp4_new) cmd = ffmpeg -f concat -i + tempFile + -c copy + tempAllTs + -y os.system(cmd) # # 將大的ts轉為mp4 cmd = ffmpeg -i + tempAllTs + -c:v copy -c:a copy -bsf:a aac_adtstoasc + tempAllMp4 os.system(cmd) # # # 截取一部分 # # (1) 測出文件有多長 videoLength=GetVideoLength(tempAllMp4) print(videoLength) # # # # (2)去頭部10秒,尾部5秒 # cmd = ‘ffmpeg -i ‘ + tempAllMp4 + ‘ -vcodec copy -acodec copy -ss 00:00:11 -to 00:01:30 ‘ + tempAllMp4_new + ‘ -y‘ # os.system(cmd) # # # (3)將mp4切割分片,10 秒一個 # cmd = ‘ffmpeg -i ‘ + tempAllMp4_new + ‘ -c:v copy -c:a aac -strict -10 -f hls ‘+m3u8FileName # os.system(cmd) # # # (4)刪除臨時文件 # os.remove(tempAllTs) # os.remove(tempAllMp4) # os.remove(tempAllMp4_new) # (5)上傳到oss,放到另一個目錄下 if __name__ == __main__: # 刪除臨時目錄並重新創建 WorkingPath = /usr/local/software/TestM3u8/M3u8 os.system(rm -rf + WorkingPath) os.mkdir(WorkingPath) # 以一個名師雲課為例 m3u8_url = http://video.edusoa.com/down/M3u8/C4/C4CD770E-C98A-AF8C-8B53-8541210B355B.m3u8 downM3u8(m3u8_url)

修正名師雲課的視頻片頭