Skip to Content

Unlocking YouTube Shorts Automation: A Look Under the Bonnet of XRobotix's Python Generator for Kodesangers

April 20, 2025 by
XRobotix (Pty) Ltd, Jaco Bezuidenhout


The world of online content moves at a blistering pace, and nowhere is that more obvious than with short-form video. Platforms like YouTube Shorts demand a steady flow of engaging, vertical content. But what if you've got longer videos – like music tracks, helpful guides, or presentations – and want to re-purpose them effectively without spending ages manually editing?

Here at XRobotix, we're passionate about using technology, especially AI and automation, to solve real-world problems and unlock creative potential. This passion fuels projects like Kodesangers, our eclectic South African music act whose very existence blends human creativity with AI tools. To support Kodesangers and streamline our own content production line, we developed an internal tool: the ShortsGenerator.

Today, we're giving you a look behind the scenes, sharing this powerful Python script and showcasing how smart automation can transform content re-purposing.

The Challenge: The Short-Form Content Grind

Creating compelling YouTube Shorts from existing footage isn't just about snipping out a clip. It involves:

  1. Finding Engaging Bits: Identifying those perfect 15-60 second hooks, choruses, or highlights.
  2. Reformatting: Cropping and resizing standard 16:9 video to the vertical 9:16 aspect ratio.
  3. Adding Context: Overlaying text to grab attention, provide information, or add a call-to-action.
  4. Trying Things Out: Creating multiple variations to see what clicks with the audience.

Doing this manually for every piece of content is hugely time-consuming and repetitive. We knew there had to be a better way.

The Solution: ShortsGenerator - A Python & MoviePy Powerhouse

Our ShortsGenerator is a Python script built using the versatile MoviePy library. At its heart, it takes a source video and a detailed JSON configuration file to automatically generate multiple YouTube Shorts, complete with rather sophisticated text overlays.

Python

1# --- Core Class Definition --- 2import json 3import os 4import random 5import gc 6import math 7from moviepy.editor import VideoFileClip, TextClip, CompositeVideoClip 8from moviepy.video.fx.crop import crop 9 10class ShortsGenerator: 11 """ 12 Generates YouTube Shorts (9:16 aspect ratio) from a source video 13 based on advanced configurations provided in a JSON file, including 14 thematic tagging and varied text overlay options, supporting sequential text. 15 """ 16 17 def __init__(self, config_path): 18 self.config = self._load_config(config_path) 19 self._validate_config() 20 print("Configuration loaded and validated successfully.") 21 22 # ... (rest of the methods: _load_config, _validate_config, _get_segments, etc.) ... 23 24 def generate_shorts(self): 25 """Loads video, extracts segments, processes, and saves shorts with advanced text options.""" 26 # ... main generation logic ... 27 pass 28 29# --- Example Usage --- 30if __name__ == "__main__": 31 CONFIG_FILE = "config.json" 32 try: 33 generator = ShortsGenerator(CONFIG_FILE) 34 generator.generate_shorts() 35 except (FileNotFoundError, ValueError, Exception) as e: 36 print(f"Failed to run generator: {e}")

But the really clever bit is its flexibility and advanced features, all driven by that configuration file.

Diving Deeper: More Than Just Snipping

The ShortsGenerator isn't just a video slicer. Here's what makes it stand out:

  1. Configuration-Driven: Instead of hardcoding parameters, everything is controlled via a config.json file. This makes it incredibly easy to adapt the script for different videos and campaigns without changing the Python code itself.
  2. Flexible Segment Selection: While the provided code focuses on a manual strategy (perfect for targeting specific parts like choruses or verses), the structure allows for future expansion into interval or random segment generation for quick, exploratory cuts.
  3. Automatic 9:16 Formatting: It intelligently crops the video to the vertical Shorts aspect ratio, attempting to keep the centre of the frame as the focus.
  4. Sophisticated Text Overlays: This is where things get really interesting:
    • Static Text: Add a simple, unchanging message to all generated shorts.
    • Sequential Text: Define a list of strings in the config, and the script will display them one after another within a single Short (e.g., ["Want a bakkie?", "That LASTS?", "Toyota of Gaan Huis Toe!"]). This is brilliant for storytelling or building intrigue.
    • Thematic Text (The Real Game-Changer): This is where the script truly shines for projects like Kodesangers. In the manual_segments section of the config, you can add tags to describe the content of that segment (e.g., "chorus", "ai_story", "hook"). Then, in the themed_text_options, you define text variations specific to those tags!
    • Generation Modes (random_themed, permutations_themed): When using themes, you can either pick a random relevant text for each tagged segment or generate shorts for all relevant text options (permutations), creating a wide array of variations instantly. If no specific theme matches, it can fall back to general text options.
  5. Customisation: Control fonts, sizes, colours, stroke, text position, output codecs, bitrate, and more – all from the config file.

The ShortsGenerator in Action: Powering Kodesangers

Let's look at how we use this for Kodesangers, specifically for the "Toyota of Gaan Huis Toe" track.

Here’s a snippet of our config.json:

JSON

