TTS 설정: - 음성: Neural2-C (남성) → Neural2-A (여성) - 속도: 1.5x → 1.2x (20% 감속) - 피치: 2.0 → 0.0 (자연스러운 여성 톤) 자막 한글자/한단어 버그 수정: - 최소 청크 길이 10자 보장 (짧은 조각 인접 청크에 병합) - 전체 25자 이하면 분리하지 않고 한 블록으로 표시 - 남은 짧은 버퍼는 마지막 청크에 합치기 - 최소 표시 시간 0.8초 → 1.5초로 증가 - 줄바꿈 기준 14자 → 16자 (가독성 향상) Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
123 lines
3.5 KiB
PHP
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-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;
|
|
}
|
|
}
|