修正名师云课的视频片头
Posted littlehb
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了修正名师云课的视频片头相关的知识,希望对你有一定的参考价值。
# 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() + ‘ ‘, 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() + ‘ ‘, 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)
以上是关于修正名师云课的视频片头的主要内容,如果未能解决你的问题,请参考以下文章