mirror of
https://github.com/LLOneBot/LLOneBot.git
synced 2024-11-22 01:56:33 +00:00
Merge branch 'short-video'
This commit is contained in:
commit
82f9a4c63f
@ -4,6 +4,7 @@ import {ConfigUtil} from "./config";
|
|||||||
import util from "util";
|
import util from "util";
|
||||||
import {encode, getDuration, isWav} from "silk-wasm";
|
import {encode, getDuration, isWav} from "silk-wasm";
|
||||||
import fs from 'fs';
|
import fs from 'fs';
|
||||||
|
import * as crypto from 'crypto';
|
||||||
import {v4 as uuidv4} from "uuid";
|
import {v4 as uuidv4} from "uuid";
|
||||||
import ffmpeg from "fluent-ffmpeg"
|
import ffmpeg from "fluent-ffmpeg"
|
||||||
|
|
||||||
@ -234,9 +235,9 @@ export async function encodeSilk(filePath: string) {
|
|||||||
} else {
|
} else {
|
||||||
const pcm = fs.readFileSync(filePath);
|
const pcm = fs.readFileSync(filePath);
|
||||||
let duration = 0;
|
let duration = 0;
|
||||||
try{
|
try {
|
||||||
duration = getDuration(pcm);
|
duration = getDuration(pcm);
|
||||||
}catch (e) {
|
} catch (e) {
|
||||||
log("获取语音文件时长失败", filePath, e.stack)
|
log("获取语音文件时长失败", filePath, e.stack)
|
||||||
duration = fs.statSync(filePath).size / 1024 / 3 // 每3kb大约1s
|
duration = fs.statSync(filePath).size / 1024 / 3 // 每3kb大约1s
|
||||||
duration = Math.floor(duration)
|
duration = Math.floor(duration)
|
||||||
@ -256,6 +257,80 @@ export async function encodeSilk(filePath: string) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export async function getVideoInfo(filePath: string) {
|
||||||
|
const size = fs.statSync(filePath).size;
|
||||||
|
return new Promise<{ width: number, height: number, time: number, format: string, size: number, filePath: string }>((resolve, reject) => {
|
||||||
|
ffmpeg.ffprobe(filePath, (err, metadata) => {
|
||||||
|
if (err) {
|
||||||
|
reject(err);
|
||||||
|
} else {
|
||||||
|
const videoStream = metadata.streams.find(s => s.codec_type === 'video');
|
||||||
|
if (videoStream) {
|
||||||
|
console.log(`视频尺寸: ${videoStream.width}x${videoStream.height}`);
|
||||||
|
} else {
|
||||||
|
console.log('未找到视频流信息。');
|
||||||
|
}
|
||||||
|
resolve({
|
||||||
|
width: videoStream.width, height: videoStream.height,
|
||||||
|
time: parseInt(videoStream.duration),
|
||||||
|
format: metadata.format.format_name,
|
||||||
|
size,
|
||||||
|
filePath
|
||||||
|
});
|
||||||
|
}
|
||||||
|
});
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function encodeMp4(filePath: string) {
|
||||||
|
let videoInfo = await getVideoInfo(filePath);
|
||||||
|
log("视频信息", videoInfo)
|
||||||
|
if (videoInfo.format.indexOf("mp4") === -1) {
|
||||||
|
log("视频需要转换为MP4格式", filePath)
|
||||||
|
// 转成mp4
|
||||||
|
const newPath: string = await new Promise<string>((resolve, reject) => {
|
||||||
|
const newPath = filePath + ".mp4"
|
||||||
|
ffmpeg(filePath)
|
||||||
|
.toFormat('mp4')
|
||||||
|
.on('error', (err) => {
|
||||||
|
reject(`转换视频格式失败: ${err.message}`);
|
||||||
|
})
|
||||||
|
.on('end', () => {
|
||||||
|
log('视频转换为MP4格式完成');
|
||||||
|
resolve(newPath); // 返回转换后的文件路径
|
||||||
|
})
|
||||||
|
.save(newPath);
|
||||||
|
});
|
||||||
|
return await getVideoInfo(newPath)
|
||||||
|
}
|
||||||
|
return videoInfo
|
||||||
|
}
|
||||||
|
|
||||||
export function isNull(value: any) {
|
export function isNull(value: any) {
|
||||||
return value === undefined || value === null;
|
return value === undefined || value === null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
export function calculateFileMD5(filePath: string): Promise<string> {
|
||||||
|
return new Promise((resolve, reject) => {
|
||||||
|
// 创建一个流式读取器
|
||||||
|
const stream = fs.createReadStream(filePath);
|
||||||
|
const hash = crypto.createHash('md5');
|
||||||
|
|
||||||
|
stream.on('data', (data: Buffer) => {
|
||||||
|
// 当读取到数据时,更新哈希对象的状态
|
||||||
|
hash.update(data);
|
||||||
|
});
|
||||||
|
|
||||||
|
stream.on('end', () => {
|
||||||
|
// 文件读取完成,计算哈希
|
||||||
|
const md5 = hash.digest('hex');
|
||||||
|
resolve(md5);
|
||||||
|
});
|
||||||
|
|
||||||
|
stream.on('error', (err: Error) => {
|
||||||
|
// 处理可能的读取错误
|
||||||
|
reject(err);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
|
@ -1,16 +1,20 @@
|
|||||||
import {
|
import {
|
||||||
AtType,
|
AtType,
|
||||||
ElementType, PicType, SendArkElement,
|
ElementType,
|
||||||
|
PicType,
|
||||||
|
SendArkElement,
|
||||||
SendFaceElement,
|
SendFaceElement,
|
||||||
SendFileElement,
|
SendFileElement,
|
||||||
SendPicElement,
|
SendPicElement,
|
||||||
SendPttElement,
|
SendPttElement,
|
||||||
SendReplyElement,
|
SendReplyElement,
|
||||||
SendTextElement
|
SendTextElement,
|
||||||
|
SendVideoElement
|
||||||
} from "./types";
|
} from "./types";
|
||||||
import {NTQQApi} from "./ntcall";
|
import {NTQQApi} from "./ntcall";
|
||||||
import {encodeSilk, isGIF} from "../common/utils";
|
import {calculateFileMD5, encodeSilk, getVideoInfo, isGIF, log, sleep} from "../common/utils";
|
||||||
import * as fs from "node:fs";
|
import {promises as fs} from "node:fs";
|
||||||
|
import ffmpeg from "fluent-ffmpeg"
|
||||||
|
|
||||||
|
|
||||||
export class SendMsgElementConstructor {
|
export class SendMsgElementConstructor {
|
||||||
@ -57,7 +61,7 @@ export class SendMsgElementConstructor {
|
|||||||
|
|
||||||
static async pic(picPath: string): Promise<SendPicElement> {
|
static async pic(picPath: string): Promise<SendPicElement> {
|
||||||
const {md5, fileName, path, fileSize} = await NTQQApi.uploadFile(picPath, ElementType.PIC);
|
const {md5, fileName, path, fileSize} = await NTQQApi.uploadFile(picPath, ElementType.PIC);
|
||||||
if (fileSize === 0){
|
if (fileSize === 0) {
|
||||||
throw "文件异常,大小为0";
|
throw "文件异常,大小为0";
|
||||||
}
|
}
|
||||||
const imageSize = await NTQQApi.getImageSize(picPath);
|
const imageSize = await NTQQApi.getImageSize(picPath);
|
||||||
@ -84,15 +88,9 @@ export class SendMsgElementConstructor {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
static async file(filePath: string, showPreview: boolean = false, fileName: string = ""): Promise<SendFileElement> {
|
static async file(filePath: string, fileName: string = ""): Promise<SendFileElement> {
|
||||||
let picHeight = 0;
|
|
||||||
let picWidth = 0;
|
|
||||||
if (showPreview) {
|
|
||||||
picHeight = 1024;
|
|
||||||
picWidth = 768;
|
|
||||||
}
|
|
||||||
const {md5, fileName: _fileName, path, fileSize} = await NTQQApi.uploadFile(filePath, ElementType.FILE);
|
const {md5, fileName: _fileName, path, fileSize} = await NTQQApi.uploadFile(filePath, ElementType.FILE);
|
||||||
if (fileSize === 0){
|
if (fileSize === 0) {
|
||||||
throw "文件异常,大小为0";
|
throw "文件异常,大小为0";
|
||||||
}
|
}
|
||||||
let element: SendFileElement = {
|
let element: SendFileElement = {
|
||||||
@ -102,28 +100,89 @@ export class SendMsgElementConstructor {
|
|||||||
fileName: fileName || _fileName,
|
fileName: fileName || _fileName,
|
||||||
"filePath": path,
|
"filePath": path,
|
||||||
"fileSize": (fileSize).toString(),
|
"fileSize": (fileSize).toString(),
|
||||||
picHeight,
|
|
||||||
picWidth
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return element;
|
return element;
|
||||||
}
|
}
|
||||||
|
|
||||||
static video(filePath: string, fileName: string=""): Promise<SendFileElement> {
|
static async video(filePath: string, fileName: string = ""): Promise<SendVideoElement> {
|
||||||
return SendMsgElementConstructor.file(filePath, true, fileName);
|
let {fileName: _fileName, path, fileSize, md5} = await NTQQApi.uploadFile(filePath, ElementType.VIDEO);
|
||||||
|
if (fileSize === 0) {
|
||||||
|
throw "文件异常,大小为0";
|
||||||
|
}
|
||||||
|
// const videoInfo = await encodeMp4(path);
|
||||||
|
// path = videoInfo.filePath
|
||||||
|
// md5 = videoInfo.md5;
|
||||||
|
// fileSize = videoInfo.size;
|
||||||
|
// log("上传视频", md5, path, fileSize, fileName || _fileName)
|
||||||
|
const pathLib = require("path");
|
||||||
|
let thumb = path.replace(`${pathLib.sep}Ori${pathLib.sep}`, `${pathLib.sep}Thumb${pathLib.sep}`)
|
||||||
|
thumb = pathLib.dirname(thumb)
|
||||||
|
// log("thumb 目录", thumb)
|
||||||
|
const videoInfo = await getVideoInfo(path);
|
||||||
|
// log("视频信息", videoInfo)
|
||||||
|
const createThumb = new Promise<string>((resolve, reject) => {
|
||||||
|
const thumbFileName = `${md5}_0.png`
|
||||||
|
ffmpeg(filePath)
|
||||||
|
.on("end", () => {
|
||||||
|
})
|
||||||
|
.on("error", (err) => {
|
||||||
|
reject(err);
|
||||||
|
})
|
||||||
|
.screenshots({
|
||||||
|
timestamps: [0],
|
||||||
|
filename: thumbFileName,
|
||||||
|
folder: thumb,
|
||||||
|
size: videoInfo.width + "x" + videoInfo.height
|
||||||
|
}).on("end", () => {
|
||||||
|
resolve(pathLib.join(thumb, thumbFileName));
|
||||||
|
});
|
||||||
|
})
|
||||||
|
let thumbPath = new Map()
|
||||||
|
const _thumbPath = await createThumb;
|
||||||
|
const thumbSize = (await fs.stat(_thumbPath)).size;
|
||||||
|
// log("生成缩略图", _thumbPath)
|
||||||
|
thumbPath.set(0, _thumbPath)
|
||||||
|
const thumbMd5 = await calculateFileMD5(_thumbPath);
|
||||||
|
let element: SendVideoElement = {
|
||||||
|
elementType: ElementType.VIDEO,
|
||||||
|
elementId: "",
|
||||||
|
videoElement: {
|
||||||
|
fileName: fileName || _fileName,
|
||||||
|
filePath: path,
|
||||||
|
videoMd5: md5,
|
||||||
|
thumbMd5,
|
||||||
|
fileTime: videoInfo.time,
|
||||||
|
thumbPath: thumbPath,
|
||||||
|
thumbSize,
|
||||||
|
thumbWidth: videoInfo.width,
|
||||||
|
thumbHeight: videoInfo.height,
|
||||||
|
fileSize: "" + fileSize,
|
||||||
|
// fileUuid: "",
|
||||||
|
// transferStatus: 0,
|
||||||
|
// progress: 0,
|
||||||
|
// invalidState: 0,
|
||||||
|
// fileSubId: "",
|
||||||
|
// fileBizId: null,
|
||||||
|
// originVideoMd5: "",
|
||||||
|
// fileFormat: 2,
|
||||||
|
// import_rich_media_context: null,
|
||||||
|
// sourceVideoCodecFormat: 2
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return element;
|
||||||
}
|
}
|
||||||
|
|
||||||
static async ptt(pttPath: string): Promise<SendPttElement> {
|
static async ptt(pttPath: string): Promise<SendPttElement> {
|
||||||
const {converted, path: silkPath, duration} = await encodeSilk(pttPath);
|
const {converted, path: silkPath, duration} = await encodeSilk(pttPath);
|
||||||
// log("生成语音", silkPath, duration);
|
// log("生成语音", silkPath, duration);
|
||||||
const {md5, fileName, path, fileSize} = await NTQQApi.uploadFile(silkPath, ElementType.PTT);
|
const {md5, fileName, path, fileSize} = await NTQQApi.uploadFile(silkPath, ElementType.PTT);
|
||||||
if (fileSize === 0){
|
if (fileSize === 0) {
|
||||||
throw "文件异常,大小为0";
|
throw "文件异常,大小为0";
|
||||||
}
|
}
|
||||||
if (converted) {
|
if (converted) {
|
||||||
fs.unlink(silkPath, () => {
|
fs.unlink(silkPath).then();
|
||||||
});
|
|
||||||
}
|
}
|
||||||
return {
|
return {
|
||||||
elementType: ElementType.PTT,
|
elementType: ElementType.PTT,
|
||||||
|
@ -28,6 +28,7 @@ export enum ReceiveCmd {
|
|||||||
FRIEND_REQUEST = "nodeIKernelBuddyListener/onBuddyReqChange",
|
FRIEND_REQUEST = "nodeIKernelBuddyListener/onBuddyReqChange",
|
||||||
SELF_STATUS = 'nodeIKernelProfileListener/onSelfStatusChanged',
|
SELF_STATUS = 'nodeIKernelProfileListener/onSelfStatusChanged',
|
||||||
CACHE_SCAN_FINISH = "nodeIKernelStorageCleanListener/onFinishScan",
|
CACHE_SCAN_FINISH = "nodeIKernelStorageCleanListener/onFinishScan",
|
||||||
|
MEDIA_UPLOAD_COMPLETE = "nodeIKernelMsgListener/onRichMediaUploadComplete",
|
||||||
}
|
}
|
||||||
|
|
||||||
interface NTQQApiReturnData<PayloadType = unknown> extends Array<any> {
|
interface NTQQApiReturnData<PayloadType = unknown> extends Array<any> {
|
||||||
@ -224,6 +225,10 @@ registerReceiveHook<{ msgList: Array<RawMessage> }>(ReceiveCmd.NEW_MSG, (payload
|
|||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
for (const msgElement of message.elements) {
|
for (const msgElement of message.elements) {
|
||||||
|
if (msgElement.videoElement) {
|
||||||
|
log("收到视频消息", msgElement.videoElement)
|
||||||
|
log("視頻缩略图", msgElement.videoElement.thumbPath.get(0));
|
||||||
|
}
|
||||||
setTimeout(() => {
|
setTimeout(() => {
|
||||||
const picPath = msgElement.picElement?.sourcePath
|
const picPath = msgElement.picElement?.sourcePath
|
||||||
const pttPath = msgElement.pttElement?.filePath
|
const pttPath = msgElement.pttElement?.filePath
|
||||||
@ -235,6 +240,7 @@ registerReceiveHook<{ msgList: Array<RawMessage> }>(ReceiveCmd.NEW_MSG, (payload
|
|||||||
if (aioOpGrayTipElement){
|
if (aioOpGrayTipElement){
|
||||||
tempGroupCodeMap[aioOpGrayTipElement.peerUid] = aioOpGrayTipElement.fromGrpCodeOfTmpChat;
|
tempGroupCodeMap[aioOpGrayTipElement.peerUid] = aioOpGrayTipElement.fromGrpCodeOfTmpChat;
|
||||||
}
|
}
|
||||||
|
|
||||||
// log("需要清理的文件", pathList);
|
// log("需要清理的文件", pathList);
|
||||||
for (const path of pathList) {
|
for (const path of pathList) {
|
||||||
if (path) {
|
if (path) {
|
||||||
|
@ -71,6 +71,7 @@ export enum ElementType {
|
|||||||
PIC = 2,
|
PIC = 2,
|
||||||
FILE = 3,
|
FILE = 3,
|
||||||
PTT = 4,
|
PTT = 4,
|
||||||
|
VIDEO = 5,
|
||||||
FACE = 6,
|
FACE = 6,
|
||||||
REPLY = 7,
|
REPLY = 7,
|
||||||
ARK = 10,
|
ARK = 10,
|
||||||
@ -167,11 +168,16 @@ export interface FileElement {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export interface SendFileElement {
|
export interface SendFileElement {
|
||||||
elementType: ElementType.FILE,
|
elementType: ElementType.FILE
|
||||||
elementId: "",
|
elementId: "",
|
||||||
fileElement: FileElement
|
fileElement: FileElement
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export interface SendVideoElement {
|
||||||
|
elementType: ElementType.VIDEO
|
||||||
|
elementId: "",
|
||||||
|
videoElement: VideoElement
|
||||||
|
}
|
||||||
export interface SendArkElement {
|
export interface SendArkElement {
|
||||||
elementType: ElementType.ARK,
|
elementType: ElementType.ARK,
|
||||||
elementId: "",
|
elementId: "",
|
||||||
@ -180,7 +186,7 @@ export interface SendArkElement {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export type SendMessageElement = SendTextElement | SendPttElement |
|
export type SendMessageElement = SendTextElement | SendPttElement |
|
||||||
SendPicElement | SendReplyElement | SendFaceElement | SendFileElement | SendArkElement
|
SendPicElement | SendReplyElement | SendFaceElement | SendFileElement | SendVideoElement | SendArkElement
|
||||||
|
|
||||||
export enum AtType {
|
export enum AtType {
|
||||||
notAt = 0,
|
notAt = 0,
|
||||||
@ -258,26 +264,26 @@ export interface FaceElement {
|
|||||||
export interface VideoElement {
|
export interface VideoElement {
|
||||||
"filePath": string,
|
"filePath": string,
|
||||||
"fileName": string,
|
"fileName": string,
|
||||||
"videoMd5": string,
|
"videoMd5"?: string,
|
||||||
"thumbMd5": string
|
"thumbMd5"?: string
|
||||||
"fileTime": 87, // second
|
"fileTime"?: number, // second
|
||||||
"thumbSize": 314235, // byte
|
"thumbSize"?: number, // byte
|
||||||
"fileFormat": 2, // 2表示mp4?
|
"fileFormat"?: number, // 2表示mp4?
|
||||||
"fileSize": string, // byte
|
"fileSize"?: string, // byte
|
||||||
"thumbWidth": number,
|
"thumbWidth"?: number,
|
||||||
"thumbHeight": number,
|
"thumbHeight"?: number,
|
||||||
"busiType": 0, // 未知
|
"busiType"?: 0, // 未知
|
||||||
"subBusiType": 0, // 未知
|
"subBusiType"?: 0, // 未知
|
||||||
"thumbPath": Map<number, any>,
|
"thumbPath"?: Map<number, any>,
|
||||||
"transferStatus": 0, // 未知
|
"transferStatus"?: 0, // 未知
|
||||||
"progress": 0, // 下载进度?
|
"progress"?: 0, // 下载进度?
|
||||||
"invalidState": 0, // 未知
|
"invalidState"?: 0, // 未知
|
||||||
"fileUuid": string, // 可以用于下载链接?
|
"fileUuid"?: string, // 可以用于下载链接?
|
||||||
"fileSubId": "",
|
"fileSubId"?: "",
|
||||||
"fileBizId": null,
|
"fileBizId"?: null,
|
||||||
"originVideoMd5": "",
|
"originVideoMd5"?: "",
|
||||||
"import_rich_media_context": null,
|
"import_rich_media_context"?: null,
|
||||||
"sourceVideoCodecFormat": 0
|
"sourceVideoCodecFormat"?: number
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface TipAioOpGrayTipElement { // 这是什么提示来着?
|
export interface TipAioOpGrayTipElement { // 这是什么提示来着?
|
||||||
|
@ -22,7 +22,7 @@ class BaseAction<PayloadType, ReturnDataType> {
|
|||||||
return OB11Response.ok(resData);
|
return OB11Response.ok(resData);
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
log("发生错误", e)
|
log("发生错误", e)
|
||||||
return OB11Response.error(e.toString(), 200);
|
return OB11Response.error(e?.toString() || e?.stack?.toString() || "未知错误,可能操作超时", 200);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -36,7 +36,7 @@ class BaseAction<PayloadType, ReturnDataType> {
|
|||||||
return OB11Response.ok(resData, echo);
|
return OB11Response.ok(resData, echo);
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
log("发生错误", e)
|
log("发生错误", e)
|
||||||
return OB11Response.error(e.toString(), 1200, echo)
|
return OB11Response.error(e.stack?.toString() || e.toString(), 1200, echo)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -157,14 +157,9 @@ export class SendMsg extends BaseAction<OB11PostSendMsg, ReturnDataType> {
|
|||||||
}
|
}
|
||||||
// log("send msg:", peer, sendElements)
|
// log("send msg:", peer, sendElements)
|
||||||
const {sendElements, deleteAfterSentFiles} = await this.createSendElements(messages, group)
|
const {sendElements, deleteAfterSentFiles} = await this.createSendElements(messages, group)
|
||||||
try {
|
|
||||||
const returnMsg = await this.send(peer, sendElements, deleteAfterSentFiles)
|
const returnMsg = await this.send(peer, sendElements, deleteAfterSentFiles)
|
||||||
deleteAfterSentFiles.map(f => fs.unlink(f, () => {}));
|
deleteAfterSentFiles.map(f => fs.unlink(f, () => {}));
|
||||||
return {message_id: returnMsg.msgShortId}
|
return {message_id: returnMsg.msgShortId}
|
||||||
} catch (e) {
|
|
||||||
log("发送消息失败", e.stack.toString())
|
|
||||||
throw (e.toString())
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
protected convertMessage2List(message: OB11MessageMixType) {
|
protected convertMessage2List(message: OB11MessageMixType) {
|
||||||
@ -372,7 +367,7 @@ export class SendMsg extends BaseAction<OB11PostSendMsg, ReturnDataType> {
|
|||||||
}
|
}
|
||||||
if (sendMsg.type === OB11MessageDataType.file) {
|
if (sendMsg.type === OB11MessageDataType.file) {
|
||||||
log("发送文件", path, payloadFileName || fileName)
|
log("发送文件", path, payloadFileName || fileName)
|
||||||
sendElements.push(await SendMsgElementConstructor.file(path, false, payloadFileName || fileName));
|
sendElements.push(await SendMsgElementConstructor.file(path, payloadFileName || fileName));
|
||||||
} else if (sendMsg.type === OB11MessageDataType.video) {
|
} else if (sendMsg.type === OB11MessageDataType.video) {
|
||||||
log("发送视频", path, payloadFileName || fileName)
|
log("发送视频", path, payloadFileName || fileName)
|
||||||
sendElements.push(await SendMsgElementConstructor.video(path, payloadFileName || fileName));
|
sendElements.push(await SendMsgElementConstructor.video(path, payloadFileName || fileName));
|
||||||
|
Loading…
x
Reference in New Issue
Block a user