0

As a personal project, I decided to create one of the reddit text-to-speech bot.

I pulled all the data from reddit with praw

import praw, random

def scrapeData(subredditName):
    # Instantiate praw
    reddit = praw.Reddit()

    # Get subreddit
    subreddit = reddit.subreddit(subredditName)

    # Get a bunch of posts and convert them into a list
    posts = list(subreddit.new(limit=100))

    # Get random number
    randomNumber = random.randint(0, 100)

    # Store post's title and description in variables
    postTitle = posts[randomNumber].title
    postDesc = posts[randomNumber].selftext

    return postTitle + " " + postDesc

Then, I converted it to speech stored in a .mp3 file with gTTS.

from google.cloud import texttospeech

def convertTextToSpeech(textString):
    # Instantiate TTS
    client = texttospeech.TextToSpeechClient().from_service_account_json("path/to/json")

    # Set text input to be synthesized
    synthesisInput = texttospeech.SynthesisInput(text=textString)

    # Build the voice request
    voice = texttospeech.VoiceSelectionParams(language_code = "en-us", 
         ssml_gender = texttospeech.SsmlVoiceGender.MALE)

    # Select the type of audio file
    audioConfig = texttospeech.AudioConfig(audio_encoding = 
        texttospeech.AudioEncoding.MP3)

    # Perform the TTS request on the text input
    response = client.synthesize_speech(input = synthesisInput, voice = 
        voice, audio_config= audioConfig)

    # Convert from binary to mp3
    with open("output.mp3", "wb") as out:
        out.write(response.audio_content)

I've created an .mp4 with moviepy that has generic footage in the background with the audio synced over it,

from moviepy.editor import *
from moviepy.video.tools.subtitles import SubtitlesClip

# get vide and audio source files
clip = VideoFileClip("background.mp4").subclip(20,30)
audio = AudioFileClip("output.mp3").subclip(0, 10)

# Set audio and create final video
videoClip = clip.set_audio(audio)
videoClip.write_videofile("output.mp4")

but my issue is I can't find a way to have only the current word or sentence displayed on screen as a subtitle, rather than the entire post.

jshel
  • 11
  • 5
  • You complaint seems to be "textString is too long!" Suppose we have `textString = 'display text on the screen'`. It sounds like you want to produce several audio clips, perhaps 'display text' & 'on the screen', and incorporate the sequence of audio clips into the .mp4, one after the other. – J_H Aug 12 '22 at 00:08
  • @J_H Yes that's what I'm going for. Now that you put it like that, I think I was approaching it the wrong way. Rather than converting the whole text to speech then trying to divide it, I should first divided the text by each sentence and then convert it to speech. – jshel Aug 12 '22 at 00:18
  • @jshel were you able to figure this out? – theprogrammer Jun 13 '23 at 21:31
  • @theprogrammer yes, I used this script (https://stackoverflow.com/a/31505798/18549381) to split the text into an array of sentences. Then, I made audio clips from each sentence string. After that, I created subtitles that lasted the length of the audio. – jshel Aug 08 '23 at 04:56

0 Answers0