I am trying to use the YouTube API in Python to upload multiple videos, but I keep encountering an error message stating that my YouTube quota has been exceeded. The first video uploads successfully, but subsequent uploads fail. I am seeking a solution to this issue so that I can upload multiple videos without any interruptions.
Here is my code
import os
from google.oauth2.credentials import Credentials
from googleapiclient.discovery import build
from google.auth.transport.requests import Request
from google_auth_oauthlib.flow import InstalledAppFlow
from googleapiclient.errors import HttpError
import datetime
from datetime import timedelta
from googleapiclient.http import MediaFileUpload
import time
import random
import pytz
# Set up OAuth2 credentials
creds = None
if os.path.exists('token2.json'):
creds = Credentials.from_authorized_user_file('api/token2.json')
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('api/youtube2.json', scopes=['https://www.googleapis.com/auth/youtube.force-ssl'])
creds = flow.run_local_server(port=0)
# Save the credentials for the next run
with open('token.json', 'w') as token_file:
token_file.write(creds.to_json())
with open('token.json', 'w') as token_file:
token_file.write(creds.to_json())
# Set up YouTube API client
youtube = build('youtube', 'v3', credentials=creds)
print('Youtube Api Logged In')
with open('title.txt', 'r') as f:
title = f.read().strip()
# Set your timezone
local_tz = pytz.timezone('Asia/Kolkata')
# Set the initial scheduled upload time to the current time plus 15 minutes
scheduled_time = datetime.datetime.now(local_tz) + timedelta(minutes=15)
print(f'Current time : {scheduled_time}')
videoFolder = 'videos'
titleNum = 1
for video_file in os.listdir(videoFolder):
if video_file.endswith('.mp4'):
# Create a new video object and set its attributes
video_path = os.path.join(videoFolder, video_file)
video = {
'snippet': {
'title': f'{title} - # {titleNum}',
'description': '',
'categoryId': '22',
'tags': ['tag1', 'tag2', 'tag3'],
'defaultLanguage': 'en'
},
'status': {
'privacyStatus': 'unlisted',
'selfDeclaredMadeForKids': False,
}
}
# Upload the video to YouTube
try:
response = youtube.videos().insert(
part='snippet,status',
body=video,
media_body=MediaFileUpload(video_path)
).execute()
video_id = response['id']
print(f'The video was uploaded with ID: {video_id}')
# Schedule the video to be published
publish_time = scheduled_time
update = {'id': video_id, 'status': {'publishAt': publish_time.strftime('%Y-%m-%dT%H:%M:%SZ'), 'privacyStatus': 'private'}}
youtube.videos().update(part='status', body=update).execute()
print(f'Video "{video_file}" scheduled for {scheduled_time.strftime("%Y-%m-%d %H:%M:%S")} IST')
except HttpError as error:
print(f'An HTTP error {error.resp.status} occurred: {error.content}')
# Increment the scheduled upload time by 15 minutes for the next video
scheduled_time += timedelta(minutes=15)
titleNum += 1
randInt = random.randint(4,10)
time.sleep(randInt)
Here is Image you asked Comment about my Current Quota -
Your help would mean a lot to me, and I would be deeply grateful for any support you could provide. Thank you in advance for your kind assistance.
If you check the quota cost calculator you will find that it costs 1600 quota points to upload a video.
10000k default quota / 1600 = 6,25
which means that you can upload a max of 6 videos per day. The quota resets at midnight west cost USA time.
If you need to upload more just request an extension.