0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

美形会議をgoogle-homeに演じてもらった

Last updated at Posted at 2020-02-19

#作成の動機
google-homeをpythonでしゃべらせることができるが、ただ単語を1つしゃべっても面白くないので、会話っぽくしゃべらせてみたかった

#美形会議って?
ここの[NEOGEO会議室]を参照

#ソース

talk.py
#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import time
import datetime
import pychromecast
from gtts import gTTS
from mutagen.mp3 import MP3

#IPアドレスでgoogle-homeを指定
googleHome = pychromecast.Chromecast('192.168.0.xx')

if not googleHome.is_idle:
    googleHome.quit_app()
    time.sleep(5)

f = open('./script.txt', 'r') #会話を書いたテキストを用意
line = f.readline()

while line:
  #1行づつしゃべらせる
  print(line.strip())

  savefile = "voice-%s.mp3" % datetime.datetime.now().strftime('%Y%m%d-%H%M%S')
  savepath = "/tts/%s" % savefile

  try:
    #google speech to textで音声データへ変換して保存
    tts = gTTS(text=line.strip(), lang='ja')
    tts.save(savepath)
  except:
    continue
  finally:
    line = f.readline()

  #mp3ファイルの情報を取得
  audio = MP3(savepath)

  #上記のsavepathがwebからアクセス可能な場所として公開
  mp3url = "https://(domainname)/tts/%s" % savefile;

  #google homeにmp3をしゃべらせる
  googleHome.wait()
  googleHome.media_controller.play_media(mp3url, 'audio/mp3')
  googleHome.media_controller.block_until_active()

  #しゃべている間に次の会話が始まると途中でストップするので、
  #会話データの秒数だけ待つ
  time.sleep(audio.info.length)

f.close()

#script.txtのサンプル

やぁ!餓狼伝説の美形キャラ、~
わざわざ説明せんでもええやろうけど~
サムライスピリッツの美形キャラ、橘~

#コマンド

docker run -v /script:/data -v /tts/:/tts toru2220/google-home-python python talk.py

実際の操作

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?