去掉m3u8的片头和片尾
# pip3 install -i https://mirrors.aliyun.com/pypi/simple/ m3u8
# pip3 install -i https://mirrors.aliyun.com/pypi/simple/ oss2
# pip3 install --upgrade pip # 在Centos中安装ffmpeg和aria2c
# https://www.cnblogs.com/littlehb/p/9347978.html import m3u8, sys
import os, subprocess
import oss2, requests, datetime # ========================================================================
# 前面需要截取的时间长度,单位:秒
prefixCutSeconds = 10
# 后面需要截取的时间长度,单位:秒
suffixCutSeconds = 5
# 上传的路径前缀
prefix = 'down/M3u8_ZhengZhou103/' # ========================================================================
# 访问oss的用户名与密码,无需修改
access_key_id = 'xxxxxxxxxxxx'
access_key_secret = 'xxxxxxxxxxxx'
bucket_name = 'dsideal-yy' # 检查是内网还是外网
def CheckInOut():
# 探测内网外网
print('正在探测使用环境是阿里云内网还是外网,请稍等...')
url = 'http://' + bucket_name + '.oss-cn-qingdao-internal.aliyuncs.com/down/Material/42/42385DBE-E03F-FF6A-A37C-CC8A04612BE4.doc'
try:
requests.get(url, timeout=1)
endpoint = 'http://oss-cn-qingdao-internal.aliyuncs.com/'
print('内部网络访问,将使用endpoint:' + endpoint)
except Exception as err:
endpoint = 'http://oss-cn-qingdao.aliyuncs.com/'
print('外网访问,将使用endpoint:' + endpoint)
return endpoint # 从哪个结点下进行操作
endpoint = CheckInOut() # 从哪里下载回来,如果是内网,应该写成 http://dsideal_yy.oss-cn-qingdao-internal.aliyuncs.com
if 'internal' in endpoint:
downloadPrefixUrl = 'http://' + bucket_name + '.oss-cn-qingdao-internal.aliyuncs.com/down/M3u8/'
else:
downloadPrefixUrl = 'http://' + bucket_name + '.oss-cn-qingdao.aliyuncs.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) # 下载m3u8的所有ts文件
def DoActionM3u8(m3u8_url):
# m3u8文件的真实名称
m3u8FileName = WorkingPath + "/" + 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) tempAllTs_new = WorkingPath + '/output_new.ts'
if os.path.exists(tempAllTs_new):
os.remove(tempAllTs_new) 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) # 拼接为原始的ts
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)
os.remove(tempAllTs)
# 以下为new的步骤=========================================================================
#
# 截取一部分
# (1) 测出文件有多长
videoLength = GetVideoLength(tempAllMp4)
# 去掉后suffixCutSeconds秒
seconds = t2s(videoLength) - suffixCutSeconds
m, s = divmod(seconds, 60)
h, m = divmod(m, 60)
endTime = "%02d:%02d:%02d" % (h, m, s) # 开始的秒数
m, s = divmod(prefixCutSeconds, 60)
h, m = divmod(m, 60)
startTime = "%02d:%02d:%02d" % (h, m, s)
# #
# # (2)去头部prefixCutSeconds秒,尾部suffixCutSeconds秒
print('截取并生成mp4!')
cmd = 'ffmpeg -i ' + tempAllMp4 + ' -vcodec copy -acodec copy -ss ' + startTime + ' -to ' + endTime + ' ' + tempAllMp4_new + ' -y'
os.system(cmd)
os.remove(tempAllMp4)
print('生成新mp4成功!到这里都是非常正常的!!!!!') # (3) mp4 转 ts
cmd = 'ffmpeg -y -i ' + tempAllMp4_new + ' -vcodec copy -acodec copy -vbsf h264_mp4toannexb ' + tempAllTs_new
os.system(cmd)
os.remove(tempAllMp4_new) # (4) 切割ts
prefixName = m3u8_url.split('/')[-1][0:36]
cmd = 'ffmpeg -i ' + tempAllTs_new + ' -c copy -map 0 -f segment -segment_list ' + WorkingPath + '/' + prefixName + '.m3u8 -segment_time 10 ' + WorkingPath + '/' + prefixName + '%03d.ts'
os.system(cmd)
os.remove(tempAllTs_new) # (5)上传到oss,放到另一个目录下
path = os.listdir(os.getcwd()+'/M3u8')
for filename in path:
if os.path.isfile(WorkingPath+'/'+filename):
if '.m3u8' in filename or '.ts' in filename:
logInfo('开始进行文件上传:' + filename)
key = prefix + filename[0:2] + '/' + filename
bucket.put_object_from_file(key, WorkingPath + '/' + filename, progress_callback=percentage)
print('文件上传云存储成功完成!' + filename) # 黄海定义的输出信息的办法,带当前时间
def logInfo(msg):
i = datetime.datetime.now()
print(" %s %s" % (i, msg)) # 进度条功能
def percentage(consumed_bytes, total_bytes):
if total_bytes:
per = int(100 * (float(consumed_bytes)) / (float(total_bytes)))
s1 = "\r[%s%s]%d%%" % ("=" * int(per), " " * (100 - int(per)), per)
sys.stdout.write(s1)
sys.stdout.flush() if __name__ == '__main__':
# 删除临时目录并重新创建
WorkingPath = '/usr/local/software/TestM3u8/M3u8'
os.system('rm -rf ' + WorkingPath)
os.mkdir(WorkingPath) # 构建OSS存储对象
auth = oss2.Auth(access_key_id, access_key_secret)
# 阿里云上应该用这个内部网络选项
bucket = oss2.Bucket(auth, endpoint, bucket_name) # 以一个名师云课为例
m3u8_url = downloadPrefixUrl+'C4/C4CD770E-C98A-AF8C-8B53-8541210B355B.m3u8'
print(m3u8_url)
DoActionM3u8(m3u8_url)
# http://video.edusoa.com/down/M3u8_ZhengZhou103/C4/C4CD770E-C98A-AF8C-8B53-8541210B355B.m3u8
# http://dsideal-yy.oss-cn-qingdao.aliyuncs.com/down/M3u8_ZhengZhou103/C4/C4CD770E-C98A-AF8C-8B53-8541210B355B.m3u8
#
去掉m3u8的片头和片尾的更多相关文章
- 去掉utf-8的Bom头:使用java以及jdbc不使用第三方库执行sql文件脚本
package com.xxx.xxx.dao; import java.io.BufferedReader; import java.io.File; import java.io.FileInpu ...
- Python实现视频片头和片尾添加
import imageio imageio.plugins.ffmpeg.download() from datetime import datetime import os from moviep ...
- Python调用ffpmeg和ffprobe处理视频文件
需求: 运营有若干批次的视频.有上千个,视频文件,有mp4格式的,有ts格式的 现在有需要去掉视频文件片头和片尾的批量操作需求. 比如 文件夹A下面的视频去掉片尾10秒 文件夹B下面的视频去掉片头6秒 ...
- 怎么用Camtasia给视频添加片头片尾
有许多朋友现在喜欢自己拍摄一些小视频,现在不管是在抖音还是在B站,我们看到的大部分视频都有UP主自己制作的片头或片尾.片头做的好,甚至会有人因为片头而关注UP主,能吸引更多的人来观看视频. 所以,如果 ...
- VC6_预编译头
1.去掉 使用预编译头"stdafx.h" VC6 --> Project --> Settings.. --> C/C++选项卡 --> "Ca ...
- 简单几步就能把素材变成大片?老司机推荐Vegas
"素材编辑"一般分为两种,一种是对时间线素材长度和位置的编辑,另一种就是遮罩法操作. 第一种,裁剪素材(将素材在我们选定的位置一分为二),对时间线上的素材进行裁剪,有两种方法: 一 ...
- (十三)Packet socket 和 sockaddr_ll
描述 本文简单描述了数据链路层的socket使用的两种方法正文 Linux下有两种方式接收数据链路层的数据包: (1)原始的方法,即创建一个类型为SOCK_PACKET的s ...
- Android 手机卫士--阶段小结1
本文地址:http://www.cnblogs.com/wuyudong/p/5904528.html,转载请注明源地址. 本文对之前手机卫士开发进行一个小结. 1.SplashActivity 版本 ...
- ckplayer.js视频播放插件
网页中常见的功能就是播放视频,下面介绍的这个ckplayer.js既可以在pc端播放,也可以在手机网页上播放. 可调用flash也可以调用html5播放器: <div id="a1&q ...
随机推荐
- JAVA记录-SpringMVC scope属性的两种模式
singleton作用域:当把一个Bean定义设置为singleton作用域是,Spring IoC容器中只会存在一个共享的Bean实例,并且所有对Bean的请求,只要id与该Bean定义相匹配,则只 ...
- Neural Networks and Deep Learning 课程笔记(第四周)深层神经网络(Deep Neural Networks)
1. 深层神经网络(Deep L-layer neural network ) 2. 前向传播和反向传播(Forward and backward propagation) 3. 总结 4. 深层网络 ...
- (cx_Oracle.DatabaseError) DPI-1047: 64-bit Oracle Client library cannot be loaded: "libclntsh.so: cannot open shared object file: No such file or directory"
打开https://oracle.github.io/odpi/doc/installation.html 官方相关如下 Oracle Instant Client RPM¶ To run ODPI- ...
- 集大软件工程15级个人作业Week2
集大软件工程15级个人作业Week2 快速通读教材<构建之法>,并参照提问模板,提出5个问题. 在每个问题后面,请说明哪一章节的什么内容引起了你的提问,提供一些上下文 列出一些事例或资料, ...
- 液晶数字显示屏QLCDNumbe
import sys from PyQt5.QtWidgets import QApplication, QWidget, QLCDNumber, QVBoxLayout class Demo(QWi ...
- luogu P3565 [POI2014]HOT-Hotels
传送门 无脑暴力+O2=AC 题目要统计距离两两相等的三个点的组数,这三个点之间显然有一个点,并且这三个点到这个点的距离都相同.所以枚举中间这个点作为根,然后bfs整棵树,对于每一层,把以根的某个儿子 ...
- TypeError: view must be a callable or a list/tuple in the case of include()
原文连接: http://www.imooc.com/qadetail/98920 我是这么写的就好了 from django.conf.urls import url from django.con ...
- android 常见分辨率与DPI对照表
分辨率对应DPI ldpi QVGA (240×320) mdpi HVGA (320×480) hdpi WVGA (480×800),FWVGA (480×854) xhdpi 720P( ...
- js中获取时间new date()的用法和获取时间戳
获取时间: 1 var myDate = new Date();//获取系统当前时间 获取特定格式的时间: 1 myDate.getYear(); //获取当前年份(2位) 2 myDate.getF ...
- python3之redis
1.redis简介 redis是一个key-value存储系统.和Memcached类似,它支持存储的value类型相对更多,包括string(字符串).list(链表).set(集合).zset(s ...