googleCloud = $googleCloud; } /** * 텍스트 → MP3 음성 파일 생성 * * @return string|null 저장된 파일 경로 */ public function synthesize(string $text, string $savePath, array $options = []): ?string { $token = $this->googleCloud->getAccessToken(); if (! $token) { Log::error('TtsService: 액세스 토큰 획득 실패'); return null; } try { $languageCode = $options['language_code'] ?? 'ko-KR'; $voiceName = $options['voice_name'] ?? 'ko-KR-Neural2-A'; $speakingRate = $options['speaking_rate'] ?? 1.2; $pitch = $options['pitch'] ?? 0.0; $response = Http::withToken($token) ->timeout(30) ->post('https://texttospeech.googleapis.com/v1/text:synthesize', [ 'input' => [ 'text' => $text, ], 'voice' => [ 'languageCode' => $languageCode, 'name' => $voiceName, ], 'audioConfig' => [ 'audioEncoding' => 'MP3', 'speakingRate' => $speakingRate, 'pitch' => $pitch, 'sampleRateHertz' => 24000, ], ]); if (! $response->successful()) { Log::error('TtsService: TTS API 실패', [ 'status' => $response->status(), 'body' => $response->body(), ]); return null; } $data = $response->json(); $audioContent = $data['audioContent'] ?? null; if (! $audioContent) { Log::error('TtsService: 오디오 데이터 없음'); return null; } $dir = dirname($savePath); if (! is_dir($dir)) { mkdir($dir, 0755, true); } file_put_contents($savePath, base64_decode($audioContent)); Log::info('TtsService: 음성 파일 생성 완료', [ 'path' => $savePath, 'text_length' => mb_strlen($text), ]); return $savePath; } catch (\Exception $e) { Log::error('TtsService: 예외 발생', ['error' => $e->getMessage()]); return null; } } /** * 장면별 일괄 나레이션 생성 * * @param array $scenes [{narration, scene_number}, ...] * @return array [scene_number => file_path, ...] */ public function synthesizeScenes(array $scenes, string $baseDir): array { $results = []; foreach ($scenes as $scene) { $sceneNum = $scene['scene_number'] ?? 0; $narration = $scene['narration'] ?? ''; if (empty($narration)) { continue; } $savePath = "{$baseDir}/narration_{$sceneNum}.mp3"; $result = $this->synthesize($narration, $savePath); if ($result) { $results[$sceneNum] = $result; } } return $results; } }