technical case study

Automation • Short videos / Pipeline

Factory Shorts

A Python pipeline that turns scripts into vertical short videos, with burned-in subtitles, a random background and batch generation. There are two versions: one using AI APIs and one using only ffmpeg.

Automation and Python scripting Prototype Two versions: March 2025 and January 2026
Python ffmpeg MoviePy OpenAI API Pexels API gTTS
executive summary

Problem

Producing short videos regularly takes time: writing, finding a background, narrating or subtitling, and editing each video by hand.

Solution

A script that puts these steps into a single flow. Version 1 generated the text with AI, fetched the image and narrated it; version 2 reads ready-made scripts and renders all of them at once, with subtitles.

Result

From version 1 to version 2, the pipeline stopped depending on paid APIs and the internet and started generating videos in batches, one per script.

Current stage

Working prototype. Next steps: subtitles sized and padded for vertical screens, background music and a fixed 1080 × 1920 format.

context

From a motivational video to a batch of shorts

The first version, from March 2025, was an end-to-end experiment: it asked GPT-4 for a motivational quote, fetched a nature image from Pexels, generated the narration with gTTS and assembled a 10-second video with MoviePy. The second, from January 2026, changed the approach: the scripts are written beforehand, in text files, and the code only handles the repetitive part, subtitling and rendering each one with ffmpeg.

goals and constraints

Goals

  • Turn text into video without manual editing
  • Generate several videos at once from a folder of scripts
  • Vary the background automatically
  • Keep the flow simple: one command and the videos show up in the out folder

Constraints

  • Version 1 depended on three external services and API keys
  • Format and resolution follow the randomly picked background video
  • Fixed-size subtitles can get too large on a vertical screen
  • Music is not applied yet in version 2
solution architecture

Front-end

No interface: the output is the video itself. In version 2, subtitles are burned into the image, centered, in Arial 42.

Back-end

Version 1: Python calling OpenAI, Pexels, gTTS and MoviePy in sequence. Version 2: Python generates an .srt for each script and calls ffmpeg to loop the background, apply the subtitles and cut the video at 30 seconds.

Database

No database: scripts in scripts/, backgrounds and music in assets/ and finished videos in out/.

security and safeguards

Keys out of the code

In version 1, the API keys lived in the script itself. Version 2 needs no keys at all; if a future version uses APIs again, they go into environment variables.

Runs locally

Version 2 runs entirely on the machine, without sending the scripts to external services.

No personal data

The pipeline only handles script text and background media; no user data is collected.

process
01

First experiment

March 2025: a quote from GPT-4, an image from Pexels, a voice from gTTS and a video from MoviePy.

02

Version 1 limits

Dependence on paid APIs, a still image and a single video per run.

03

Batch pipeline

January 2026: scripts in .txt, .srt subtitles and ffmpeg rendering for the whole folder.

04

Next steps

A fixed vertical format, subtitles fitted to the screen and a background track.

behind the scenes
auto_shorts/run.py Python lines 13–21
def text_to_srt(text, path):
    lines = text.strip().split("\n")
    with open(path, "w", encoding="utf-8") as f:
        t = 0
        for i, line in enumerate(lines, 1):
            start = f"00:00:{t:02},000"
            end = f"00:00:{t+3:02},000"
            f.write(f"{i}\n{start} --> {end}\n{line}\n\n")
            t += 3
Each line of the script becomes a 3-second subtitle in .srt format.
auto_shorts/run.py Python lines 23–44
def generate_video(script_path):
    name = script_path.stem
    with open(script_path, encoding="utf-8") as f:
        text = f.read()

    bg = random.choice(list(BACKGROUNDS.glob("*.mp4")))
    temp_srt = OUT / f"{name}.srt"
    output = OUT / f"{name}.mp4"

    text_to_srt(text, temp_srt)

    cmd = [
        "ffmpeg", "-y",
        "-stream_loop", "-1",
        "-i", str(bg),
        "-vf", f"subtitles={temp_srt}:force_style='FontName=Arial,FontSize=42,Alignment=10'",
        "-t", "30",
        "-pix_fmt", "yuv420p",
        str(output)
    ]

    subprocess.run(cmd, shell=True)
Picks a random background, loops the video, burns in the subtitles and cuts at 30 seconds in a single ffmpeg call.
videos automaticos/sla.py Python lines 41–57
# 3. Gerar áudio do texto
speech = gTTS(text=texto_motivacional, lang="pt")
audio_path = "audio.mp3"
speech.save(audio_path)

# Converter para WAV para evitar erros no MoviePy
os.system("ffmpeg -i audio.mp3 audio_converted.wav")
narration = mp.AudioFileClip("audio_converted.wav")

# 4. Criar vídeo
background = mp.ImageClip(image_path).set_duration(10)  # Duração de 10 segundos
music = mp.AudioFileClip(MUSIC_PATH).set_duration(10).volumex(0.3)  # Música mais baixa
final_audio = mp.CompositeAudioClip([narration.set_duration(10), music.set_duration(10)])

video = background.set_audio(final_audio)
video_path = "video_motivacional.mp4"
video.write_videofile(video_path, fps=24)
Version 1: narration with gTTS and assembly in MoviePy, with the music at 30% volume under the voice.
impact

Results

Version 2 turns a folder of scripts into finished videos with one command, with no API costs.

Learnings

Separating the creative part (the script) from the repetitive part (subtitling and rendering) made the pipeline simpler, cheaper and more predictable than generating everything with AI.