File size: 11,258 Bytes
3a5cf48 | 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 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 | # coding: utf-8
# -------------------------------------------------------------------
# 宝塔Linux面板
# -------------------------------------------------------------------
# Copyright (c) 2015-2099 宝塔软件(http://bt.cn) All rights reserved.
# -------------------------------------------------------------------
# Author: sww <cjxin@bt.cn>
# -------------------------------------------------------------------
import json
import os
import sys
import time
import traceback
os.chdir('/www/server/panel')
if not 'class/' in sys.path:
sys.path.insert(0, 'class/')
# 文件格式转化
# ------------------------------
from filesModel.base import filesBase
import public
class main(filesBase):
def __init__(self):
self.convert_type = {
'audio': ['mp3', 'wav', 'flac'],
'video': ['mp4', 'avi', 'mov', 'mkv'],
'image': ['png', 'jpeg', 'jpg', 'bmp', 'tiff', 'webp'],
'all': ['png', 'jpeg', 'jpg', 'bmp', 'tiff', 'webp', 'mp4', 'avi', 'mov', 'mkv', 'mp3', 'wav', 'flac']
}
def convert_audio(self, input_file, output_file):
try:
# 读取输入文件
try:
import librosa
except:
os.system('btpip install librosa')
import librosa
audio, sr = librosa.load(input_file)
# 将音频转换为 WAV 格式
try:
import soundfile as sf
except:
os.system('btpip install soundfile')
import soundfile as sf
sf.write(output_file, audio, sr)
if os.path.exists(output_file):
self.write_log('{};音频;从{}->{};转换成功!'.format(int(time.time()), input_file, output_file))
else:
self.write_log('{};音频;从{}->{};转换失败,未检测到转换后的文件!'.format(int(time.time()), input_file, output_file))
return False
if self.is_del:
os.remove(input_file)
self.write_log('{};音频;从{};已删除源文件!'.format(int(time.time()), input_file))
return True
except:
print(traceback.format_exc())
self.write_log('{};音频;从{}->{};转换失败!'.format(int(time.time()), input_file, output_file))
if os.path.exists(output_file):
os.remove(output_file)
return False
def convert_video(self, input_file, output_file):
try:
try:
import imageio
except:
os.system('btpip install imageio')
import imageio
import imageio
# 读取输入文件
reader = imageio.get_reader(input_file)
# 创建输出文件
try:
meta_data = reader.get_meta_data()
writer = imageio.get_writer(output_file, fps=meta_data["fps"])
except ValueError:
os.system("btpip install imageio[ffmpeg]")
os.system("btpip install imageio[pyav]")
meta_data = reader.get_meta_data()
writer = imageio.get_writer(output_file, fps=meta_data["fps"])
# 读取每一帧并写入输出文件
for frame in reader:
writer.append_data(frame)
# 关闭文件
reader.close()
writer.close()
if os.path.exists(output_file):
self.write_log('{};视频;从{}->{};转换成功!'.format(int(time.time()), input_file, output_file))
else:
self.write_log('{};视频;从{}->{};转换失败,未检测到转换后的文件!'.format(int(time.time()), input_file, output_file))
print('1111')
return False
if self.is_del:
os.remove(input_file)
self.write_log('{};视频;从{};已删除源文件!'.format(int(time.time()), input_file))
return True
except:
print(traceback.format_exc())
self.write_log('{};视频;从{}->{};转换失败!'.format(int(time.time()), input_file, output_file))
if os.path.exists(output_file):
os.remove(output_file)
return False
def convert_image(self, input_file, output_file):
try:
try:
import cv2
except:
os.system('btpip install opencv-python')
import cv2
if output_file.split('.')[-1] in ['ico', 'svg'] or input_file.split('.')[-1] in ['ico', 'svg']:
from PIL import Image
img = Image.open(input_file)
img.save(output_file, format=input_file.split('.')[-1])
import cv2
img = cv2.imread(input_file)
cv2.imwrite(output_file, img)
if os.path.exists(output_file):
self.write_log('{};图片;从{}->{};转换成功!'.format(int(time.time()), input_file, output_file))
else:
self.write_log('{};图片;从{}->{};转换失败,未检测到转换后的文件!'.format(int(time.time()), input_file, output_file))
return False
if self.is_del:
os.remove(input_file)
self.write_log('{};图片;从{};已删除源文件!'.format(int(time.time()), input_file))
return True
except:
print(traceback.format_exc())
self.write_log('{};图片;从{}->{};转换失败!'.format(int(time.time()), input_file, output_file))
if os.path.exists(output_file):
os.remove(output_file)
return False
def run(self, get):
try:
public.set_module_logs('conversion', 'run_conversion', 1)
if not (hasattr(get, 'pdata') and get.pdata != '[]' and get.pdata):
return public.returnMsg(False, '转换列表为空!')
self.is_del = int(get.get('is_save', 0))
pdata = json.loads(get.pdata)
if not pdata:
return public.returnMsg(False, '参数错误!')
result = []
for i in pdata:
# 判断参数是否正确
type = [k for k, v in self.convert_type.items() if i['input_file'].split('.')[-1] in v]
if not type:
result.append({'name': i['input_file'].split('/')[-1], 'status': False})
self.write_log('{};{};从{}->{};转换失败,不支持的格式!'.format(int(time.time()), '未知', i['input_file'], i['output_file']))
continue
if os.path.exists(i['output_file']):
self.write_log('{};{};从{}->{};转换失败,输出地址已存在同名文件!'.format(int(time.time()), '未知', i['input_file'], i['output_file']))
result.append({'name': i['input_file'].split('/')[-1] + '->' + i['output_file'].split('/')[-1], 'status': False})
continue
type = type[0]
if not i['input_file'] or not i['output_file']:
return public.returnMsg(False, '参数错误!')
# 判断源文件和目标文件是否相同
if i['input_file'] == i['output_file']:
return public.returnMsg(False, '源文件和目标文件不能相同!')
# 判断源文件是否存在
if not os.path.exists(i['input_file']):
return public.returnMsg(False, '源文件不存在!')
# 音频格式转换
if type == 'audio':
if i['input_file'].split('.')[-1] not in self.convert_type['audio'] or i['output_file'].split('.')[-1] not in self.convert_type['audio']:
return public.returnMsg(False, '不支持的格式转换!')
if self.convert_audio(i['input_file'], i['output_file']):
result.append({'name': i['input_file'].split('/')[-1] + '->' + i['output_file'].split('/')[-1], 'status': True})
else:
result.append({'name': i['input_file'].split('/')[-1] + '->' + i['output_file'].split('/')[-1], 'status': False})
# 视频格式转换
elif type == 'video':
if i['input_file'].split('.')[-1] not in self.convert_type['video'] or i['output_file'].split('.')[-1] not in self.convert_type['video']:
return public.returnMsg(False, '不支持的格式转换!')
if self.convert_video(i['input_file'], i['output_file']):
result.append({'name': i['input_file'].split('/')[-1] + '->' + i['output_file'].split('/')[-1], 'status': True})
else:
result.append({'name': i['input_file'].split('/')[-1] + '->' + i['output_file'].split('/')[-1], 'status': False})
# 图片格式转换
elif type == 'image':
if i['input_file'].split('.')[-1] not in self.convert_type['image'] or i['output_file'].split('.')[-1] not in self.convert_type['image']:
return public.returnMsg(False, '不支持的格式转换!')
if self.convert_image(i['input_file'], i['output_file']):
result.append({'name': i['input_file'].split('/')[-1] + '->' + i['output_file'].split('/')[-1], 'status': True})
else:
result.append({'name': i['input_file'].split('/')[-1] + '->' + i['output_file'].split('/')[-1], 'status': False})
return public.returnMsg(True, result)
except:
return traceback.format_exc()
return public.returnMsg(False, '转换失败!')
def get_convert_liet(self, get=None):
# 获取转换支持列表
try:
return public.returnMsg(True, self.convert_type)
except:
return public.returnMsg(False, '获取转换支持列表失败!')
def write_log(self, log):
# 写入日志
path = '/www/server/panel/logs/convert.log'
if not os.path.exists(path):
public.writeFile(path, '')
public.writeFile(path, log + '\n', 'a+')
def get_log(self, get=None):
try:
# 获取日志
p = int(get.p) if hasattr(get, 'p') else 1
row = int(get.row) if hasattr(get, 'row') else 10
path = '/www/server/panel/logs/convert.log'
if not os.path.exists(path):
return public.returnMsg(True, '无日志!')
logs = public.readFile(path)
logs = logs.strip().split('\n')
logs.reverse()
logs = [i.split(';') for i in logs]
logs = [{'time': i[0], 'type': i[1], 'operation': i[2], 'status': i[3]} for i in logs if len(i) == 4]
data = public.get_page(len(logs), p, row)
data['data'] = logs[p * row - row: p * row]
return public.returnMsg(True, data)
except:
return traceback.format_exc()
return public.returnMsg(False, '获取日志失败!')
|