본문 바로가기
+ Development/Python

[Python] 파이썬 gtts Module을 이용한 텍스트 음성 변환(Text to Speech)

by :: Teacher :: 2020. 9. 30.
728x90
반응형

gtts(Google Text-to-Speech)는 구글에서 만든 모듈(Module)이며 이외 네이버에서 제공하는 클로버(Clova) API를 이용하여 하는 방법도 있다.

이번에는 gtts 모듈을 이용하여 텍스트를 음성으로 변환하는 방법을 알아 보도록 하자.

1. 모듈 설치

우선 사용을 하기 위해서는 모듈을 설치 해야한다. 

pip 명령어를 통해서 아래와 같이 설치를 해보도록 하자.

  • pip install gtts
# pip를 이용한 gtts 설치

$ pip install gtts
Collecting gtts
  Using cached gTTS-2.1.1-py3-none-any.whl (25 kB)
Collecting click
  Using cached click-7.1.2-py2.py3-none-any.whl (82 kB)
Collecting gtts-token>=1.1.3
  Using cached gTTS-token-1.1.3.tar.gz (3.4 kB)
Collecting six
  Using cached six-1.15.0-py2.py3-none-any.whl (10 kB)
Collecting beautifulsoup4
  Using cached beautifulsoup4-4.9.2-py3-none-any.whl (115 kB)
Collecting requests
  Using cached requests-2.24.0-py2.py3-none-any.whl (61 kB)
Collecting soupsieve>1.2; python_version >= "3.0"
  Using cached soupsieve-2.0.1-py3-none-any.whl (32 kB)
Collecting chardet<4,>=3.0.2
  Using cached chardet-3.0.4-py2.py3-none-any.whl (133 kB)
Collecting certifi>=2017.4.17
  Using cached certifi-2020.6.20-py2.py3-none-any.whl (156 kB)
Collecting urllib3!=1.25.0,!=1.25.1,<1.26,>=1.21.1
  Using cached urllib3-1.25.10-py2.py3-none-any.whl (127 kB)
Collecting idna<3,>=2.5
  Using cached idna-2.10-py2.py3-none-any.whl (58 kB)
Using legacy 'setup.py install' for gtts-token, since package 'wheel' is not installed.
Installing collected packages: click, chardet, certifi, urllib3, idna, requests, gtts-token, six, soupsieve, beautifulsoup4, gtts
    Running setup.py install for gtts-token ... done
Successfully installed beautifulsoup4-4.9.2 certifi-2020.6.20 chardet-3.0.4 click-7.1.2 gtts-2.1.1 gtts-token-1.1.3 idna-2.10 requests-2.24.0 six-1.15.0 soupsieve-2.0.1 urllib3-1.25.10

728x90

2. 지원하는 언어(IETF)

태크명 언어명
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)

3. 예제 Code

예제 파일은 itschool-info-lab Github와 파일로 올려 놓았다. 

ex_gtts.py
0.00MB

3-1 Python Code

이제 모듈을 설치했으면 간단한 Code를 이용해서 mp3파일을 생성해보도록 하자.

# Python 예제 Code

# -*- coding: utf-8 -*-
from gtts import gTTS
__author__ = 'info-lab'

tts = gTTS(
    text='안녕하세요',
    lang='ko', slow=False
)
tts.save('ex_ko.mp3')

tts1 = gTTS(
    text='Hello',
    lang='en', slow=False
)
tts1.save('ex_en.mp3')

3-2 CLI Mode

해당 모듈을 설치하면 CLI Mode를 통해서 바로 사용도 가능하다.

  • $ 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.

많은 언어를 지원하고 있고 사용방법도 어렵지 않다. 

728x90
반응형

댓글


loading