Files
yaoyaoai/scripts/nodejs/publish_cover_adapt.js
949036910@qq.com c81c4e31e0 11
2026-06-03 18:49:37 +08:00

65 lines
1.7 KiB
JavaScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"use strict";
/**
* 发布前封面适配(对齐 Electron CoverAdapter / PLATFORM_REQUIREMENTS.douyin
*/
const fs = require("fs");
const path = require("path");
const os = require("os");
const { spawnSync } = require("child_process");
const pipelineNative = require("./pipeline_native.js");
const PLATFORM_COVER = {
douyin: { width: 1080, height: 1920, format: "jpg" },
};
function ensurePublishCoverDir() {
const dir = path.join(os.tmpdir(), "aiclient-publish-cover");
fs.mkdirSync(dir, { recursive: true });
return dir;
}
/**
* @param {string} coverPath
* @param {'douyin'|'kuaishou'|'shipin'|'xiaohongshu'} [platform]
* @returns {string}
*/
function adaptCoverForPublish(coverPath, platform = "douyin") {
const req = PLATFORM_COVER[platform];
if (!req) return coverPath;
if (!fs.existsSync(coverPath)) {
throw new Error(`封面文件不存在: ${coverPath}`);
}
const ffmpeg = pipelineNative.locateFfmpeg();
if (!ffmpeg) {
console.warn("[publish_cover_adapt] 未找到 ffmpeg使用原始封面");
return coverPath;
}
const outPath = path.join(
ensurePublishCoverDir(),
`${platform}_${Date.now()}.jpg`,
);
const { width, height } = req;
const vf = `scale=${width}:${height}:force_original_aspect_ratio=decrease,pad=${width}:${height}:(ow-iw)/2:(oh-ih)/2:black`;
const r = spawnSync(
ffmpeg,
["-y", "-i", coverPath, "-vf", vf, "-q:v", "2", outPath],
{ encoding: "utf8", windowsHide: true },
);
if (r.status !== 0 || !fs.existsSync(outPath)) {
console.warn(
"[publish_cover_adapt] ffmpeg 适配失败,使用原始封面:",
(r.stderr || "").slice(0, 200),
);
return coverPath;
}
return outPath;
}
module.exports = { adaptCoverForPublish, PLATFORM_COVER };