Hacking perfectly square AI videos with Veo 3.1 and…
    Neura MarketNeura Market/CoPilot
    ChatGPTChatGPTClaudeClaudeGeminiGeminiCursorCursorGrokGrokPerplexityPerplexityCoPilotCoPilot
    DeepSeekDeepSeekStable DiffusionStable DiffusionMidjourneyMidjourney
    View All Directories
    OverviewRulesPromptsMCPsAgentsGamesBlogVideosGuidesCoursesCommunityPluginsTrending
    CoPilotBlogHacking perfectly square AI videos with Veo 3.1 and NanoBanana 2
    Back to Blog
    Hacking perfectly square AI videos with Veo 3.1 and NanoBanana 2
    python

    Hacking perfectly square AI videos with Veo 3.1 and NanoBanana 2

    Paige Bailey May 12, 2026
    0 views

    If you’ve been playing around with AI video generation lately, you already know the struggle: the...

    If you’ve been playing around with AI video generation lately, you already know the struggle: the tech is insanely cool, but sometimes getting it to output exactly the format you want feels like trying to center a <div> in 2014.

    Recently, I needed to generate a perfectly looping, high-quality square (1:1) video with audio using Google's new video models. The problem? Native aspect ratio support can sometimes be finicky depending on the model tier, and cropping a generated 16:9 or 9:16 video often ruins the framing or hallucinates weird artifacts at the edges.

    So, I had to let it cook. I came up with a slightly hacky but reliable workaround using NanoBanana 2, Veo 3.1 Lite, and our old reliable friend, FFmpeg.

    Here is the ultimate pipeline to get flawless square AI videos:

    TL;DR

    1. Start with a square image concept.
    2. Ask NanoBanana 2 to convert it to a 9:16 aspect ratio by literally just padding the top and bottom with black bars.
    3. Feed that phone-format 9:16 image into Veo 3.1 Lite as your start and end frames to force a loop.
    4. Run a quick Python script using ffmpeg to slice off the black bars.

    Boom. Perfect square video. Perfect audio sync. And no weird edge hallucinations. Here’s how to automate this flow using Python. 🐍

    Image description


    Step 1: Generating the "phone format" 9:16 frames with NanoBanana 2

    First, we need to generate our 9:16 image with the black bars baked in. Using the new Gemini API SDK, we can prompt NanoBanana 2 to do the heavy lifting for us.

    from google import genai
    from google.genai import types
    
    # Initialize your client
    client = genai.Client(api_key="YOUR_API_KEY")
    
    def generate_padded_frame(prompt, output_filename):
        print("🎨 Generating padded 9:16 image with NanoBanana 2...")
        
        # We explicitly tell NanoBanana 2 to give us a 9:16 image 
        # where the subject is a square in the middle, padded by black bars.
        hacked_prompt = f"{prompt}. Keep the main subject perfectly square in the center, and pad the top and bottom with solid black bars to make the overall aspect ratio 9:16."
        
        result = client.models.generate_images(
            model='nanobanana-2', # Our trusty image model
            prompt=hacked_prompt,
            config=types.GenerateImagesConfig(
                number_of_images=1,
                aspect_ratio="9:16",
                output_mime_type="image/jpeg"
            )
        )
        
        # Save the output
        for generated_image in result.generated_images:
            image = generated_image.image
            image.save(output_filename)
            print(f"✅ Saved to {output_filename}")
    
    # Generate our start/end frame
    generate_padded_frame("A majestic pink flamingo standing in a serene pond", "flamingo_padded.jpg")
    

    Step 2: Generating the video with Veo 3.1 Lite

    Now that we have our 9:16 image with black bars (flamingo_padded.jpg), we pass it to Veo 3.1 Lite. By using the same image as the visual prompt, we ensure the video maintains those exact black bars throughout the generation process.

    (Note: In the Veo web UI, you can set this as the start and end frame for a perfect loop. Here is the API equivalent for generating the video from your image).

    import time
    
    def generate_video(image_path, video_prompt, output_filename):
        print("🎬 Uploading frame and prompting Veo 3.1 Lite...")
        
        # Upload the padded image to the Gemini API
        initial_frame = client.files.upload(file=image_path)
        
        # Wait for the file to be processed
        while initial_frame.state.name == "PROCESSING":
            print(".", end="", flush=True)
            time.sleep(2)
            initial_frame = client.files.get(name=initial_frame.name)
        
        # Call Veo 3.1 Lite
        # We ask it to animate the subject but keep the black bars untouched
        response = client.models.generate_content(
            model='veo-3.1-lite',
            contents=[
                initial_frame, 
                f"{video_prompt}. The flamingo moves slightly, but the black bars at the top and bottom must remain exactly the same."
            ]
        )
        
        # Save the generated video bytes
        with open(output_filename, "wb") as f:
            f.write(response.text.encode('utf-8')) # Handling depends on raw bytes returned
        print(f"\n✅ Video generated and saved as {output_filename}")
    
    generate_video("flamingo_padded.jpg", "Cinematic shot of a flamingo looking around", "raw_veo_output.mp4")
    

    Step 3: The ffmpeg post-processing

    Now we have a beautiful video of a flamingo, but it's a 9:16 file with annoying black bars at the top and bottom.

    We could crop this frame-by-frame using Python libraries like MoviePy, but honestly? ffmpeg via the subprocess module is infinitely faster, uses way less memory, and most importantly: it perfectly preserves the audio stream without degrading it through re-encoding.

    Since the video is 9:16, trimming it to iw:iw (input width : input width) creates a perfect 1:1 square. FFmpeg is smart enough to center the crop automatically, perfectly slicing off the top and bottom black bars.

    import subprocess
    
    def crop_to_square(input_video, output_video):
        print("✂️ Cropping out the black bars with FFmpeg...")
        
        command =[
            'ffmpeg',
            '-y',                 # Overwrite output if it exists
            '-i', input_video,    # Input file
            '-vf', 'crop=iw:iw',  # Video Filter: Crop to width x width (automatically centered!)
            '-c:a', 'copy',       # Copy the audio as-is (chef's kiss for performance)
            output_video
        ]
        
        try:
            subprocess.run(command, check=True, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
            print(f"🔥 Success! Perfectly square video saved to {output_video}")
        except subprocess.CalledProcessError as e:
            print(f"💀 FFmpeg failed: {e}")
    
    # Run the final crop
    crop_to_square("raw_veo_output.mp4", "final_square_flamingo.mp4")
    

    {% embed https://x.com/DynamicWebPaige/status/2054247282841985125 %}

    Why this workaround actually... works

    1. Framing control: When you force the AI to outpaint black bars first, you control the framing of the main subject. You aren't relying on the video model to guess what to keep in the center.
    2. Audio preservation: The '-c:a', 'copy' flag in FFmpeg ensures you don't lose any audio fidelity when manipulating the video file.
    3. Zero hallucinations: Because the video model is explicitly told to keep the black bars, it doesn't waste compute trying to generate weird background details at the extreme top and bottom edges.

    Sometimes the best engineering solutions are just stacking simple tools together in a trench coat. 🧥

    Have you guys found any other weird/genius hacks for wrangling AI video generation APIs? Drop them in the comments, I’d love to test them out!

    (P.S. Make sure you have ffmpeg already installed on your machine before running the Python script, or it will yell at you).

    Tags

    pythonaivideogenwebdev

    Comments

    More Blog

    View all
    Five Gemma-4 models, one accelerator: what porting E2B 31B to AWS Inferentia2 taught megemma

    Five Gemma-4 models, one accelerator: what porting E2B 31B to AWS Inferentia2 taught me

    I ported the whole Gemma-4 family — E2B, E4B, 12B, 31B, and the 26B-A4B MoE — to run on...

    X
    xbill
    Hey DEV, I'm Tobore. Let's actually connect.community

    Hey DEV, I'm Tobore. Let's actually connect.

    Hey DEV, I'm Tobore. Let's actually connect. I've been on here for a while now, mostly writing and...

    L
    Laurina Ayarah
    I burned through thousands of AI tokens. Then a friend did it for freeai

    I burned through thousands of AI tokens. Then a friend did it for free

    (yep, kinda clickbait, just for the funsies 😊) At the beginning of the year, I relaunched my...

    P
    Paulo Henrique
    Claude might be saturating your machineai

    Claude might be saturating your machine

    My laptop was sitting idle with the fan at full tilt. Nothing was running that I knew of. The culprit...

    S
    Sidhant Panda
    Automated GitHub Code Reviews Using Google Geminigithubactions

    Automated GitHub Code Reviews Using Google Gemini

    I Built a Thing! TL;DR — Google Gemini-based Pull Request reviews and Issue Triaging for...

    D
    Darren "Dazbo" Lester
    What is an "agentic harness," actually?ai

    What is an "agentic harness," actually?

    I've been hearing the word "harness" thrown around a lot lately. I assumed it just meant "the IDE" or...

    T
    Tilde A. Thurium

    Stay up to date

    Get the latest CoPilot prompts, rules, and resources delivered to your inbox weekly.

    Neura Market LogoNeura Market

    Discover the best AI prompts, plugins, and resources for CoPilot and more.

    Content Types

    • Rules
    • Prompts
    • MCPs
    • Agents
    • Guides

    Platforms

    • ChatGPT Directory
    • Claude Directory
    • Gemini Directory
    • Cursor Directory
    • Grok Directory
    • Perplexity Directory
    • DeepSeek Directory
    • CoPilot Directory
    • Stable Diffusion Directory
    • Midjourney Directory
    • All Directories

    Resources

    • Blog
    • Documentation
    • Help Center
    • Marketplace

    Legal

    • Privacy Policy
    • Terms of Service

    © 2026 Neura Market. All rights reserved.

    |

    Not affiliated with any AI platform vendors.

    Neura Market

    Custom AI Systems & Services

    Our team of experienced AI builders will help build custom AI systems, workflows, and solutions for your business.

    Request custom work

    Ready-made automations for this

    Workflows from the Neura Market marketplace related to this CoPilot resource

    • Generate AI Viral Videos with NanoBanana & VEO3, Shared on Socials via Blotaton8n · $24.99 · Related topic
    • End-to-End Blog Generation for WordPress with LLM Agents & Image - GP-5 Optimizedn8n · $24.99 · Related topic
    • Automated Blog Post Generation with GPT-4 and Publishing to Ghost CMSn8n · $4.99 · Related topic
    • Automate Image and Blog Generation via Telegram with Pollinations and Geminin8n · $24.99 · Related topic
    Browse all workflows