Update src/api-zalo/apis/sendVideov2.js
Browse files- src/api-zalo/apis/sendVideov2.js +72 -59
src/api-zalo/apis/sendVideov2.js
CHANGED
|
@@ -1,43 +1,71 @@
|
|
|
|
|
| 1 |
import { appContext } from "../context.js";
|
| 2 |
import { ZaloApiError } from "../Errors/ZaloApiError.js";
|
| 3 |
import { encodeAES, handleZaloResponse, makeURL, request } from "../utils.js";
|
| 4 |
import { Zalo } from "../index.js";
|
| 5 |
import { MessageType } from "../models/Message.js";
|
| 6 |
-
import ffmpeg from "fluent-ffmpeg";
|
| 7 |
|
| 8 |
-
|
| 9 |
-
ffmpeg.setFfmpegPath(process.env.FFMPEG_PATH || "/usr/bin/ffmpeg");
|
| 10 |
-
ffmpeg.setFfprobePath(process.env.FFPROBE_PATH || "/usr/bin/ffprobe");
|
| 11 |
|
| 12 |
-
|
| 13 |
return new Promise((resolve, reject) => {
|
| 14 |
-
|
| 15 |
-
if (
|
| 16 |
-
|
|
|
|
| 17 |
return;
|
| 18 |
}
|
| 19 |
-
|
| 20 |
-
const videoStream =
|
| 21 |
-
metadata.streams.find((stream) => stream.codec_type === "video") ||
|
| 22 |
-
metadata.streams[0] ||
|
| 23 |
-
{};
|
| 24 |
-
|
| 25 |
-
const duration =
|
| 26 |
-
Number(metadata.format?.duration || videoStream.duration || 0) * 1000;
|
| 27 |
-
|
| 28 |
-
const width = Number(videoStream.width || 1280);
|
| 29 |
-
const height = Number(videoStream.height || 720);
|
| 30 |
-
const fileSize = Number(metadata.format?.size || 0);
|
| 31 |
-
|
| 32 |
-
resolve({
|
| 33 |
-
duration,
|
| 34 |
-
width,
|
| 35 |
-
height,
|
| 36 |
-
fileSize
|
| 37 |
-
});
|
| 38 |
});
|
| 39 |
});
|
| 40 |
-
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
|
| 42 |
export function sendVideov2Factory(api) {
|
| 43 |
const directMessageServiceURL = makeURL(`${api.zpwServiceMap.file[0]}/api/message/forward`, {
|
|
@@ -64,30 +92,12 @@ export function sendVideov2Factory(api) {
|
|
| 64 |
if (!appContext.cookie) throw new ZaloApiError("Cookie is not available");
|
| 65 |
if (!appContext.userAgent) throw new ZaloApiError("User agent is not available");
|
| 66 |
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
try {
|
| 74 |
-
const info = await getVideoInfo(videoUrl);
|
| 75 |
-
|
| 76 |
-
duration = info.duration || 0;
|
| 77 |
-
width = info.width || 1280;
|
| 78 |
-
height = info.height || 720;
|
| 79 |
-
fileSize = info.fileSize || 0;
|
| 80 |
-
thumbnailUrl = videoUrl.replace(/\.[^/.]+$/, ".jpg") || null;
|
| 81 |
-
} catch (error) {
|
| 82 |
-
console.error("FFPROBE ERROR, dùng metadata mặc định:", error?.message || error);
|
| 83 |
-
|
| 84 |
-
// Không throw nữa để tránh chết lệnh gửi video
|
| 85 |
-
duration = 0;
|
| 86 |
-
width = 1280;
|
| 87 |
-
height = 720;
|
| 88 |
-
fileSize = 0;
|
| 89 |
-
thumbnailUrl = videoUrl.replace(/\.[^/.]+$/, ".jpg") || null;
|
| 90 |
-
}
|
| 91 |
|
| 92 |
const payload = {
|
| 93 |
params: {
|
|
@@ -97,11 +107,14 @@ export function sendVideov2Factory(api) {
|
|
| 97 |
msgType: 5,
|
| 98 |
msgInfo: JSON.stringify({
|
| 99 |
videoUrl: String(videoUrl),
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
|
|
|
|
|
|
|
|
|
| 105 |
properties: {
|
| 106 |
color: -1,
|
| 107 |
size: -1,
|
|
@@ -113,12 +126,12 @@ export function sendVideov2Factory(api) {
|
|
| 113 |
msg_warning_type: 0,
|
| 114 |
},
|
| 115 |
},
|
| 116 |
-
title: message
|
| 117 |
}),
|
| 118 |
},
|
| 119 |
};
|
| 120 |
|
| 121 |
-
if (message
|
| 122 |
payload.params.mentionInfo = message.mention;
|
| 123 |
}
|
| 124 |
|
|
|
|
| 1 |
+
import { execFile } from "child_process";
|
| 2 |
import { appContext } from "../context.js";
|
| 3 |
import { ZaloApiError } from "../Errors/ZaloApiError.js";
|
| 4 |
import { encodeAES, handleZaloResponse, makeURL, request } from "../utils.js";
|
| 5 |
import { Zalo } from "../index.js";
|
| 6 |
import { MessageType } from "../models/Message.js";
|
|
|
|
| 7 |
|
| 8 |
+
const FFPROBE_PATH = process.env.FFPROBE_PATH || "/usr/bin/ffprobe";
|
|
|
|
|
|
|
| 9 |
|
| 10 |
+
function execFileAsync(cmd, args) {
|
| 11 |
return new Promise((resolve, reject) => {
|
| 12 |
+
execFile(cmd, args, { timeout: 15000 }, (error, stdout, stderr) => {
|
| 13 |
+
if (error) {
|
| 14 |
+
error.stderr = stderr;
|
| 15 |
+
reject(error);
|
| 16 |
return;
|
| 17 |
}
|
| 18 |
+
resolve(stdout);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
});
|
| 20 |
});
|
| 21 |
+
}
|
| 22 |
+
|
| 23 |
+
async function getRemoteFileSize(url) {
|
| 24 |
+
try {
|
| 25 |
+
const res = await fetch(url, { method: "HEAD" });
|
| 26 |
+
const size = Number(res.headers.get("content-length") || 0);
|
| 27 |
+
return size > 0 ? size : 1024 * 1024;
|
| 28 |
+
} catch {
|
| 29 |
+
return 1024 * 1024;
|
| 30 |
+
}
|
| 31 |
+
}
|
| 32 |
+
|
| 33 |
+
async function getVideoInfo(url) {
|
| 34 |
+
try {
|
| 35 |
+
const stdout = await execFileAsync(FFPROBE_PATH, [
|
| 36 |
+
"-v", "quiet",
|
| 37 |
+
"-print_format", "json",
|
| 38 |
+
"-show_format",
|
| 39 |
+
"-show_streams",
|
| 40 |
+
url
|
| 41 |
+
]);
|
| 42 |
+
|
| 43 |
+
const metadata = JSON.parse(stdout);
|
| 44 |
+
const videoStream =
|
| 45 |
+
metadata.streams?.find((s) => s.codec_type === "video") ||
|
| 46 |
+
metadata.streams?.[0] ||
|
| 47 |
+
{};
|
| 48 |
+
|
| 49 |
+
return {
|
| 50 |
+
duration: Math.max(
|
| 51 |
+
1000,
|
| 52 |
+
Math.round(Number(metadata.format?.duration || videoStream.duration || 10) * 1000)
|
| 53 |
+
),
|
| 54 |
+
width: Number(videoStream.width || 1280),
|
| 55 |
+
height: Number(videoStream.height || 720),
|
| 56 |
+
fileSize: Number(metadata.format?.size || await getRemoteFileSize(url))
|
| 57 |
+
};
|
| 58 |
+
} catch (error) {
|
| 59 |
+
console.error("FFPROBE lỗi, dùng metadata mặc định:", error?.message || error);
|
| 60 |
+
|
| 61 |
+
return {
|
| 62 |
+
duration: 10000,
|
| 63 |
+
width: 1280,
|
| 64 |
+
height: 720,
|
| 65 |
+
fileSize: await getRemoteFileSize(url)
|
| 66 |
+
};
|
| 67 |
+
}
|
| 68 |
+
}
|
| 69 |
|
| 70 |
export function sendVideov2Factory(api) {
|
| 71 |
const directMessageServiceURL = makeURL(`${api.zpwServiceMap.file[0]}/api/message/forward`, {
|
|
|
|
| 92 |
if (!appContext.cookie) throw new ZaloApiError("Cookie is not available");
|
| 93 |
if (!appContext.userAgent) throw new ZaloApiError("User agent is not available");
|
| 94 |
|
| 95 |
+
const info = await getVideoInfo(videoUrl);
|
| 96 |
+
|
| 97 |
+
const duration = Number(info.duration || 10000);
|
| 98 |
+
const width = Number(info.width || 1280);
|
| 99 |
+
const height = Number(info.height || 720);
|
| 100 |
+
const fileSize = Number(info.fileSize || 1024 * 1024);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 101 |
|
| 102 |
const payload = {
|
| 103 |
params: {
|
|
|
|
| 107 |
msgType: 5,
|
| 108 |
msgInfo: JSON.stringify({
|
| 109 |
videoUrl: String(videoUrl),
|
| 110 |
+
|
| 111 |
+
// Không tự chế .jpg nữa vì link đó thường không tồn tại => Zalo báo tham số không hợp lệ
|
| 112 |
+
thumbUrl: String(videoUrl),
|
| 113 |
+
|
| 114 |
+
duration,
|
| 115 |
+
width,
|
| 116 |
+
height,
|
| 117 |
+
fileSize,
|
| 118 |
properties: {
|
| 119 |
color: -1,
|
| 120 |
size: -1,
|
|
|
|
| 126 |
msg_warning_type: 0,
|
| 127 |
},
|
| 128 |
},
|
| 129 |
+
title: message?.text || "",
|
| 130 |
}),
|
| 131 |
},
|
| 132 |
};
|
| 133 |
|
| 134 |
+
if (message?.mention) {
|
| 135 |
payload.params.mentionInfo = message.mention;
|
| 136 |
}
|
| 137 |
|