_fix_voices.js
4.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
const fs = require('fs');
const path = require('path');
// Load env
const envPath = path.join(__dirname, '.env');
fs.readFileSync(envPath, 'utf8').split('\n').forEach(l => {
const m = l.match(/^([^#=]+)=(.*)$/);
if (m) process.env[m[1].trim()] = m[2].trim();
});
const { Sequelize } = require('sequelize');
const updates = [
{ id: 553, voice_id: 'zh_male_dayi_uranus_bigtts' }, // President - 成熟沉稳
{ id: 554, voice_id: 'en_female_dacey_uranus_bigtts' }, // Press Secretary - 职业女性
{ id: 555, voice_id: 'en_male_tim_uranus_bigtts' }, // Reporter - 年轻男记者
{ id: 556, voice_id: 'zh_male_ruyayichen_uranus_bigtts' }, // Doctor - 儒雅紧张
{ id: 557, voice_id: 'zh_male_dayi_uranus_bigtts' }, // _default - same as President
];
(async () => {
const s = new Sequelize(process.env.DB_NAME, process.env.DB_USER, process.env.DB_PASSWORD, {
host: process.env.DB_HOST, port: +process.env.DB_PORT, dialect: 'mysql', logging: false
});
for (const u of updates) {
await s.query('UPDATE drama_characters SET voice_id = ? WHERE id = ?', { replacements: [u.voice_id, u.id] });
}
// Verify
const [rows] = await s.query('SELECT id, character_name, voice_id FROM drama_characters WHERE drama_id = 99 ORDER BY id');
rows.forEach(r => console.log(`${r.character_name}: ${r.voice_id}`));
// Now regenerate bundle - read insert-drama.js logic for bundle generation
// Get drama info
const [dramas] = await s.query('SELECT * FROM dramas WHERE id = 99');
const drama = dramas[0];
const dramaKey = drama.drama_key;
// Get characters
const [chars] = await s.query('SELECT * FROM drama_characters WHERE drama_id = 99 ORDER BY id');
// Get scripts
const [scripts] = await s.query('SELECT * FROM drama_scripts WHERE drama_id = 99 ORDER BY seq_order');
// Get emotions
const [emotions] = await s.query('SELECT * FROM drama_emotions WHERE drama_id = 99');
const baseLang = drama.language || 'zh';
const extractTranslations = (v) => v ? { [baseLang]: v } : {};
const bundle = {
meta: {
id: dramaKey,
title: extractTranslations(drama.title),
subtitle: extractTranslations(drama.subtitle),
description: extractTranslations(drama.description),
system_prompt: drama.system_prompt,
cover_url: drama.cover_url,
background_url: drama.background_url,
bgm_url: drama.bgm_url || null,
type: drama.type || 'classic',
language: baseLang,
},
characters: {},
script: [],
};
for (const char of chars) {
const charKey = char.character_name === '_default' ? '_default' : char.character_name;
const faces = {};
const charEmotions = emotions.filter(e =>
(char.character_name === '_default' ? e.character_name === null : e.character_name === char.character_name) ||
(!e.character_name)
);
for (const e of charEmotions) {
if (e.avatar_key) {
faces[e.avatar_key] = { url: e.face_url, emotion: e.emotion_name, scale: e.emotion_scale };
}
}
// Use shared emotions for non-default chars
if (char.character_name !== '_default' && Object.keys(faces).length === 0) {
const shared = emotions.filter(e => !e.character_name);
for (const e of shared) {
if (e.avatar_key) {
faces[e.avatar_key] = { url: e.face_url, emotion: e.emotion_name, scale: e.emotion_scale };
}
}
}
const voiceIdMap = {};
if (char.voice_id) voiceIdMap['zh'] = char.voice_id;
if (char.voice_id_en) voiceIdMap['en'] = char.voice_id_en;
bundle.characters[charKey] = {
id: char.id,
name: char.character_name === '_default' ? null : extractTranslations(char.character_name),
voice_id: Object.keys(voiceIdMap).length > 0 ? voiceIdMap : null,
body_url: char.body_url,
face_offset: { top: char.face_offset_top, left: char.face_offset_left, width: char.face_offset_width },
faces,
};
}
for (const sc of scripts) {
bundle.script.push({
character: sc.character_name,
dialogue: extractTranslations(sc.dialogue),
avatar: sc.avatar_key || 'normal.png',
action: sc.action || 'none',
position: sc.position || 'left',
instruction: sc.instruction || null,
});
}
// Upload to OSS
const OSS = require('ali-oss');
const client = new OSS({
region: process.env.OSS_REGION,
accessKeyId: process.env.OSS_ACCESS_KEY_ID,
accessKeySecret: process.env.OSS_ACCESS_KEY_SECRET,
bucket: process.env.OSS_BUCKET,
});
const bundleJson = JSON.stringify(bundle, null, 2);
const ossPath = `dramas/${dramaKey}/bundle.json`;
await client.put(ossPath, Buffer.from(bundleJson));
console.log(`\nBundle 已重新上传: https://${process.env.OSS_BUCKET}.${process.env.OSS_REGION}.aliyuncs.com/${ossPath}`);
await s.close();
})();