728x90
#Text to Speech #tts #텍스트 음성 변환 #python
ElevenLabs api 를 사용해서
텍스트를 음성으로 변환하는 방법입니다.
전 파이썬 사용했습니다.
목소리나 발음 속도 톤 등 다양하게 변경이 가능합니다.
우선 User API Keys 가 있어야 합니다.
아래 사이트에서 생성가능합니다.
https://elevenlabs.io/app/settings/api-keys
Free Text to Speech & AI Voice Generator | ElevenLabs
Create the most realistic speech with our AI audio tools in 1000s of voices and 32 languages. Easy to use API's and SDK's. Scalable, secure, and customizable voice solutions tailored for enterprise needs. Pioneering research in Text to Speech and AI Voice
elevenlabs.io
Create API Key 창에서 Name 을 입력하고 Create 클릭해주시면 됩니다.
728x90
visual studio code 터미널 창에서
$ pip install elevenlabs
설치해주세요.
api 는 Python Library 를 클릭해 주시면 Git 사이트에서 확인가능합니다.
코드 공유드립니다.
import os
import uuid
from elevenlabs import VoiceSettings
from elevenlabs.client import ElevenLabs
client = ElevenLabs(
api_key="api 키 입력해주세요."
)
def text_to_speech_file(text: str) -> str:
# Calling the text_to_speech conversion API with detailed parameters
response = client.text_to_speech.convert(
voice_id="EXAVITQu4vr4xnSDxMaL",#"pNInz6obpgDQGcFmaJgB", # Adam pre-made voice
output_format="mp3_22050_32",
text=text,
model_id="eleven_turbo_v2_5", # use the turbo model for low latency
voice_settings=VoiceSettings(
stability=0.0,
similarity_boost=1.0,
style=0.0,
use_speaker_boost=True,
),
)
# uncomment the line below to play the audio back
# play(response)
# Generating a unique file name for the output MP3 file
save_file_path = f"{uuid.uuid4()}.mp3"
# Writing the audio to a file
with open(save_file_path, "wb") as f:
for chunk in response:
if chunk:
f.write(chunk)
print(f"{save_file_path}: A new audio file was saved successfully!")
# Return the path of the saved audio file
return save_file_path
text_to_speech_file("안녕하세요. 텍스트를 음성으로 변경해보겠습니다.")
아래는 구현한 영상입니다.
감사합니다.
반응형
'배워보자!! > python' 카테고리의 다른 글
엑셀에 목록 불러와 자동으로 폴더 생성하기, python 프로그램 공유 (0) | 2024.02.14 |
---|
댓글