perf: 优化视频压缩

This commit is contained in:
kuaifan 2025-04-18 22:28:24 +08:00
parent 00a8514245
commit bdabfdcb3d
4 changed files with 47 additions and 2 deletions

View File

@ -41,7 +41,7 @@ class SystemController extends AbstractController
* @apiParam {String} type
* - get: 获取(默认)
* - all: 获取所有(需要管理员权限)
* - save: 保存设置(参数:['reg', 'reg_identity', 'reg_invite', 'temp_account_alias', 'login_code', 'password_policy', 'project_invite', 'chat_information', 'anon_message', 'voice2text', 'translation', 'e2e_message', 'auto_archived', 'archived_day', 'task_visible', 'task_default_time', 'all_group_mute', 'all_group_autoin', 'user_private_chat_mute', 'user_group_chat_mute', 'system_alias', 'system_welcome', 'image_compress', 'image_quality', 'image_save_local', 'start_home']
* - save: 保存设置(参数:['reg', 'reg_identity', 'reg_invite', 'temp_account_alias', 'login_code', 'password_policy', 'project_invite', 'chat_information', 'anon_message', 'voice2text', 'translation', 'convert_video', 'compress_video', 'e2e_message', 'auto_archived', 'archived_day', 'task_visible', 'task_default_time', 'all_group_mute', 'all_group_autoin', 'user_private_chat_mute', 'user_group_chat_mute', 'system_alias', 'system_welcome', 'image_compress', 'image_quality', 'image_save_local', 'start_home']
* @apiSuccess {Number} ret 返回状态码1正确、0错误
* @apiSuccess {String} msg 返回信息(错误描述)
@ -70,6 +70,8 @@ class SystemController extends AbstractController
'anon_message',
'voice2text',
'translation',
'convert_video',
'compress_video',
'e2e_message',
'msg_rev_limit',
'msg_edit_limit',
@ -136,6 +138,8 @@ class SystemController extends AbstractController
$setting['anon_message'] = $setting['anon_message'] ?: 'open';
$setting['voice2text'] = $setting['voice2text'] ?: 'close';
$setting['translation'] = $setting['translation'] ?: 'close';
$setting['convert_video'] = $setting['convert_video'] ?: 'close';
$setting['compress_video'] = $setting['compress_video'] ?: 'close';
$setting['e2e_message'] = $setting['e2e_message'] ?: 'close';
$setting['msg_rev_limit'] = $setting['msg_rev_limit'] ?: '';
$setting['msg_edit_limit'] = $setting['msg_edit_limit'] ?: '';

View File

@ -879,13 +879,15 @@ class WebSocketDialog extends AbstractModel
Base::makeDir(public_path($path));
copy($filePath, public_path($path) . basename($filePath));
} else {
$setting = Base::setting("system");
$data = Base::upload([
"file" => $files,
"type" => 'more',
"path" => $path,
"fileName" => $fileName,
"quality" => true,
"convertVideo" => true
"convertVideo" => $setting['convert_video'] === 'open',
"compressVideo" => $setting['compress_video'] === 'open',
]);
}
//

View File

@ -2022,6 +2022,7 @@ class Base
chmod=>权限(默认0644),
quality=>压缩图片质量(默认0不压缩),
convertVideo=>转换视频格式(默认false) ,
compressVideo=>压缩视频(默认false如果转换就不压缩) ,
]
* @return array [
name=>原文件名,
@ -2179,6 +2180,7 @@ class Base
}
@shell_exec($command);
if (file_exists($output) && filesize($output) > 0) {
// 压缩后的文件正常
@unlink($array['file']);
$array = array_merge($array, [
"name" => Base::rightReplace($array['name'], ".{$array['ext']}", '.mp4'),
@ -2189,6 +2191,27 @@ class Base
"ext" => 'mp4',
]);
}
$param['compressVideo'] = false; // 如果转换就不压缩
}
if ($param['compressVideo'] && $array['ext'] == 'mp4') {
// 压缩视频
$output = $array['file'] . '_compress';
$command = sprintf("ffmpeg -y -i %s -c:v libx264 -crf 28 -preset medium -c:a aac -b:a 96k %s 2>&1", escapeshellarg($array['file']), escapeshellarg($output));
@shell_exec($command);
if (file_exists($output) && filesize($output) > 0) {
// 压缩后的文件正常
if (filesize($output) < filesize($array['file'])) {
// 小于原文件
@unlink($array['file']);
$array = array_merge($array, [
"size" => Base::twoFloat(filesize($output) / 1024, true),
"file" => $output,
]);
} else {
// 大于原文件
@unlink($output);
}
}
}
if (in_array($array['ext'], ['mov', 'webm', 'mp4'])) {
// 视频尺寸

View File

@ -198,6 +198,22 @@
<div v-if="formDatum.translation == 'open'" class="form-tip">{{$L('长按文本消息可翻译成当前设置的语言')}} ({{$L('需要在应用中开启 ChatGPT AI 机器人')}})</div>
<div v-else class="form-tip">{{$L('关闭文本消息翻译功能。')}}</div>
</FormItem>
<FormItem :label="$L('视频转换')" prop="convertVideo">
<RadioGroup v-model="formDatum.convert_video">
<Radio label="open">{{$L('开启')}}</Radio>
<Radio label="close">{{$L('关闭')}}</Radio>
</RadioGroup>
<div v-if="formDatum.convert_video == 'open'" class="form-tip">{{$L('将MOVWEBM格式的视频转换为MP4格式')}}</div>
<div v-else class="form-tip">{{$L('关闭视频格式转换功能。')}}</div>
</FormItem>
<FormItem :label="$L('视频压缩')" prop="compressVideo">
<RadioGroup v-model="formDatum.compress_video">
<Radio label="open">{{$L('开启')}}</Radio>
<Radio label="close">{{$L('关闭')}}</Radio>
</RadioGroup>
<div v-if="formDatum.compress_video == 'open'" class="form-tip">{{$L('对MP4格式的视频进行压缩处理')}}</div>
<div v-else class="form-tip">{{$L('关闭视频压缩功能。')}}</div>
</FormItem>
<FormItem :label="$L('端到端加密')" prop="e2eMessage">
<RadioGroup v-model="formDatum.e2e_message">
<Radio label="open">{{$L('开启')}}</Radio>