-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmaster_local.py
More file actions
177 lines (143 loc) · 4.29 KB
/
master_local.py
File metadata and controls
177 lines (143 loc) · 4.29 KB
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
"""
master
watch MKV file, then upload to ftp server
need curl, alternative ftp or rsync
"""
import os
import json
import hashlib
from icecream import ic
from loguru import logger
SRC = None
CACHE = None
CURL_cmd = None
FFmpeg = "ffmpeg"
FILE_list = []
MODIFY_list = []
CONFIG_list = []
def load():
global SRC
global CACHE
global CURL_cmd
global CONFIG_list
global MODIFY_list
data = None
with open("master_info.json", 'r', encoding='utf-8') as f:
data = json.load(f)
SRC = data["src"]
CACHE = data["cache"]
CURL_cmd = data["curlCmd"]
CONFIG_list = data["infoArr"]
for item in CONFIG_list:
MODIFY_list.append(item["file"])
def launch():
global SRC
global FILE_list
global CONFIG_list
ls_list = os.listdir(SRC)
for filename in ls_list:
if "." not in filename:
continue
(basename, extname) = filename.rsplit('.',1) # basename.mkv in the end
if extname in ["mkv","mp4","MP4"]:
FILE_list.append((filename,basename,extname))
ic(FILE_list)
ffmpeg_list = []
for (filename, basename, extname) in FILE_list:
# default
outputname = basename + ".mp4"
param = "-crf 17 -c:a copy"
if filename in MODIFY_list:
# skip modity
continue
filepath = SRC + "\\" + filename
cachepath = CACHE + "\\" + outputname
hashpath = cachepath + ".md5"
filesafe = " \"" + filepath + "\" "
cachesafe = " \"" + cachepath + "\" "
ffmpeg_cmd = FFmpeg + " -i" + filesafe + param + cachesafe
ffmpeg_list.append(ffmpeg_cmd)
for info_item in CONFIG_list:
if info_item['file'] not in ls_list:
print("skip => ", info_item['file'])
continue
filename = info_item['file']
alias = info_item['alias']
param = info_item['param']
(basename, extname) = filename.rsplit('.',1)
if alias == "":
outputname = basename + ".mp4"
else:
outputname = basename + " " + alias + ".mp4"
if param == "":
param = "-crf 17 -c:a copy"
filepath = SRC + "\\" + filename
cachepath = CACHE + "\\" + outputname
hashpath = cachepath + ".md5"
filesafe = " \"" + filepath + "\" "
cachesafe = " \"" + cachepath + "\" "
ffmpeg_cmd = FFmpeg + " -i" + filesafe + param + cachesafe
ffmpeg_list.append(ffmpeg_cmd)
ic(ffmpeg_list)
for item_cmd in ffmpeg_list:
lumos(item_cmd)
# createhash(cachepath)
# filesync(cachepath)
# filesync(hashpath)
def createhash(filepath):
hashhex = None
filesize = os.path.getsize(filepath)
if filesize > 10*1024*1024*1024:
hashhex = "infinite"
else:
with open(filepath, 'rb') as f:
data = f.read()
hasher = hashlib.md5()
hasher.update(data)
hashhex = hasher.hexdigest()
hashpath = filepath + ".md5"
with open(hashpath, 'w', encoding='utf-8') as f:
f.write(hashhex)
def lumos(cmd):
# print(cmd)
# res = 0
pre = "\n🧪 "
logger.debug(pre + cmd)
res = os.system(cmd)
return res
def filesync(filepath):
upload_cmd = CURL_cmd + "\"" + filepath + "\""
# for windows, curl need download
# if os.path.exists('curl.exe'):
# windows_curl = ".\\curl ftp://192.168.3.200/ftp/input/ -u \"anonymous:\" -T "
# upload_cmd = windows_curl + "\"" + filepath + "\""
ic(upload_cmd) # debug
res = os.system(upload_cmd)
return res
def precheck():
global FILE_list
global SRC
for item in FILE_list:
filename = item["file"]
filepath = SRC + "\\" + filename
if not os.path.exists(filepath):
raise Exception("Missing " + filepath)
print("==== Precheck is OK ====")
def pretty_ffmpeg():
global FFmpeg
try:
if os.system("ffmpeg-bar -h") == 0:
FFmpeg = "ffmpeg-bar"
except:
pass
if __name__ == '__main__':
load()
pretty_ffmpeg()
# precheck()
print("Source PATH: \t" + SRC)
print("Output PATH: \t" + CACHE)
print("Info FTP: \t" + CURL_cmd)
# ic("Source PATH: \t" + SRC)
# ic("Output PATH: \t" + CACHE)
# ic("Info FTP: \t" + CURL_cmd)
launch()