| 123 | cleanedText = cleanedText.replace(broadSymbolRegex, ""); |
| 124 | const whitelistRegex = /[^\u4e00-\u9fa5a-zA-Z0-9\s,。?!、,?!.]/g; |
| 125 | cleanedText = cleanedText.replace(whitelistRegex, ""); |
| 126 | cleanedText = cleanedText.replace(/([,。?!、,?!.])\1+/g, "$1"); |
| 127 | return cleanedText.trim(); |
| 128 | } |
| 129 | |
| 130 | // 生成带封面的音乐 |
| 131 | async function generateMusic( |
| 132 | text: string, |
| 133 | referenceId: string, |
| 134 | apiKey: string, |
| 135 | meta: { title: string; artist: string; album: string; cover?: string } |
| 136 | ): Promise<string | null> { |
| 137 | const api_url = "https://api.fish.audio/v1/tts"; |
| 138 | const unique = `${Date.now()}-${Math.random().toString(36).slice(2, 8)}`; |
| 139 | const rawFile = path.join(cacheDir, `tts-${unique}.mp3`); |
| 140 | const finalFile = path.join(cacheDir, `tts-${unique}-meta.mp3`); |
| 141 | |
| 142 | try { |
| 143 | const res = await axios.post( |
| 144 | api_url, |
| 145 | { text, reference_id: referenceId }, |
| 146 | { |
| 147 | headers: { Authorization: `Bearer ${apiKey}`, "Content-Type": "application/json" }, |
| 148 | responseType: "arraybuffer", |
| 149 | } |
| 150 | ); |
| 151 | await fs.writeFile(rawFile, res.data); |
| 152 | |
| 153 | const args: string[] = ["-y", "-i", rawFile]; |
| 154 | |
| 155 | if (meta.cover) { |
| 156 | const coverPath = path.join(cacheDir, `${meta.album}.jpg`); |
| 157 | try { await fs.access(coverPath); } |
| 158 | catch { |
| 159 | const coverRes = await axios.get(meta.cover, { responseType: "arraybuffer" }); |
| 160 | await fs.writeFile(coverPath, coverRes.data); |
| 161 | } |
| 162 | |
| 163 | args.push( |
| 164 | "-i", coverPath, |
| 165 | "-map", "0:a", |
| 166 | "-map", "1:v", |
| 167 | "-c:a", "libmp3lame", |
| 168 | "-q:a", "2", |
| 169 | "-c:v", "mjpeg", |
| 170 | "-id3v2_version", "3", |
| 171 | "-disposition:v", "attached_pic", |
| 172 | "-metadata:s:v", "title=Album cover", |
| 173 | "-metadata:s:v", "comment=Cover (front)" |
| 174 | ); |
| 175 | } else { |
| 176 | args.push("-c:a", "libmp3lame", "-q:a", "2"); |
| 177 | } |
| 178 | |
| 179 | args.push( |
| 180 | "-metadata", `title=${meta.title}`, |
| 181 | "-metadata", `artist=${meta.artist}`, |
| 182 | "-metadata", `album=${meta.album}`, |