ts文件特点:视频被切割成很小很小的碎片,每段只有2-3秒钟,一个电影文件被切成几千个ts文件,按照时间顺序排列。
index1.ts、index2.ts、index3.ts、index4.ts…index799.ts
所以代码的核心功能就出现了,
ts文件多线程下载,可以从web下载ts文件
- 1)“静默下载”,静悄悄的批量下载,一边工作,稍后欣赏
- 2)“快速合并”一键合并成可拖动的完整视频,不用再打命令了
- 3)保存历史记录,上次下载的地址清清楚楚
- 4)界面轻美化
留下代码,以备后用:
import os,time,datetime,random
import urllib.request
import threading
from datetime import date,timedelta
from PyQt5 import QtCore
from PyQt5.QtCore import QDate,Qt,QRegExp,QFile,Qt,QModelIndex
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
from PyQt5.QtSql import *
import configparser
from qt_material import apply_stylesheet
class ThreadWorker(threading.Thread):
def __init__(self,url,downpath,from_id,to_id):
threading.Thread.__init__(self)
self.url = url
self.downpath = downpath
self.f_id = from_id
self.t_id = to_id
def run(self):
for i in range(self.f_id,self.t_id):
rurl = self.url +"/index{}.ts".format(i)
path = self.downpath+"/web{}.ts".format(str(i).zfill(4))
if not os.path.exists(path):
urllib.request.urlretrieve(rurl,path)
class DtsWidget(QWidget):
def __init__(self):
super(QWidget, self).__init__()
self.main_layout = QVBoxLayout()
self.url_edit = QLineEdit()
self.path_edit = QLineEdit()
self.thread_edit = QLineEdit()
self.config_path = 'history.ini'
self.btn_down = QPushButton(u"下载")
self.btn_glu = QPushButton(u"合并")
self.main_layout.addWidget(self.url_edit)
self.main_layout.addWidget(self.path_edit)
self.main_layout.addWidget(self.thread_edit)
self.main_layout.addWidget(self.btn_down)
self.main_layout.addWidget(self.btn_glu)
self.config = configparser.ConfigParser()
self.threadNum = 30
# stud_widget = QWidget()
# stud_grid = QGridLayout()
# edit_stud_widget = QWidget()
# horz_stud_layout = QHBoxLayout()
self.setLayout(self.main_layout)
self.path_edit.setText("ts")
self.thread_edit.setText('6')
self.btn_down.clicked.connect(self.down_load)
self.btn_glu.clicked.connect(self.glu)
self.init_config()
def init_config(self):
if not os.path.exists(self.config_path):
self.addhistory("","")
else:
url,path = self.load_history()
self.url_edit.setText(url)
self.path_edit.setText(path)
def addhistory(self,url,path):
if not self.config.has_section("history"):
self.config.add_section('history')
self.config.set('history','url',url)
self.config.set('history','path',path)
self.config.write(open(self.config_path,'w'))
def load_history(self):
self.config.read(self.config_path)
history_name = ''
if self.config.has_option('history','path'):
history_url = self.config.get('history','url')
history_path = self.config.get('history','path')
return history_name,history_path
def down_load(self):
url = self.url_edit.text();
arr = url.split("/")
narr = arr[0:len(arr)-1]
base_url = '/'.join(narr)
path = self.path_edit.text()
if not os.path.exists(path):
os.system('md '+path)
self.addhistory(base_url,path)
thread_num = int(self.thread_edit.text())
for i in range(0, thread_num):
New_threads = ThreadWorker(base_url,path,i*self.threadNum, (i+1)*self.threadNum)
New_threads.start()
def glu(self):
path = self.path_edit.text()
os.system("copy /b " + path + "/*.ts 合成.ts")
if __name__=='__main__':
import sys
app = QApplication(sys.argv)
app.setApplicationName("ts下载神器")
apply_stylesheet(app, theme='dark_teal.xml')
w = DtsWidget()
w.show()
sys.exit(app.exec_())