-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcm.pyw
216 lines (187 loc) · 9.22 KB
/
cm.pyw
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
# -*- coding: utf-8 -*-
# author: cheneyjin@outlook.com
import _thread
import json
import logging
import os
import re
import sys
import uuid
from subprocess import Popen, PIPE
from PyQt5 import QtCore, QtWidgets
from PyQt5.QtGui import QIcon, QTextCursor
from PyQt5.QtWidgets import QMainWindow, QApplication
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
filename='log.md')
logger = logging.getLogger(__name__)
def validate(file_name_str):
r_str = r"[\/\\\:\*\?\"\<\>\|]"
new_title = re.sub(r_str, "_", file_name_str)
return new_title
class Target(object):
def __init__(self, name, video_path, audio_path, root) -> None:
super().__init__()
self.name = name
self.video_path = video_path
self.audio_path = audio_path
self.root = root
def __str__(self) -> str:
return "name:{}, video_path:{}, audio_path:{}, root:{}" \
.format(self.name, self.video_path, self.audio_path, self.root)
class UiMainWindow(object):
def __init__(self) -> None:
super().__init__()
self.task_id = None
self.directory = None
self.target_video_name = "video.m4s"
self.target_audio_name = "audio.m4s"
self.target_file_info = "entry.json"
self.task_list = None
self.run_path = os.path.abspath(os.path.join(os.path.abspath(__file__), ".."))
self.merging = False
self.searching = False
# print(self.run_path)
self.style = """
*{
font-family: "Microsoft YaHei";
}
"""
def retranslate_ui(self, main_window):
_translate = QtCore.QCoreApplication.translate
main_window.setWindowTitle(_translate("bili cache merging tool", "B站缓存合并工具-翻滚吧年糕君 ID:1489684"))
main_window.setFixedSize(main_window.width(), main_window.height())
main_window.setWindowIcon(QIcon('local/favicon.ico'))
self.text_view.setText(_translate("copy cache folder from your phone local storage path :\n"
"'/Android/data/tv.danmaku.bili/download' into your pc. \n"
"note: do not change anything in cache folder",
"从手机存储路径:'/Android/data/tv.danmaku.bili/download'\n"
"拷贝缓存<完整文件夹>到电脑中,放于任意目录。"))
self.text_view.append("\r\n")
self.text_view.append(_translate("if application gets some exception, "
"please email me at 'cheneyjin@outlook.com' and attach the file named 'log.md'"
"that in application folder."
"email content has picture and text that will be better."
, "如果运行异常,请邮件至'cheneyjin@outlook.com',"
"邮件中请上传程序目录下的'log.md'文件,"
"内容能图文并茂就更好了~"))
self.path_select_btn.setText(_translate("select cache path", "选择路径"))
self.start_btn.setText(_translate("start merge", "开始"))
# noinspection PyAttributeOutsideInit,PyUnresolvedReferences
def setup_ui(self, main_window):
main_window.setObjectName("mainWindow")
main_window.resize(471, 203)
main_window.setStyleSheet(self.style)
self.central_widget = QtWidgets.QWidget(main_window)
self.central_widget.setObjectName("central_widget")
self.vertical_LayoutWidget = QtWidgets.QWidget(self.central_widget)
self.vertical_LayoutWidget.setGeometry(QtCore.QRect(0, 0, 471, 191))
self.vertical_LayoutWidget.setObjectName("verticalLayoutWidget")
self.vertical_LayoutWidget.setContentsMargins(1, 1, 1, 1)
self.vertical_layout = QtWidgets.QVBoxLayout(self.vertical_LayoutWidget)
self.vertical_layout.setObjectName("verticalLayout")
self.path_select_btn = QtWidgets.QPushButton(self.vertical_LayoutWidget)
self.path_select_btn.setObjectName("path_select_btn")
self.vertical_layout.addWidget(self.path_select_btn)
self.text_view = QtWidgets.QTextEdit(self.vertical_LayoutWidget)
self.text_view.setObjectName("text_view")
self.vertical_layout.addWidget(self.text_view)
self.start_btn = QtWidgets.QPushButton(self.vertical_LayoutWidget)
self.start_btn.setObjectName("start_btn")
self.start_btn.setContentsMargins(0, 10, 0, 10)
self.vertical_layout.addWidget(self.start_btn)
main_window.setCentralWidget(self.central_widget)
self.status_bar = QtWidgets.QStatusBar(main_window)
self.status_bar.setObjectName("status_bar")
main_window.setStatusBar(self.status_bar)
self.path_select_btn.clicked.connect(self.select_path)
self.start_btn.clicked.connect(self.start_merge)
self.retranslate_ui(main_window)
QtCore.QMetaObject.connectSlotsByName(main_window)
def start_merge(self):
if self.searching:
self.text_view.append("检测缓存数量任务还没结束,别点啦!")
return
if not self.merging:
self.text_view.setText("开始合并,请等待...")
self.merging = True
else:
self.text_view.append("正在工作,不要重复点击")
return
_thread.start_new_thread(self.task, ("Thread-1", 2,))
def task(self, arg1, arg2):
try:
self.task_id = uuid.uuid1()
logger.info("\nTASK START {}".format(self.task_id))
if self.task_list is None or len(self.task_list) < 1:
self.text_view.append("别点啦,没检测到文件")
return
for index, f in enumerate(self.task_list):
cmd = "{}/local/ffmpeg.exe -y -i \"{}\" -i \"{}\" -c:v copy -c:a aac -strict " \
"experimental \"{}/{}.mp4\"" \
.format(self.run_path, f.video_path, f.audio_path, f.root, validate(f.name))
logger.info(cmd)
with Popen(cmd, shell=True, stdin=PIPE, stdout=PIPE, stderr=PIPE) as p:
p.communicate()
self.text_view.append("内容:{},{}\n进度:{}%".format(f.name, "已合并", str(
round(((index + 1) / len(self.task_list)) * 100, 2))))
self.text_view.moveCursor(QTextCursor.End)
self.text_view.append("运行结束。 developed by: BILIBILI_ID:1489684 翻滚吧年糕君")
except Exception as e:
logger.error(e)
finally:
self.merging = False
logger.info("TASK FINISHED {}".format(self.task_id))
def select_path(self):
self.task_list = list()
self.directory = QtWidgets.QFileDialog.getExistingDirectory(None, "选取文件夹", "D:/")
self.text_view.setText("路径: {}".format(self.directory))
path_str = str(self.directory)
if "" == path_str:
self.text_view.append("没选择任何路径。")
else:
self.searching = True
try:
self.search_path(path_str)
self.text_view.append("识别到: {}个可合并视频。".format(len(self.task_list)))
finally:
self.searching = False
# for f in self.task_list:
# print(f)
def search_path(self, root_path):
for l in os.listdir(root_path):
path = os.path.join(root_path, l)
if os.path.isdir(path):
self.search_path(path)
else:
if path.__contains__(self.target_video_name):
self.load_file(path)
def load_file(self, path):
parent_path = os.path.abspath(os.path.join(path, ".."))
file_info_path = "{}/../{}".format(parent_path, self.target_file_info)
name = None
try:
with open(file_info_path, "r", encoding="utf-8") as f:
file_info = json.load(f)
name = file_info["page_data"]["download_subtitle"]
except Exception as e:
logger.error(e)
video_path = "{}/{}".format(parent_path, self.target_video_name)
audio_path = "{}/{}".format(parent_path, self.target_audio_name)
video_exist = os.path.exists(video_path)
audio_exist = os.path.exists(audio_path)
if video_exist is False or audio_exist is False:
logger.error("FILE PATH {}:{}".format(video_exist, video_path))
logger.error("FILE PATH {}:{}".format(audio_exist, audio_path))
self.text_view.append("检测到缓存存在缺失,路径:{}".format(parent_path))
return
# "{}/../".format(parent_path)
t = Target(name if name is not None else "临时名称{}".format(uuid.uuid1())
, video_path, audio_path, parent_path)
self.task_list.append(t)
if __name__ == '__main__':
app = QApplication(sys.argv)
MainWindow = QMainWindow()
ui = UiMainWindow()
ui.setup_ui(MainWindow)
MainWindow.show()
sys.exit(app.exec_())