(
audioPath: string,
metadata?: SongInfo,
thumbnailPath?: string
)
| 1480 | console.log(` - 艺术家: ${metadata.artist || "无"}`); |
| 1481 | console.log(` - 专辑: ${metadata.album || "无"}`); |
| 1482 | } |
| 1483 | |
| 1484 | // OPUS 格式特殊处理 - 转换为 MP3 以确保兼容性 |
| 1485 | const ext = path.extname(audioPath).toLowerCase(); |
| 1486 | if (ext === ".opus") { |
| 1487 | console.log("[music] OPUS 格式:转换为 MP3 以确保 Telegram 兼容性"); |
| 1488 | const mp3Path = await this.embedMetadataOnly(audioPath, metadata); |
| 1489 | |
| 1490 | // 如果有缩略图,为 MP3 嵌入封面 |
| 1491 | if ( |
| 1492 | thumbnailPath && |
| 1493 | fs.existsSync(thumbnailPath) && |
| 1494 | mp3Path.endsWith(".mp3") |
| 1495 | ) { |
| 1496 | return this.embedCoverToMp3(mp3Path, metadata, thumbnailPath); |
| 1497 | } |
| 1498 | return mp3Path; |
| 1499 | } |
| 1500 | |
| 1501 | try { |
| 1502 | const ext = path.extname(audioPath).toLowerCase(); |
| 1503 | const outputPath = audioPath.replace(ext, `_tagged${ext}`); |
| 1504 | |
| 1505 | // 构建FFmpeg命令 - 添加静默模式 |
| 1506 | const ffmpegArgs: string[] = ['-loglevel', 'error', '-i', audioPath]; |
| 1507 | |
| 1508 | // 添加封面(如果有) |
| 1509 | if (thumbnailPath && fs.existsSync(thumbnailPath)) { |
| 1510 | ffmpegArgs.push('-i', thumbnailPath); |
| 1511 | } |
| 1512 | |
| 1513 | // 复制音频流 - 保持原始编码 |
| 1514 | ffmpegArgs.push('-c:a', 'copy'); |
| 1515 | |
| 1516 | // 添加元数据 |
| 1517 | if (metadata) { |
| 1518 | if (metadata.title && metadata.title !== "Unknown") { |
| 1519 | ffmpegArgs.push('-metadata', `title=${metadata.title}`); |
| 1520 | console.log(`[music] 添加标题: ${metadata.title}`); |
| 1521 | } |
| 1522 | if (metadata.artist && metadata.artist !== "Unknown Artist") { |
| 1523 | ffmpegArgs.push('-metadata', `artist=${metadata.artist}`); |
| 1524 | console.log(`[music] 添加艺术家: ${metadata.artist}`); |
| 1525 | } |
| 1526 | if (metadata.album) { |
| 1527 | ffmpegArgs.push('-metadata', `album=${metadata.album}`); |
| 1528 | console.log(`[music] 添加专辑: ${metadata.album}`); |
| 1529 | } |
| 1530 | // 添加更多元数据 |
| 1531 | ffmpegArgs.push('-metadata', 'comment=Downloaded by TeleBox Music Plugin'); |
| 1532 | ffmpegArgs.push('-metadata', `date=${new Date().getFullYear()}`); |
| 1533 | } |
| 1534 | |
| 1535 | // 嵌入封面 |
| 1536 | if (thumbnailPath && fs.existsSync(thumbnailPath)) { |
| 1537 | // 对于不同格式使用不同的封面嵌入方法 |
| 1538 | if (ext === ".mp3") { |
| 1539 | ffmpegArgs.push('-map', '0:a', '-map', '1:v', '-c:v', 'mjpeg', '-disposition:v', 'attached_pic'); |
no test coverage detected