Automated YouTube Channel with Python
Table of Contents
Goal
An example Python script to make videos out of text. Please ask in the comments for any of your specific use cases. Hope to spark curiosity with this video.
Code Sample - Pageviews by Property
from datetime import datetime
from PIL import Image, ImageDraw, ImageFont
import os
from moviepy.editor import (
concatenate_videoclips,
concatenate_videoclips,
VideoFileClip,
AudioFileClip,
ImageClip,
)
from gtts import gTTS
text_filename = datetime.now().strftime("%Y%m%d_%H_%M_%S")
import os
from pprint import pprint
from textwrap import wrap
import pathlib
class VideoFormat_TextToVideo:
"""Class to handle different video format configurations"""
def __init__(
self,
width,
height,
font_size=60,
lines_per_frame=13,
bg_color=(255, 255, 255),
text_color=(255, 0, 0),
chars_per_line=50,
font_name="arial.ttf",
):
self.width = width
self.height = height
self.font_size = font_size
self.lines_per_frame = lines_per_frame
self.bg_color = bg_color
self.text_color = text_color
self.chars_per_line = chars_per_line
self.font_name = font_name
@classmethod
def desktop(cls):
"""16:9 format suitable for YouTube, standard desktop"""
return cls(
width=1920, height=1080, font_size=60, lines_per_frame=13, chars_per_line=60
)
@classmethod
def shorts(cls):
"""9:16 format suitable for Shorts, Reels, TikTok"""
return cls(
width=1080, height=1920, font_size=50, lines_per_frame=10, chars_per_line=40
)
print(text_filename)
file_array = []
# os.mkdir(path=f"./vids/{text_filename}/")
save_path = f"./vids/{text_filename}/"
import pathlib
save_path2 = pathlib.Path(f"./vids/{text_filename}")
cwd = pathlib.Path.cwd()
nov2file = cwd.joinpath("mangoblogger.txt")
with open(nov2file, "r", encoding="utf-8") as file:
text = file.read()
def text_to_video(
text: str,
languange: str,
font: str,
title: str = None,
):
language = languange
vidarray = []
# https://www.blog.pythonlibrary.org/2021/02/02/drawing-text-on-images-with-pillow-and-python/
desktop_format = VideoFormat_TextToVideo.desktop()
mobile_format = VideoFormat_TextToVideo.shorts()
desktop_format.font_name = font
mobile_format.font_name = font
from textwrap import wrap
size_object = desktop_format
chunks = wrap(text, size_object.chars_per_line)
print(chunks)
for i in range(0, len(chunks), 13):
if i == 0:
titleimg = Image.new(
"RGB", (size_object.width, size_object.height), size_object.bg_color
)
# img = Image.open("./pic.jpg")
I2 = ImageDraw.Draw(titleimg)
myFont = ImageFont.truetype(
size_object.font_name, size_object.font_size + 10
)
y = 50
I2.text((50, y), title, font=myFont, fill=(255, 0, 0))
imagefilename = "bbposter2.jpg"
titleimg.save(f"./title.jpg")
titleobj = gTTS(text=title, lang=language, slow=False)
# Saving the converted audio in a mp3 file named
titleobj.save(f"./title.mp3")
audio_clip = AudioFileClip(f"./title.mp3")
titleimg = ImageClip(f"./title.jpg")
titlevid = titleimg.set_audio(audio_clip)
titlevid.duration = audio_clip.duration
titlevid.fps = 30
vidarray.append(titlevid)
img = Image.new(
"RGB", (size_object.width, size_object.height), size_object.bg_color
)
# img = Image.open("./pic.jpg")
I1 = ImageDraw.Draw(img)
myFont = ImageFont.truetype(size_object.font_name, size_object.font_size)
group = chunks[i : i + 13]
y = 50
for sentence in group:
y += 70
# Add Text to an image
I1.text((50, y), sentence, font=myFont, fill=(255, 0, 0))
imagefilename = str(i) + ".png"
img.save(f"./{imagefilename}")
text = ""
for m in group:
text += m
myobj = gTTS(text=text, lang=language, slow=False)
# Saving the converted audio in a mp3 file named
# welcome
myobj.save(f"./{i}.mp3")
audio_clip = AudioFileClip(f"./{i}.mp3")
image_clip = ImageClip(f"./{imagefilename}")
video_clip = image_clip.set_audio(audio_clip)
video_clip.duration = audio_clip.duration
video_clip.fps = 30
vidarray.append(video_clip)
print(f"{i} Saved {imagefilename}")
# image_clip = ImageClip(f"./bbposter.jpg")
# # Convert image clip to video clip by setting duration and fps
# video_clip = image_clip.set_duration(3) # Set duration to 30 seconds
# video_clip.duration = 5 # Set frames per second to 30
# video_clip.fps = 30
file_array = vidarray
concatenate_videoclips(file_array).write_videofile(f"./texttovideo.mp4", fps=30)
return True
text_to_video(
text,
"en",
"arial.ttf",
"mangoblogger \n\n Like and Subscribe\n\n",
)