Files
flypic/backend/utils/videotool.js
T

491 lines
19 KiB
JavaScript
Raw 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.
/**
* 视频处理工具(ffmpeg / ffprobe 封装)
* - probeVideo ffprobe 提取宽高/时长/编码
* - addWatermark 文字水印烧录(drawtext,textfile 避免转义地狱)
* - removeWatermark 区域去水印(delogo)
* - addSubtitleAudio 字幕烧录 + TTS/上传配音(音轨替换或混合)
* - stripSubtitleAudio 移除内嵌字幕流与音轨
* - generateTts edge-tts 朗读(返回 mp3 路径与秒数)
*/
const { spawn } = require('child_process');
const fs = require('fs');
const os = require('os');
const path = require('path');
const logger = require('../src/utils/logger');
const FFMPEG = process.env.FFMPEG_PATH || 'ffmpeg';
const FFPROBE = process.env.FFPROBE_PATH || 'ffprobe';
/** 运行外部命令,超时强杀 */
function run(cmd, args, { timeoutMs = 600 * 1000, onStderr, env } = {}) {
return new Promise((resolve, reject) => {
const child = spawn(cmd, args, { stdio: ['ignore', 'pipe', 'pipe'], env });
let out = '';
let err = '';
const timer = setTimeout(() => {
child.kill('SIGKILL');
reject(new Error(`${cmd} 超时 ${Math.round(timeoutMs / 1000)}s`));
}, timeoutMs);
child.stdout.on('data', (d) => { out += d; });
child.stderr.on('data', (d) => {
err += d;
if (onStderr) onStderr(String(d));
});
child.on('error', (e) => { clearTimeout(timer); reject(e); });
child.on('close', (code) => {
clearTimeout(timer);
if (code === 0) resolve({ stdout: out, stderr: err });
else {
// 剥掉 ffmpeg 版本/banner 行,只留真正的错误
const clean = err.split('\n').filter((l) =>
!/ffmpeg version|configuration:|^\s*lib(av|sw|post)|^\s*\d+\.\s+\d+\.\d+/.test(l)
).join('\n').trim();
reject(new Error(`${cmd} 退出码 ${code}: ${(clean || err).slice(-600)}`));
}
});
});
}
/** ffprobe:视频宽高/时长 */
async function probeVideo(file) {
const { stdout } = await run(FFPROBE, [
'-v', 'error',
'-select_streams', 'v:0',
'-show_entries', 'stream=width,height,codec_name:format=duration',
'-of', 'json',
file
]);
const j = JSON.parse(stdout || '{}');
const s = (j.streams && j.streams[0]) || {};
const dur = parseFloat(j.format && j.format.duration);
return {
width: s.width || 0,
height: s.height || 0,
codec: s.codec_name || '',
duration: Number.isFinite(dur) ? dur : 0
};
}
/** 位置预设 → drawtext x/y 表达式(相对画面,text_w/text_h 为文本尺寸) */
const POSITIONS = {
'top-left': { x: 'w*0.03', y: 'h*0.05' },
'top-center': { x: '(w-text_w)/2', y: 'h*0.05' },
'top-right': { x: 'w-text_w-w*0.03', y: 'h*0.05' },
'center': { x: '(w-text_w)/2', y: '(h-text_h)/2' },
'bottom-left': { x: 'w*0.03', y: 'h-text_h-h*0.06' },
'bottom-center':{ x: '(w-text_w)/2', y: 'h-text_h-h*0.06' },
'bottom-right': { x: 'w-text_w-w*0.03', y: 'h-text_h-h*0.06' }
};
/** 平铺九宫格 x/y 列表(每个单元一格) */
function tilePositions(cols, rows) {
const list = [];
for (let r = 0; r < rows; r++) {
for (let c = 0; c < cols; c++) {
list.push({
x: `w*${(c + 0.5) / cols}-text_w/2`,
y: `h*${(r + 0.5) / rows}-text_h/2`
});
}
}
return list;
}
/**
* 生成 drawtext filter 串
* @param {object} opts 文本/位置/字号%/透明度/描边/旋转/平铺
* @param {number} width 视频像素宽(换算字号)
* @param {number} height 视频像素高
* @param {string} textFile 临时文本文件(UTF-8)
*/
function buildDrawText(opts, width, height, textFile) {
const {
position = 'bottom-right',
fontSizePct = 4, // 字号 = 视频高度的百分比
opacity = 0.6, // 0~1
color = 'white',
stroke = true,
rotate = 0, // 逆时针角度
tile = false
} = opts;
const fontSize = Math.max(10, Math.round((height * fontSizePct) / 100));
const alpha = Math.max(0, Math.min(1, opacity));
const colorHex = /^#?[0-9a-fA-F]{6}$/.test(String(color)) ? color : 'white';
const fg = /^#/.test(colorHex) ? colorHex : colorHex; // drawtext 接受 #RRGGBB 与颜色名
const fgColor = fg.startsWith('#') ? `0x${fg.slice(1)}@${alpha}` : `${fg}@${alpha}`;
const positions = tile ? tilePositions(3, 3) : [POSITIONS[position] || POSITIONS['bottom-right']];
const parts = positions.map((p) => {
const args = [
`fontfile=/usr/share/fonts/opentype/noto/NotoSansCJK-Regular.ttc`,
`textfile=${textFile}`,
`fontsize=${fontSize}`,
`fontcolor=${fgColor}`,
`x=${p.x}`,
`y=${p.y}`
];
if (stroke) {
args.push('borderw=2', 'bordercolor=black@0.55');
}
if (rotate) args.push(`rotate=${(-rotate) * Math.PI / 180}:c=none`);
return `drawtext=${args.join(':')}`;
});
return parts.join(',');
}
/** 生成 SRT(多行文本按行数均分时长) */
function textToSrt(text, durationSec) {
const lines = String(text).split('\n').map((s) => s.trim()).filter(Boolean);
if (!lines.length) throw new Error('字幕文本为空');
const per = durationSec / lines.length;
const fmt = (sec) => {
const ms = Math.max(0, Math.round(sec * 1000));
const h = String(Math.floor(ms / 3600000)).padStart(2, '0');
const m = String(Math.floor((ms % 3600000) / 60000)).padStart(2, '0');
const s = String(Math.floor((ms % 60000) / 1000)).padStart(2, '0');
const mm = String(ms % 1000).padStart(3, '0');
return `${h}:${m}:${s},${mm}`;
};
return lines.map((line, i) =>
`${i + 1}\n${fmt(i * per)} --> ${fmt((i + 1) * per)}\n${line}\n`
).join('\n');
}
/** 临时工作目录 */
function tmpDir() {
return fs.mkdtempSync(path.join(os.tmpdir(), 'flypic-vid-'));
}
/**
* 加水印(烧录)
* @param {string} input 输入视频
* @param {string} output 输出视频
* @param {object} opts { text, position, fontSizePct, opacity, color, stroke, rotate, tile }
*/
async function addWatermark(input, output, opts) {
const text = String(opts.text || '').trim();
if (!text) throw new Error('水印文字不能为空');
const meta = await probeVideo(input);
const dir = tmpDir();
try {
const textFile = path.join(dir, 'wm.txt');
fs.writeFileSync(textFile, text, 'utf8');
const vf = buildDrawText(opts, meta.width || 1280, meta.height || 720, textFile);
await run(FFMPEG, [
'-y', '-i', input,
'-vf', vf,
'-c:v', 'libx264', '-preset', 'veryfast', '-crf', '20',
'-c:a', 'copy',
'-movflags', '+faststart',
output
]);
return meta;
} finally {
fs.rmSync(dir, { recursive: true, force: true });
}
}
/**
* 去水印(delogo 区域平滑填充)
* @param {object} region { x, y, w, h } 百分比(相对画面)
*/
async function removeWatermark(input, output, region) {
const meta = await probeVideo(input);
const W = meta.width || 1280;
const H = meta.height || 720;
const x = Math.max(0, Math.round((region.x / 100) * W));
const y = Math.max(0, Math.round((region.y / 100) * H));
const w = Math.min(W - x, Math.max(6, Math.round((region.w / 100) * W))); // delogo 要求 ≥6px
const h = Math.min(H - y, Math.max(6, Math.round((region.h / 100) * H)));
if (w < 6 || h < 6) throw new Error('去水印区域太小(至少约 10px)');
await run(FFMPEG, [
'-y', '-i', input,
'-vf', `delogo=x=${x}:y=${y}:w=${w}:h=${h}`,
'-c:v', 'libx264', '-preset', 'veryfast', '-crf', '20',
'-c:a', 'copy',
'-movflags', '+faststart',
output
]);
return meta;
}
/** edge-tts 朗读:文本 → mp3,返回 { file, seconds } */
async function generateTts(text, { voice = 'zh-CN-XiaoxiaoNeural', outPath } = {}) {
const content = String(text || '').trim();
if (!content) throw new Error('朗读文本为空');
const target = outPath || path.join(tmpDir(), 'tts.mp3');
const env = { ...process.env };
// 容器内如需代理(edge-tts 直连失败时)可注入 HTTPS_PROXY,不设置则直连
await run('edge-tts', [
'--voice', voice,
'--text', content,
'--write-media', target
], { timeoutMs: 120 * 1000, env });
if (!fs.existsSync(target)) throw new Error('TTS 生成失败');
const meta = await probeVideo(target).catch(() => ({ duration: 0 }));
// mp3 用 ffprobe format.duration 更准
let seconds = meta.duration;
if (!seconds) {
const { stdout } = await run(FFPROBE, ['-v', 'error', '-show_entries', 'format=duration', '-of', 'csv=p=0', target]).catch(() => ({ stdout: '0' }));
seconds = parseFloat(stdout) || 0;
}
return { file: target, seconds };
}
/**
* 添加字幕 + 声音
* - 字幕:多行文本烧录(drawtext,按视频时长均分逐段出现)或整段常驻
* - 声音:ttsText 走 edge-tts 朗读;audioFile 用上传音频
* - audioMode: 'replace' 替换原声 | 'mix' 与原声混合 | 'keep' 只加字幕不动声音
*
* 字幕实现:按配音时长切分视频分段,逐段烧录对应字幕后拼接——保证字幕与朗读同步。
* 若无配音(audioMode keep 且无 tts),字幕整段常驻。
*/
async function addSubtitleAudio(input, output, opts) {
const {
subtitleText = '',
ttsText = '',
voice = 'zh-CN-XiaoxiaoNeural',
audioFile = '', // 前端上传的音频(临时路径)
audioMode = 'tts', // 'tts' | 'file' | 'none'
audioVolume = 1.0,
originalVolume = 1.0, // 混合时原声相对音量
tile = false,
fontSizePct = 5,
opacity = 0.95,
color = 'white'
} = opts;
const text = String(subtitleText || ttsText || '').trim();
if (!text) throw new Error('字幕文本不能为空');
const meta = await probeVideo(input);
const duration = meta.duration;
if (!duration) throw new Error('无法读取视频时长');
const dir = tmpDir();
try {
// 1) 配音
let voiceFile = '';
let voiceSec = 0;
if (audioMode === 'tts' && ttsText) {
const t = await generateTts(ttsText, { voice, outPath: path.join(dir, 'voice.mp3') });
voiceFile = t.file; voiceSec = t.seconds;
} else if (audioMode === 'file' && audioFile && fs.existsSync(audioFile)) {
voiceFile = audioFile;
const { stdout } = await run(FFPROBE, ['-v', 'error', '-show_entries', 'format=duration', '-of', 'csv=p=0', voiceFile]).catch(() => ({ stdout: '0' }));
voiceSec = parseFloat(stdout) || 0;
}
const lines = text.split('\n').map((s) => s.trim()).filter(Boolean);
// 2) 有配音 → 分段字幕与语音同步;无配音 → 整段常驻
if (voiceFile && voiceSec > 0) {
const segCount = Math.max(lines.length, 1);
const segDur = voiceSec / segCount;
const segDir = path.join(dir, 'segs');
fs.mkdirSync(segDir, { recursive: true });
const segFiles = [];
for (let i = 0; i < segCount; i++) {
const tFile = path.join(segDir, `s${i}.txt`);
fs.writeFileSync(tFile, lines[i] || lines[lines.length - 1], 'utf8');
const vf = buildDrawText({
position: 'bottom-center', fontSizePct, opacity, color, stroke: true, tile
}, meta.width || 1280, meta.height || 720, tFile);
const segOut = path.join(segDir, `seg${i}.mp4`);
const args = [
'-y',
'-ss', (i * segDur).toFixed(3), '-t', segDur.toFixed(3), '-i', input,
'-ss', (i * segDur).toFixed(3), '-t', segDur.toFixed(3), '-i', voiceFile,
'-vf', vf,
'-map', '0:v', '-map', '1:a',
'-c:v', 'libx264', '-preset', 'veryfast', '-crf', '20'
];
if (audioVolume !== 1.0) args.push('-filter:a', `volume=${audioVolume}`);
args.push('-c:a', 'aac', '-ar', '44100', '-ac', '2', '-shortest', segOut);
await run(FFMPEG, args);
segFiles.push(segOut);
}
// 拼接(编码参数一致,直接 concat)
const listFile = path.join(segDir, 'list.txt');
fs.writeFileSync(listFile, segFiles.map((f) => `file '${f}'`).join('\n'), 'utf8');
const concatArgs = ['-y', '-f', 'concat', '-safe', '0', '-i', listFile, '-c', 'copy', '-movflags', '+faststart', output];
await run(FFMPEG, concatArgs).catch(async () => {
// 参数不一致兜底:重编码拼接
await run(FFMPEG, ['-y', '-f', 'concat', '-safe', '0', '-i', listFile,
'-c:v', 'libx264', '-preset', 'veryfast', '-crf', '20', '-c:a', 'aac',
'-movflags', '+faststart', output]);
});
return { ...meta, voiceSeconds: voiceSec };
}
// 无配音:字幕常驻 + 可选背景音
const textFile = path.join(dir, 'sub.txt');
fs.writeFileSync(textFile, text, 'utf8');
const vf = buildDrawText({
position: 'bottom-center', fontSizePct, opacity, color, stroke: true, tile
}, meta.width || 1280, meta.height || 720, textFile);
const args = ['-y', '-i', input, '-vf', vf, '-map', '0:v'];
if (audioMode === 'file' && voiceFile) {
args.push('-i', voiceFile, '-map', '1:a',
'-filter:a', `volume=${audioVolume}`,
'-c:a', 'aac', '-shortest');
} else {
args.push('-map', '0:a?');
if (originalVolume !== 1.0) {
args.push('-filter:a', `volume=${originalVolume}`);
}
args.push('-c:a', 'copy');
}
args.push('-c:v', 'libx264', '-preset', 'veryfast', '-crf', '20',
'-movflags', '+faststart', output);
await run(FFMPEG, args);
return meta;
} finally {
fs.rmSync(dir, { recursive: true, force: true });
}
}
/** 去掉字幕流与音轨(-sn -an,视频流直拷) */
async function stripSubtitleAudio(input, output, { removeAudio = true, removeSubtitle = true } = {}) {
const args = ['-y', '-i', input, '-map', '0:v', '-c:v', 'copy'];
if (removeSubtitle) args.push('-sn');
if (removeAudio) args.push('-an');
else args.push('-map', '0:a?', '-c:a', 'copy');
args.push('-movflags', '+faststart', output);
await run(FFMPEG, args);
return probeVideo(input);
}
/**
* AI 生成视频素材装配(分段:图 + 该段字幕 + 该段配音)
* @param {object} seg { imagePath, subtitle, ttsFile, duration } duration=该段秒数
*/
async function buildSegment(seg, outPath, { width = 1920, height = 1080, fontSizePct = 5, subtitleStyle = {} } = {}) {
const dir = tmpDir();
try {
const tFile = path.join(dir, 'sub.txt');
fs.writeFileSync(tFile, seg.subtitle || '', 'utf8');
const textFile = tFile;
const fontSize = Math.max(12, Math.round((height * (subtitleStyle.fontSizePct || fontSizePct)) / 100));
const color = subtitleStyle.color || 'white';
const pos = subtitleStyle.position || 'bottom-center';
const yExpr = pos.startsWith('top') ? 'h*0.07' : pos === 'center' ? '(h-text_h)/2' : 'h-text_h-h*0.07';
const xExpr = pos.endsWith('left') ? 'w*0.05' : pos.endsWith('right') ? 'w-text_w-w*0.05' : '(w-text_w)/2';
const vf = [
`scale=${width}:${height}:force_original_aspect_ratio=decrease`,
`pad=${width}:${height}:(ow-iw)/2:(oh-ih)/2:color=black`,
`drawtext=fontfile=/usr/share/fonts/opentype/noto/NotoSansCJK-Regular.ttc:textfile=${textFile}:fontsize=${fontSize}:fontcolor=${color}@0.95:borderw=2:bordercolor=black@0.6:x=${xExpr}:y=${yExpr}:line_spacing=8`
].join(',');
const args = [
'-y', '-loop', '1', '-t', seg.duration.toFixed(3), '-i', seg.imagePath
];
if (seg.ttsFile) {
args.push('-i', seg.ttsFile, '-map', '0:v', '-map', '1:a', '-c:a', 'aac', '-ar', '44100', '-ac', '2', '-shortest');
} else {
args.push('-f', 'lavfi', '-i', 'anullsrc=channel_layout=stereo:sample_rate=44100', '-map', '0:v', '-map', '1:a', '-shortest');
}
args.push(
'-vf', vf,
'-c:v', 'libx264', '-preset', 'veryfast', '-crf', '20',
'-pix_fmt', 'yuv420p',
'-r', '30',
outPath
);
await run(FFMPEG, args, { timeoutMs: 300 * 1000 });
return outPath;
} finally {
fs.rmSync(dir, { recursive: true, force: true });
}
}
/** 拼接分段 mp4(先 copy,失败重编码) */
async function concatSegments(segFiles, outPath) {
const dir = tmpDir();
try {
const listFile = path.join(dir, 'list.txt');
fs.writeFileSync(listFile, segFiles.map((f) => `file '${f}'`).join('\n'), 'utf8');
try {
await run(FFMPEG, ['-y', '-f', 'concat', '-safe', '0', '-i', listFile, '-c', 'copy', '-movflags', '+faststart', outPath]);
} catch (_) {
await run(FFMPEG, ['-y', '-f', 'concat', '-safe', '0', '-i', listFile,
'-c:v', 'libx264', '-preset', 'veryfast', '-crf', '20', '-c:a', 'aac',
'-pix_fmt', 'yuv420p', '-movflags', '+faststart', outPath],
{ timeoutMs: 600 * 1000 });
}
return outPath;
} finally {
fs.rmSync(dir, { recursive: true, force: true });
}
}
/**
* 带转场拼接分段 mp4(xfade 交叉过渡 + 音频直拼)
* transition: cut/fade/slideleft/slideright/circleopen/dissolve/wipeleft/... (ffmpeg xfade 名)
* 失败时回退为普通硬拼接
*/
async function concatWithTransition(segFiles, outPath, { transition = 'fade', width = 1920, height = 1080 } = {}) {
const n = segFiles.length;
if (n <= 1 || !transition || transition === 'cut') return concatSegments(segFiles, outPath);
const F = 0.6; // 转场时长(秒)
const dir = tmpDir();
try {
// 探测每段真实时长,用于计算 xfade offset
const durs = [];
for (const f of segFiles) {
const p = await probeVideo(f);
durs.push(p.duration > 0.2 ? p.duration : 4);
}
const inputs = [];
for (const f of segFiles) inputs.push('-i', f);
const filters = [];
for (let i = 0; i < n; i++) {
filters.push(`[${i}:v]scale=${width}:${height}:force_original_aspect_ratio=decrease,pad=${width}:${height}:(ow-iw)/2:(oh-ih)/2,setsar=1,format=yuv420p,fps=30[v${i}]`);
}
let prev = 'v0';
let acc = durs[0];
for (let i = 1; i < n; i++) {
const offset = Math.max(0, acc - F);
const out = i === n - 1 ? 'vx' : `x${i}`;
filters.push(`[${prev}][v${i}]xfade=transition=${transition}:duration=${F}:offset=${offset.toFixed(3)}[${out}]`);
prev = out;
acc = acc + durs[i] - F;
}
const audioInputs = segFiles.map((_, i) => `[${i}:a]`).join('');
filters.push(`${audioInputs}concat=n=${n}:v=0:a=1[aout]`);
const args = [
'-y', ...inputs, '-filter_complex', filters.join(';'),
'-map', '[vx]', '-map', '[aout]',
'-c:v', 'libx264', '-preset', 'veryfast', '-crf', '20',
'-pix_fmt', 'yuv420p', '-movflags', '+faststart', outPath
];
await run(FFMPEG, args, { timeoutMs: 600 * 1000 });
return outPath;
} catch (err) {
// 转场失败回退硬拼接,保证出片
logger.warn && logger.warn(`⚠️ 转场拼接失败,回退硬拼接: ${err.message}`);
return concatSegments(segFiles, outPath);
} finally {
fs.rmSync(dir, { recursive: true, force: true });
}
}
module.exports = {
run,
probeVideo,
addWatermark,
removeWatermark,
addSubtitleAudio,
stripSubtitleAudio,
generateTts,
buildSegment,
concatSegments,
concatWithTransition,
textToSrt
};