Creating Reels or Shorts using Python and ChatGPT

In today’s digital age, short-form videos have become immensely popular across social media platforms like Instagram, TikTok, and YouTube. These platforms provide an excellent opportunity for creators to express themselves creatively and engage with their audience. One way to create compelling short videos is by using a combination of Python and ChatGPT, a powerful natural language processing model developed by OpenAI. In this blog post, we’ll explore how to leverage Python scripts along with ChatGPT to automate the creation of engaging reels or shorts.

Motivational Quotes Dataset

Before diving into the technical details, let’s start by gathering a collection of motivational quotes. We’ll utilize ChatGPT to generate a diverse set of inspirational quotes. Here’s a Python script to prompt ChatGPT for 100 motivational quotes and store them in a CSV file named “content.csv”:

What You’ll Need

Before we dive into the technical details, let’s make sure we have everything we need:

  • Python: Make sure you have Python installed on your system. You can download and install Python from the official website.
  • MoviePy: MoviePy is a Python library for video editing. You can install it using pip:
pip install moviepy

Getting Started

We’ll start by setting up our environment and importing the necessary libraries. Here’s the Python code to get us started:

import pandas as pd
import random
import os
from moviepy.editor import VideoClip, ImageClip, CompositeVideoClip, AudioFileClip, TextClip, VideoFileClip
from moviepy.video.fx.all import crop, resize

Next, we’ll define some functions to help us create our videos. These functions will handle tasks such as reading data from a CSV file, selecting random video and background music files, and generating text overlays for our videos.

# Read the content data from a CSV file
df = pd.read_csv('content.csv')

# List all video and background music files
videos_directory = os.listdir("Template-Videos")
bgm_directory = os.listdir("Template-bgm")

# Function to create a video with video, background music, and text overlays
def create_video_with_video_bgm_text(video_path, music_path, output_path, quote, heading, duration=5):
    video = VideoFileClip(video_path).subclip(0, duration)
    video = resize(video, height=1920)
    video = crop(video, x1=1166.6, y1=0, x2=2246.6, y2=1920)

    music_clip = AudioFileClip(music_path).subclip(0, duration)

    # Create a heading clip
    heading_clip = TextClip(heading, fontsize=80, size=(video.size[0], None), color='white', font='Caladea-Bold',
                            method="caption")
    heading_clip = heading_clip.set_position(('center', 750))
    heading_clip = heading_clip.set_duration(duration)

    # Create a text clip
    text_clip = TextClip(quote, fontsize=50, size=(video.size[0], None), color='white', font='Caladea-Bold-Italic',
                         method="caption")
    text_clip = text_clip.set_position(('center', 900))
    text_clip = text_clip.set_duration(duration)

    # Combine video, music, heading, and text clips
    video_clip = CompositeVideoClip([video.set_audio(music_clip), heading_clip, text_clip])
    video_clip = video_clip.set_duration(duration)

    # Write the video file
    video_clip.write_videofile(output_path, fps=24)

# Loop through each row in the content data and create a video
for index, row in df.iterrows():
    video_path = "Template-Videos/" + random.choice(videos_directory)
    music_path = "Template-bgm/" + random.choice(bgm_directory)
    heading = "Deep Quotes..."
    quote = row['Quote']
    output_path = f"Result/quotes_{index}.mp4"
    create_video_with_video_bgm_text(video_path, music_path, output_path, quote, heading)

How It Works

Let’s break down the process:

  1. Reading Data: We start by reading content data from a CSV file. This data contains quotes or text that we want to include in our videos.
  2. Selecting Assets: We list all available video and background music files from their respective directories.
  3. Creating Videos: For each row in the content data, we randomly select a video and background music file. We then generate text overlays for the video using the quote from the data. Finally, we combine the video, background music, and text overlays to create a new video.
  4. Writing Output: The generated videos are saved to the specified output directory.

Links

  1. GitHub Repository – https://github.com/ranjithkumarmadhavan/generate-reels-using-python
  2. Youtube – The Madras Programmer

Conclusion

With the power of Python and ChatGPT, creating engaging reels or shorts has never been easier. By automating the video creation process, creators can focus more on crafting compelling content and less on repetitive tasks. Whether you’re a social media influencer, content creator, or aspiring filmmaker, this approach offers a convenient way to produce high-quality videos quickly and efficiently. So why not give it a try and see where your creativity takes you?

Happy creating! 🎬✨

Leave a Comment