Qiita Teams that are logged in
You are not logged in to any team

Log in to Qiita Team
Community
OrganizationAdvent CalendarQiitadon (β)
Service
Qiita JobsQiita ZineQiita Blog
1
Help us understand the problem. What is going on with this article?
@toru2220

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

More than 1 year has passed since last update.

作成の動機

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

実際の操作

1
Help us understand the problem. What is going on with this article?
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
toru2220

Comments

No comments
Sign up for free and join this conversation.
Sign Up
If you already have a Qiita account Login
1
Help us understand the problem. What is going on with this article?