본문 바로가기

프로그래밍/Python

간단한 gTTS 사용법 - GoogleTTS python

구글TTS와 파이썬을 사용한 간단한 음성 파일 생성방법 입니다.

 

1. gTTS 모듈이 설치되어 있어야 합니다.

pip install gTTS

 

2. gTTS 사용 코드

input.txt 파일을 만들고 네덜란드어 문구를 저장하고, 네덜란드어 mp3 파일을 만드는 코드입니다.

lang='nl' 에 언어에 맞는 언어 코드를 넣어야 하고, text 도 해당언어와 맞아야 만들어진다.

import time
import os
from gtts import gTTS

curTime = time.strftime('%Y%m%d_%I%M%S', time.localtime())
def makeDir():
    if not os.path.isdir(curTime):
        os.makedirs(curTime)

def tts(bSlow, nickName):
    count = 1
    #open the text data
    f = open("input.txt", 'r')
    lines = f.readlines()
    
    for line in lines:
        # Create mp3
        tts = gTTS(text=line, lang='nl', slow=bSlow)
        tts.save(f'{curTime}/{nickName}_{count}.mp3')
        
        print(f'{line} => {nickName}_{count}.mp3')
        count += 1
    
    f.close()

def main():
    makeDir()
    tts(False, 'n')
    tts(True, 's')

main()

 

 

3. CLI Mode 로 확인하는 방법

https://gtts.readthedocs.io/en/latest/cli.html#gtts-cli

 

gtts-cli [OPTIONS] <text>

 

gtts-cli '안녕' -l ko -o ko.mp3
gtts-cli 'hello' -l en -o en.mp3
gtts-cli -f input.txt -l en -o output.mp3

 

# gtts-cli 사용법

$ gtts-cli '안녕' -l ko --output ko.mp3
$ gtts-cli 'hello' -l en --output en.mp3
$ gtts-cli -f input.txt -l en -o output.mp3


# gtts-cli 도움말

$ gtts-cli -h                   
Usage: gtts-cli [OPTIONS] <text>

  Read <text> to mp3 format using Google Translate's Text-to-Speech API (set
  <text> or --file <file> to - for standard input)

Options:
  -f, --file <file>    Read from <file> instead of <text>.
  -o, --output <file>  Write to <file> instead of stdout.
  -s, --slow           Read more slowly.
  -l, --lang <lang>    IETF language tag. Language to speak in. List
                       documented tags with --all.  [default: en]

  -t, --tld <tld>      Top-level domain for the Google host, i.e
                       https://translate.google.<tld>  [default: com]

  --nocheck            Disable strict IETF language tag checking. Allow
                       undocumented tags.

  --all                Print all documented available IETF language tags and
                       exit. Use --tld beforehand to use an alternate domain

  --debug              Show debug information.
  --version            Show the version and exit.
  -h, --help           Show this message and exit.

 

4. 지원하는 언어

태크명 언어명 언어명
af Afrikaans  
ar Arabic 아랍어
bn Bengali  
bs Bosnian  
ca Catalan  
cs Czech  
cy Welsh  
da Danish  
de German 독일어
el Greek  
en-au English (Australia)  
en-ca English (Canada)  
en-gb English (UK)  
en-gh English (Ghana)  
en-ie English (Ireland)  
en-in English (India)  
en-ng English (Nigeria)  
en-nz English (New Zealand)  
en-ph English (Philippines)  
en-tz English (Tanzania)  
en-uk English (UK)  
en-us English (US)  
en-za English (South Africa)  
en English  
eo Esperanto  
es-es Spanish (Spain)  
es-us Spanish (United States)  
es Spanish  
et Estonian  
fi Finnish  
fr-ca French (Canada)  
fr-fr French (France)  
fr French  
gu Gujarati  
hi Hindi  
hr Croatian  
hu Hungarian  
hy Armenian  
id Indonesian  
is Icelandic  
it Italian  
ja Japanese  
jw Javanese  
km Khmer  
kn Kannada  
ko Korean  
la Latin  
lv Latvian  
mk Macedonian  
ml Malayalam  
mr Marathi  
my Myanmar (Burmese)  
ne Nepali  
nl Dutch 네덜란드어
no Norwegian  
pl Polish  
pt-br Portuguese (Brazil)  
pt-pt Portuguese (Portugal)  
pt Portuguese  
ro Romanian  
ru Russian  
si Sinhala  
sk Slovak  
sq Albanian  
sr Serbian  
su Sundanese  
sv Swedish  
sw Swahili  
ta Tamil  
te Telugu  
th Thai  
tl Filipino  
tr Turkish  
uk Ukrainian  
ur Urdu  
vi Vietnamese  
zh-cn Chinese (Mandarin/China)  
zh-tw Chinese (Mandarin/Taiwan)  

 

5. 가이드 사이트

https://gtts.readthedocs.io/en/latest/index.html

 

 

 

 

반응형