네이버 음성 합성 API를 사용할 일이 있어서 문서를 찾아보게 되었습니다.


https://developers.naver.com/docs/labs/tts


음성 파일을 얻어오는 방식은 간단한데 지정된 포맷에 따라 http 통신을 POST 방식으로 Request하고 Response에 있는 파일을 로컬에 저장하면 됩니다.




curl 명령어를 통해서 API를 요청하는 방법은 다음과 같습니다.



curl "https://openapi.naver.com/v1/voice/tts.bin" \
	-d "speaker=mijin&speed=0&text=만나서 반갑습니다." \
	-H "Content-Type: application/x-www-form-urlencoded; charset=UTF-8" \
	-H "X-Naver-Client-Id: {애플리케이션 등록 시 발급받은 client id 값}" \
	-H "X-Naver-Client-Secret: {애플리케이션 등록 시 발급받은 client secret 값}" -v \
		> out.mp3




위의 명렁어를 쉘 스크립트에서 간단하게 작성했습니다. 

#!/bin/sh

argc=$#
argv0=$0
argv1=$1
argv2=$2

curl "https://openapi.naver.com/v1/voice/tts.bin" -d "speaker=mijin&speed=0&text=$argv0" -H "Content-Type: application/x-www-form-urlencoded; charset=UTF-8" -H "X-Naver-Client-Id: $argv1" -H "X-Naver-Client-Secret: $argv2" -v > out.mp3

chmod 775 out.mp3


argv0=$0 이 부분은 스크립트를 실행할 때 인자를 받기 위해 변수를 저장하는 코드입니다. 


리다이렉션을 통해 out.mp3 파일에 음성이 저장되었고 파일 접근 권한을 rwxrwxr-x 로 변경하였습니다.




이를 파이썬에서 실행해봅시다.


파이썬에는 외부의 명렁어를 실행시킬 수 있는 subprocess라는 모듈이 있습니다. 이 글에서는 call 이라는 메소드만 사용하였습니다.


import subprocess
subprocess.call(['./tts.sh', "hello world", "{어플리케이션 clinet id}", "{어플리케이션 clinet secret}"])






블로그 이미지

NCookie

,