Quantcast
Channel: Active questions tagged youtube-api - Stack Overflow
Viewing all articles
Browse latest Browse all 3831

How can I upload multiple videos with youtube api python

$
0
0

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 osfrom google.oauth2.credentials import Credentialsfrom googleapiclient.discovery import buildfrom google.auth.transport.requests import Requestfrom google_auth_oauthlib.flow import InstalledAppFlowfrom googleapiclient.errors import HttpErrorimport datetimefrom datetime import timedeltafrom googleapiclient.http import MediaFileUploadimport timeimport randomimport pytz# Set up OAuth2 credentialscreds = Noneif 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 clientyoutube = build('youtube', 'v3', credentials=creds)print('Youtube Api Logged In')with open('title.txt', 'r') as f:    title = f.read().strip()# Set your timezonelocal_tz = pytz.timezone('Asia/Kolkata')# Set the initial scheduled upload time to the current time plus 15 minutesscheduled_time = datetime.datetime.now(local_tz) + timedelta(minutes=15)print(f'Current time : {scheduled_time}')videoFolder = 'videos'titleNum = 1for 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)

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.


Viewing all articles
Browse latest Browse all 3831

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>