📤 Videos automatisch auf YouTube hochladen
MySQL-Queue → Google API → YouTube. Vollautomatisch, kein manueller Upload nötig.
📊 Projektübersicht
Dieses Python-Skript-Set automatisiert den kompletten YouTube-Upload-Workflow: Videos werden aus einer MySQL-Datenbank abgerufen (inkl. Titel, Beschreibung, Tags), via Google YouTube API v3 mit OAuth2-Authentifizierung hochgeladen, Thumbnails gesetzt und optional in Playlists organisiert. Ideal für Content-Pipelines und Bulk-Uploads.
⚙️ Funktionsweise
DB-Query
MySQL: SELECT aus Videos-Tabelle WHERE created=1 AND uploaded=0 ORDER BY RAND() LIMIT 1
OAuth2 Auth
Google OAuth2 mit token.json: Auto-Refresh bei Ablauf, Fallback auf Interactive-Login.
API Upload
videos().insert() mit snippet (Titel, Tags) + status (public) + MediaFileUpload resumable.
DB-Update
MySQL UPDATE: ytID setzen, uploaded=1. Playlist via playlists().insert() erstellen.
💻 Quellcode
Haupt-Routine: DB → OAuth → Upload
import pymysql
from googleapiclient.discovery import build
from google_auth_oauthlib.flow import InstalledAppFlow
from google.auth.transport.requests import Request
from google.oauth2.credentials import Credentials
from googleapiclient.http import MediaFileUpload
from playlist_ytvid import playlistYtvid
import os, datetime, random, schedule, time
def routine():
conn = pymysql.connect(
user='politik', password='DEIN_MYSQL_PASSWORT',
host='localhost', database='euchannel')
cursor = conn.cursor()
cursor.execute("""
SELECT mediaID, link, title, description, tags
FROM Videos
WHERE created = 1 AND uploaded = 0
ORDER BY RAND() LIMIT 1""")
row = cursor.fetchone()
mediaID, link, title, description, tags = row
OAuth2 mit Token-Caching & Auto-Refresh
creds = None
if os.path.exists(TOKEN_NAME):
creds = Credentials.from_authorized_user_file(
TOKEN_NAME, SCOPES)
if not creds or not creds.valid:
if creds and creds.expired and creds.refresh_token:
creds.refresh(Request())
else:
flow = InstalledAppFlow.from_client_secrets_file(
client_secrets_file, SCOPES)
creds = flow.run_console()
with open(TOKEN_NAME, 'w') as token:
token.write(creds.to_json())
googleAPI = build('youtube', 'v3', credentials=creds)
Resumable Upload mit Metadaten
def uploadYtvid(VIDEO_FILE_NAME, title, description,
tags, thumb, mediaID, googleAPI):
request_body = {
'snippet': {
'categoryId': 27, 'title': title,
'description': description, 'tags': tags,
'playlistId': playlistID
},
'status': {
'privacyStatus': 'public',
'selfDeclaredMadeForKids': False
},
'notifySubscribers': False,
'defaultLanguage': 'en',
'embeddable': True,
'license': 'youtube',
'publicStatsViewable': True
}
mediaFile = MediaFileUpload(
VIDEO_FILE_NAME, chunksize=-1, resumable=True)
response = googleAPI.videos().insert(
part='snippet,status',
body=request_body,
media_body=mediaFile).execute()
return response.get('id')
Eigene YouTube-Upload-Automation?
Von MySQL-gesteuerten Upload-Queues bis zur vollautomatischen Content-Pipeline – ich entwickle Ihre maßgeschneiderte YouTube-Automationslösung.