-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhiroyuki.js
72 lines (60 loc) · 2.84 KB
/
hiroyuki.js
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
const axios = require('axios');
const fs = require('fs');
const path = require('path');
const readline = require('readline');
const COLORS = {
reset: "\x1b[0m",
green: "\x1b[32m",
red: "\x1b[31m",
yellow: "\x1b[33m"
};
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
const SAVE_DIR = path.join(__dirname, 'audio_files');
if (!fs.existsSync(SAVE_DIR)) {
fs.mkdirSync(SAVE_DIR, { recursive: true });
}
rl.question("喋らせるテキストを入力してください: ", async (text) => {
if (!text.trim()) {
console.error(`${COLORS.red}✗ テキストが空です。再度試してください。${COLORS.reset}`);
rl.close();
return;
}
const url = "https://plbwpbyme3.execute-api.ap-northeast-1.amazonaws.com/production/coefonts/19d55439-312d-4a1d-a27b-28f0f31bedc5/try";
try {
const response = await axios.post(url, { text });
if (response.status === 200) {
const audioUrl = response.data.location;
if (audioUrl) {
try {
const audioResponse = await axios.get(audioUrl, { responseType: 'arraybuffer' });
if (audioResponse.status === 200) {
const timestamp = Math.floor(Date.now() / 1000);
const audioFileName = `hitoyuki_audio_${timestamp}.wav`;
const audioFilePath = path.join(SAVE_DIR, audioFileName);
fs.writeFileSync(audioFilePath, audioResponse.data);
console.log(`${COLORS.green}✓ 音声ファイルが正常に保存されました: ${audioFilePath}${COLORS.reset}`);
} else {
console.error(`${COLORS.red}✗ 音声のダウンロードに失敗: ${audioResponse.status}${COLORS.reset}`);
}
} catch (error) {
console.error(`${COLORS.red}✗ 音声のダウンロード中にエラーが発生しました: ${error.message}${COLORS.reset}`);
}
} else {
console.error(`${COLORS.red}✗ 音声URLが取得できませんでした${COLORS.reset}`);
}
} else {
console.error(`${COLORS.red}✗ リクエスト失敗: Status[${response.status}] サーバーが混雑しているかNGワードが含まれています${COLORS.reset}`);
}
} catch (error) {
if (error.response) {
console.error(`${COLORS.red}✗ リクエスト失敗: Status[${error.response.status}] サーバーが混雑しているかNGワードが含まれています${COLORS.reset}`);
} else {
console.error(`${COLORS.red}✗ リクエスト中にエラーが発生しました: ${error.message}${COLORS.reset}`);
}
} finally {
rl.close();
}
});