59 lines
1.9 KiB
Python
Executable File
59 lines
1.9 KiB
Python
Executable File
#!/usr/bin/env python3
|
|
import os
|
|
from google.auth.transport.requests import Request
|
|
from google.oauth2.credentials import Credentials
|
|
from google_auth_oauthlib.flow import InstalledAppFlow
|
|
from google.api_core.retry import Retry
|
|
from googleapiclient.discovery import build
|
|
from googleapiclient.http import MediaFileUpload
|
|
|
|
SCOPES = ["https://www.googleapis.com/auth/youtube.upload"]
|
|
|
|
def authenticate():
|
|
creds = None
|
|
if os.path.exists("token.json"):
|
|
creds = Credentials.from_authorized_user_file("token.json", 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_secret.json", SCOPES)
|
|
creds = flow.run_local_server(port=0)
|
|
|
|
with open("token.json", "w") as token:
|
|
token.write(creds.to_json())
|
|
|
|
return creds
|
|
|
|
def upload_video(file_path, title, description, category="22", privacy="private"):
|
|
creds = authenticate()
|
|
youtube = build("youtube", "v3", credentials=creds)
|
|
|
|
body = {
|
|
"snippet": {
|
|
"title": title,
|
|
"description": description,
|
|
"tags": ["test"],
|
|
"categoryId": category
|
|
},
|
|
"status": {
|
|
"privacyStatus": privacy
|
|
}
|
|
}
|
|
|
|
media = MediaFileUpload(file_path, mimetype="video/mp4", resumable=True)
|
|
request = youtube.videos().insert(part="snippet,status", body=body, media_body=media)
|
|
|
|
response = None
|
|
while response is None:
|
|
status, response = request.next_chunk()
|
|
if status:
|
|
print(f"Upload progress: {int(status.progress() * 100)}%")
|
|
|
|
print(f"Video uploaded! Video ID: {response['id']}")
|
|
|
|
if __name__ == "__main__":
|
|
upload_video("your_video.mp4", "My Video Title", "My description", privacy="public")
|
|
|