Automate video creation in Python and Moviepy
Tags: python, moviepy, coding, programming
Published: 2023-09-05 19:26:09

## Hi 🙂🖐

Welcome to a new post, 
Today I will share with you how to automate video creation in Python and Moviepy. Automating the video creation process is very important if you are a content creator and want to save time by creating simple scripts that automate the whole process without using video editing programs.

In this post, I will create a python script to add a list of text and convert it into voices using **pyttsx3** and add voice to video clips to explain what happened in every video clip.

## step 1

Install pyttsx3 and moviepy

`pip install pyttsx3`
`pip install moviepy`


Record every step you want to describe with voice text that is generated by pyttsx3.

For example i create video to explane how to convert text to speesh using pyttsx3.

I created two videos. The first video is about the createin function to convert text to speech, and the second video is about testing the say function.

Save all videos to videos file and rename them.

## step 2

Create a text file called `video_text.txt` to make descriptions for videos and use `#` to split between them.

```
I will use pyttsx3 to convert text to speech an create function called say
#
Now i will to test the function
```

Now we have videos and descriptions 

## step 3
Load text and videos and convert text to speech

```python

import moviepy.editor as mp
import pyttsx3
from os import listdir

VIDEO_NAME = 'how to convert text to speech using python'

# load videos from videos file
videos = [mp.VideoFileClip(f'videos/{p}') for p in listdir('videos')]


# create voice or speech from text
def say(i, text):
    engine = pyttsx3.init()
    engine.save_to_file(text = text, filename = f'voice/{i}.mp3')
    engine.runAndWait()

for i, text in enumerate(open('video_text.txt', 'r').read().replace('\n', '').split('#')):
    say(i, text)
    
```

Now we have voiceover for every video clip we need to add them to videos.

```python
voice_list = [mp.AudioFileClip(f'voice/{p}') for p in listdir('voice')]

video_clips = []

# add audio to all videos
for video, audio in zip(videos, voice_list):
    video_clips.append(video.set_audio(audio))

# save the video
out_video = mp.concatenate_videoclips(video_clips)
out_video.write_videofile(VIDEO_NAME + '.mp4')
``` 

## full code
```python

import moviepy.editor as mp
import pyttsx3
from os import listdir

VIDEO_NAME = 'how to convert text to speech using python'

# load videos from videos file
videos = [mp.VideoFileClip(f'videos/{p}') for p in listdir('videos')]


# create voice or speech from text
def say(i, text):
    engine = pyttsx3.init()
    engine.save_to_file(text = text, filename = f'voice/{i}.mp3')
    engine.runAndWait()

for i, text in enumerate(open('video_text.txt', 'r').read().replace('\n', '').split('#')):
    say(i, text)
    

voice_list = [mp.AudioFileClip(f'voice/{p}') for p in listdir('voice')]

video_clips = []

# add audio to all videos
for video, audio in zip(videos, voice_list):
    video_clips.append(video.set_audio(audio))

# save the video
out_video = mp.concatenate_videoclips(video_clips)
out_video.write_videofile(VIDEO_NAME + '.mp4')
```




**Now we're done 🤗**

**Don't forget to like and follow 🙂**

**Support me on PayPal 🤗**
https://www.paypal.com/paypalme/amr396