Files
sam-manage/app/Services/Video/TtsService.php
김보곤 6ab93aedd2 feat:YouTube Shorts AI 자동 생성 시스템 구현 (Veo 3.1 + Gemini)
- GeminiScriptService: 트렌딩 제목/시나리오 생성
- VeoVideoService: Veo 3.1 영상 클립 생성
- TtsService: Google TTS 나레이션 생성
- BgmService: 분위기별 BGM 선택
- VideoAssemblyService: FFmpeg 영상 합성
- VideoGenerationJob: 백그라운드 처리
- Veo3Controller: API 엔드포인트
- React 프론트엔드 (5단계 위저드)
- GoogleCloudService.getAccessToken() public 변경

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-15 08:46:28 +09:00

123 lines
3.5 KiB
PHP

<?php
namespace App\Services\Video;
use App\Services\GoogleCloudService;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Log;
class TtsService
{
private GoogleCloudService $googleCloud;
public function __construct(GoogleCloudService $googleCloud)
{
$this->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-Wavenet-A';
$speakingRate = $options['speaking_rate'] ?? 1.0;
$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;
}
}