-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathstep_2_2.py
More file actions
28 lines (21 loc) · 1.36 KB
/
step_2_2.py
File metadata and controls
28 lines (21 loc) · 1.36 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
from pathlib import Path
from google.cloud import texttospeech
from step_1_1 import IN_DIR, OUT_DIR # 이전에 작성한 모듈을 불러옵니다.
from step_2_1 import tts_client
def synth_speech(text: str, voice: str, audio_encoding: str | None = None) -> bytes:
lang_code = "-".join(voice.split("-")[:2]) # 언어 코드(예: 'en-US', 'ko-KO')
MP3, WAV = texttospeech.AudioEncoding.MP3, texttospeech.AudioEncoding.LINEAR16
audio_type = MP3 if audio_encoding == "mp3" else WAV # MP3 또는 WAV
client = tts_client() # TextToSpeechClient 객체 생성
resp = client.synthesize_speech( # 텍스트를 음성으로 변환
input=texttospeech.SynthesisInput(text=text),
voice=texttospeech.VoiceSelectionParams(language_code=lang_code, name=voice),
audio_config=texttospeech.AudioConfig(audio_encoding=audio_type),
)
return resp.audio_content # 음성을 바이트(bytes) 형식으로 반환
if __name__ == "__main__":
text_path = IN_DIR / "billboard.txt" # 'img/billboard.jpg'를 묘사한 텍스트 파일 경로
text = text_path.read_text(encoding="utf-8") # 텍스트 파일에서 텍스트 불러오기
audio = synth_speech(text, "en-GB-Studio-C", "mp3") # 텍스트를 음성으로 변환
with open(OUT_DIR / f"{Path(__file__).stem}.mp3", "wb") as fp:
fp.write(audio) # 음성 파일로 저장