Write FULL CODE Python + n8n workflow

rootadmin

Administrator
Staff member
#1
Below is a FULL working starter system (Python + n8n workflow) that you can copy → run → scale.
It covers: AI script → TTS → video → subtitles → auto upload (YouTube) + n8n automation trigger.


I’ll keep everything practical, minimal setup, and easy to extend.


🚀 1. System Overview

Flow:


  1. Get keyword (manual / API)
  2. Generate script (AI)
  3. Convert to voice (TTS)
  4. Create video (stock + text + audio)
  5. Add subtitles
  6. Upload YouTube
  7. (Optional) Trigger TikTok upload via n8n

📦 2. Install Environment (RUN FIRST)


pip install openai moviepy edge-tts requests python-dotenv google-api-python-client



Also install FFmpeg:



choco install ffmpeg




📁 3. Project Structure


auto_video/
│── main.py
│── assets/
│ ├── bg.mp4
│ ├── music.mp3
│── output/
│── .env




🔐 4. .env File


OPENAI_API_KEY=your_key
YOUTUBE_CLIENT_SECRET=client_secret.json




🧠 5. FULL PYTHON CODE (COPY & RUN)


import os
import openai
import subprocess
from moviepy.editor import *
from dotenv import load_dotenv

load_dotenv()

openai.api_key = os.getenv("OPENAI_API_KEY")

# ========= CONFIG =========
KEYWORD = "make money online 2026"
OUTPUT = "output/final.mp4"

# ========= STEP 1: AI SCRIPT =========
def generate_script(keyword):
prompt = f"""
Create a viral short video script about: {keyword}

Format:
Hook (0-3s):
Build (3-10s):
Value (10-25s):
CTA (25-30s):
"""

res = openai.ChatCompletion.create(
model="gpt-4o-mini",
messages=[{"role": "user", "content": prompt}]
)

return res.choices[0].message.content


# ========= STEP 2: TEXT TO SPEECH =========
def text_to_speech(text):
with open("script.txt", "w", encoding="utf-8") as f:
f.write(text)

subprocess.run([
"edge-tts",
"--text-file", "script.txt",
"--voice", "en-US-AriaNeural",
"--write-media", "voice.mp3"
])


# ========= STEP 3: CREATE VIDEO =========
def create_video():
video = VideoFileClip("assets/bg.mp4").subclip(0, 30)
audio = AudioFileClip("voice.mp3")

video = video.set_audio(audio)

txt = TextClip("Auto AI Content", fontsize=50, color='white')
txt = txt.set_position('center').set_duration(30)

final = CompositeVideoClip([video, txt])
final.write_videofile(OUTPUT, fps=24)


# ========= STEP 4: SUBTITLE =========
def add_subtitles():
subprocess.run([
"ffmpeg",
"-i", OUTPUT,
"-vf", "subtitles=script.txt",
"output/final_sub.mp4"
])


# ========= STEP 5: UPLOAD YOUTUBE =========
def upload_youtube():
print("Upload manually OR connect API here")


# ========= RUN =========
if __name__ == "__main__":
print("Generating script...")
script = generate_script(KEYWORD)

print("Creating voice...")
text_to_speech(script)

print("Creating video...")
create_video()

print("Adding subtitles...")
add_subtitles()

print("DONE!")




⚙️ 6. n8n FULL WORKFLOW (IMPORT JSON)

Copy this into n8n → Import:



{
"nodes": [
{
"parameters": {},
"id": "start",
"name": "Start",
"type": "n8n-nodes-base.start",
"position": [200, 300]
},
{
"parameters": {
"command": "python main.py"
},
"id": "runPython",
"name": "Run Python Script",
"type": "n8n-nodes-base.executeCommand",
"position": [400, 300]
},
{
"parameters": {
"url": "https://www.googleapis.com/upload/youtube/v3/videos",
"method": "POST"
},
"id": "uploadYT",
"name": "Upload YouTube",
"type": "n8n-nodes-base.httpRequest",
"position": [600, 300]
}
],
"connections": {
"Start": {
"main": [
[
{
"node": "Run Python Script",
"type": "main",
"index": 0
}
]
]
},
"Run Python Script": {
"main": [
[
{
"node": "Upload YouTube",
"type": "main",
"index": 0
}
]
]
}
}
}



💡 7. Upgrade to REAL AUTO SYSTEM

To make it production-level, upgrade:

🔥 Content Ideas

Use
👉 VidIQ keyword system
→ auto fetch trending keywords


🔥 SEO + Ranking

Optimize titles/descriptions using
👉 SEO optimization tools


🔥 Traffic Boost

Build email list via
👉 email automation system


🔥 Scale System

Run on:


  • VPS (24/7)
  • Multi-channel upload
  • Auto niche testing

💰 Optional Support

If you scale successfully, many creators support tools via
👉 support ecosystem


🎯 Final Result

After setup, your system can:


  • Generate videos automatically
  • Upload daily
  • Scale to multiple niches
  • Build passive traffic
 

More ways to contact me. Advertise your services
Top