Help us understand the problem. What is going on with this article?

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

作成の動機

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

実際の操作

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
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  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
ユーザーは見つかりませんでした