Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
1.2 kB
1
Indexable
Never
#pip install google-auth google-api-python-client
import os
import google.auth
from googleapiclient.discovery import build
from googleapiclient.http import MediaFileUpload
from google.oauth2 import service_account

video_title = "My YouTube Video"
video_description = "This is a demo video uploaded through the YouTube Data API"
video_tags = ["youtube", "api", "python"]
video_path = "PATH_TO_VIDEO_FILE"
credentials_path = "PATH_TO_JSON_KEY_FILE"

credentials = service_account.Credentials.from_service_account_file(credentials_path)
youtube = build('youtube', 'v3', credentials=credentials)

videos_insert_request = youtube.videos().insert(
    part='snippet,status',
    body={
        'snippet': {
            'title': video_title,
            'description': video_description,
            'tags': video_tags
        },
        'status': {
            'privacyStatus': 'private'
        }
    }
)

media = MediaFileUpload(video_path, chunksize=-1, resumable=True)
upload_response = videos_insert_request.execute(http=credentials.authorize(httplib2.Http()), media_body=media)

video_id = upload_response.get('id')

if video_id:
    print("Video uploaded successfully. Video ID:", video_id)
else:
    print("Failed to upload the video.")