# -*- 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视频