スキップしてメイン コンテンツに移動

RSSからYoutube/Podcast配信

音声作成

Google Text To Speechを使用した音声作成

記事本文をGoogleの読み上げ機能を使って、音声ファイル(mp3)を作成します。
この作業にはGCPとBloggerの連携の手続きが先に必要です。

Pythonライブラリの追加

$ pip install google-api-python-client
$ pip install google-auth-httplib2
$ pip install google-auth-oauthlib 
$ pip install google-cloud-translate 

Pythonプログラム

test.pyに以下のコードを記述します。
GoogleTTSを使用して日本語テキストをSSMLに変換して音声読み上げをさせます。
'\r\n'を600msのWaitにしているのでタイトルと本文の合間などに調整してください。
test.py
import base64

def getVoice(text):
    tts = get_authenticated_service(TTS_API_SERVICE_NAME,TTS_API_VERSION)
    res = None
    params = {
        "input": {
            "ssml": '<speak>' + text.replace('\r\n','<break time=\"600ms\"/>') + '</speak>'
        },
        "voice": {
            "languageCode": "ja-JP",
            "name": "ja-JP-Wavenet-D"
        },
        "audioConfig": {
            "audioEncoding": "MP3",
            "speakingRate": 1,
            "pitch": 0
        }
    }

    try:
        req = tts.text().synthesize(body=params)
        res = req.execute()

        with open('/tmp/temp.mp3', mode='ab') as f:
            f.write(base64.b64decode(res['audioContent']))

    except Exception as e:
        print (e)
        return None

    return res
以前のgetBody()から上記のgetVoice()を呼び出します。

test.py
def getBody(link):
    try :
        res = requests.get(link)
        extractor.analyse(res.text)
        text, title = extractor.as_text()
        title = re.sub('[-|:|\||\[|\(|\{].*','',title)
        text = re.sub('&.*?;','',text)
        text = getSummary(text)
        title = getTrans(title)
        text = getTrans(text)
        getVoice(title + '\r\n\r\n' + text)
        return postBlog(title,text.replace('\n','<BR>'),'TECHNOLOGY')
    except Exception as e :
        print(e)
        return None

1話分をMP3にした結果はこちら

$ file /tmp/temp.mp3
temp.mp3: MPEG ADTS, layer III, v2,  32 kbps, 24 kHz, Monaural
$ ls -la /tmp/temp.mp3
-rw-r--r-- 1 user user 244320 Oct  9 17:04 /tmp/temp.mp3

これで音声を作成できました。これをポッドキャストにアップロードしてください。
podbeanポッドキャストホスティングサービスと、そのREST APIをつかって自動的にアップロードしていますが、これも移行予定です。
次はこの音声と映像を結び付けて動画作成に入ります。

参考URL:

Cloud Text-to-Speech API  |  Cloud Text-to-Speech Documentation  

このブログの人気の投稿

StableDiffusionを使った画像生成&動画生成

  StableDiffusionが世を賑わかせているので、便乗してニュースのテキストから画像を生成し、TTSの音声と併せて動画にしてみた。 まずは画像生成用のモジュールから。 ! pip install accelerate diffusers transformers scipy ftfy # make sure you're logged in with `huggingface-cli login` from  torch  import  autocast from  diffusers  import  StableDiffusionPipeline import  gc SDpipe = StableDiffusionPipeline.from_pretrained (      "stabilityai/stable-diffusion-2" ,     use_auth_token= "enter your token" ) .to ( "cuda" ) def   getImgFromPrompt ( prompt , imgName ) :     gc.collect ( generation= 0 )     gc.collect ( generation= 1 )     gc.collect ( generation= 2 )     image = SDpipe ( prompt , height= 512 ,  width= 512 ) .images [ 0 ]     display ( image )     image.save ( im...

GPT-3を使った翻訳・要約

GPT-3とは 「Generative Pre-trained Transformer - 3」の略で、イーロン・マスクなどが出資しているOpenAIという団体が出している自然言語処理モデルになります。 以前までは予約しないと使えないものでしたが、今はアカウントを作成すれば誰でも使用することができるようになったので、この翻訳・要約機能を試してみました。 OpenAIのアカウントを作成 Googleアカウントでログインできるため、Googleアカウントを持っている方ならそのまま「Continue with Google」で入ってください。 このままPlaygroundで遊んでも良いですが、Pythonプログラムに使用するため、以下のURLから「Create New Secret Key」を押してAPI-Keyを作成しておいてください。 https://beta.openai.com/account/api-keys Pythonライブラリ openaiのライブラリやこれまでのRSSリーダー機能に必要なライブラリもインストールします。 $ pip install openai feedparser extractcontent3 Pythonプログラム RSSからURLを取得してHTML本文を取得する箇所に関しては以前のものを使いまわします。要約・翻訳のサンプルはPlaygroundから見れますがこれを少し手を加えつつ、パラメータをチューニングしておきました。 # coding: utf-8 import  os import  openai import  requests import  feedparser from  extractcontent3  import  ExtractContent openai.api_key =  "YourOpenAI-API-Key" extractor = ExtractContent () # オプション値を指定する opt =  { "threshold" : 50 } extractor.set_option ( opt )   ...