1{ 2 "input_video_path": "XXXXX.mp4", // Path to the full music video 3 "output_directory": "output_shorts_sequential", 4 "output_filename_prefix": "toyota_hook_short", 5 "short_duration_max_seconds": 59, 6 "generation_strategy": "manual", 7 8 "manual_segments": [ 9 // Target specific choruses using timecodes and tag them 10 { "start": 15.5, "end": 30.0, "filename_suffix": "chorus1", "tags": ["chorus", "hook"] }, 11 { "start": 55.2, "end": 68.5, "filename_suffix": "chorus2", "tags": ["chorus"] }, 12 // Target visual segments and tag them with narrative context 13 { "start": 31.0, "end": 46.0, "filename_suffix": "visuals_ai_1", "tags": ["ai_story", "visuals", "cost_story"] }, 14 { "start": 95.0, "end": 110.0, "filename_suffix": "visuals_ai_2", "tags": ["ai_story", "visuals"] } 15 ], 16 17 "add_text_overlay": true, 18 "text_generation_mode": "random_themed", // Pick random text based on tags 19 20 // General fallback text options 21 "text_options": [ 22 "Toyota: Die Een Wat Hou!", 23 ["Want a bakkie?", "That LASTS?", "Toyota of Gaan Huis Toe!"], // Example of sequential text 24 "#Toyota #Bakkie #Afrikaans", 25 ["This video was made with AI...", "...it cost HOW MUCH?!", "(You won't believe it!)"], 26 "Rate this AI video 1-10!" 27 ], 28 29 // ** Thematic Text Magic! ** 30 "themed_text_options": { 31 "chorus": [ // Text specifically for segments tagged "chorus" 32 "Toyota of Gaan Huis Toe!", 33 ["Die bakkie...", "...wat net aanhou werk.", " elke. liewe. keer."], // Sequential! 34 "Sing saam!" 35 ], 36 "hook": [ // Text for segments tagged "hook" 37 "Wait for the beat drop...", 38 ["New Afrikaans Music!", "Made with AI?", "Sound On!"], 39 "You NEED to hear this chorus!" 40 ], 41 "ai_story": [ // Text for segments about the AI creation process 42 "100% AI Generated Video!", 43 ["We used Google Veo AI...", "...thought it was free...", "...woke up to a R12k+ bill!"], 44 "The future is expensive?", 45 ["AI can make videos...", "...but watch your credits!", "#AI #GoogleVeo #Costly"] 46 ], 47 "cost_story": [ // Specific text about the cost aspect 48 "Guess the AI video cost?", 49 ["Mistake #1:", "Assuming AI is cheap.", "This cost R12,000+!"], 50 "Worth the R12k AI bill? 🤔" 51 ], 52 "visuals": [ // Text focusing on the AI-generated visuals 53 "Mind blown by these AI visuals?", 54 ["No cameras.", "No actors.", "Just AI prompts."], 55 "What do you see here?" 56 ] 57 }, 58 59 // --- Text styling and output settings --- 60 "text_font": "Impact", 61 "text_fontsize": 50, 62 "text_color": "white", // Note: JSON uses 'color' 63 "text_stroke_color": "black", // Note: JSON uses 'color' 64 "text_stroke_width": 3, 65 "text_position": ["center", 0.80], // Note: JSON uses 'center' 66 // ... other settings ... 67 "video_bitrate": "6000k", 68 "preset": "fast" 69}

With this setup, running the script does the following:

  1. It identifies the four distinct segments we defined.
  2. For the segment tagged ["chorus", "hook"], it randomly selects one text option from either the "chorus" list or the "hook" list in themed_text_options.
  3. For the segment tagged ["ai_story", "visuals", "cost_story"], it randomly picks one text option from the combined pool of texts listed under "ai_story", "visuals", or "cost_story".
  4. It automatically crops each segment to 9:16.
  5. It overlays the chosen text (handling sequential text correctly if selected).
  6. It exports multiple MP4 files, named systematically (e.g., toyota_hook_short_chorus1_textVar1.mp4, toyota_hook_short_visuals_ai_1_textVar1.mp4), ready to upload.

If we changed text_generation_mode to "permutations_themed", it would generate a separate Short for every applicable text option for each segment – instantly creating dozens of variations for A/B testing or diverse content scheduling!

Why This Matters for XRobotix (and You)

Developing tools like the ShortsGenerator isn't just an academic exercise; it's about real, tangible benefits:

  • Massive Time Savings: What took hours now takes minutes of configuration and script execution time.
  • Content Velocity: Enables us to push out more Shorts content for Kodesangers, increasing visibility.
  • Creative Experimentation: Easily test different text hooks, calls-to-action, and segment highlights. The thematic tagging lets us tailor messages precisely.
  • Consistency: Ensures all Shorts adhere to defined visual styles (fonts, colours, positioning).
  • Showcasing Practical AI: It demonstrates XRobotix's ability to build practical tools that integrate into real-world creative workflows, even bridging the gap between AI-generated music/visuals and platform-specific content demands.

The Road Ahead

While powerful, the ShortsGenerator is an evolving tool. Future enhancements could include:

  • Implementing the interval and random generation strategies more robustly.
  • Adding more sophisticated text timing options for sequential overlays.
  • Integrating basic AI analysis to suggest potentially engaging segments automatically.
  • Adding options for simple transitions or background music overlays.

Conclusion

The ShortsGenerator is a testament to the XRobotix philosophy: applying smart automation and AI principles to enhance creativity and efficiency. It allows us to amplify the reach of projects like Kodesangers by tackling the content demands of modern platforms head-on. It’s a practical example of how a bit of Python and a clear understanding of the workflow can build a powerful content creation engine.

We believe that sharing tools and processes like this sparks innovation. What repetitive tasks in your creative workflow could be automated?

Follow Kodesangers on YouTube to see the ShortsGenerator output in action! And if you're intrigued by building custom AI and automation solutions, get in touch with XRobotix.