Uploading Videos to YouTube Using Python API

mrmo7ox

March 22, 2025

# Uploading Videos to YouTube Using Python API

Uploading videos to YouTube is a common requirement for many content creators and developers. While doing it manually is straightforward, automating the process using Python can save a lot of time and effort, especially if you have a large number of videos to upload. In this blog post, we'll walk you through the process of uploading videos to YouTube using the YouTube Data API and Python. Additionally, we'll discuss the limitations before and after account verification.

## Prerequisites

1. **Google Account**: Ensure you have a Google account.
2. **Google Cloud Project**: Create a project in the Google Cloud Console.
3. **YouTube Data API v3**: Enable the YouTube Data API v3 for your project.
4. **OAuth 2.0 Credentials**: Set up OAuth 2.0 credentials for your project.
5. **Python Environment**: Ensure you have Python installed on your system.

## Setting Up the Environment

First, you'll need to install the required Python libraries. You can do this using `pip`:

```bash
pip install google-auth google-auth-oauthlib google-auth-httplib2 google-api-python-client
```

## Authenticating with the YouTube API

To interact with the YouTube API, you'll need to authenticate using OAuth 2.0. Here is a basic script to handle authentication:

```python
import os
import google_auth_oauthlib.flow
import googleapiclient.discovery
import googleapiclient.errors

scopes = ["https://www.googleapis.com/auth/youtube.upload"]

def authenticate():
    os.environ["OAUTHLIB_INSECURE_TRANSPORT"] = "1"
    api_service_name = "youtube"
    api_version = "v3"
    client_secrets_file = "client_secret.json"

    flow = google_auth_oauthlib.flow.InstalledAppFlow.from_client_secrets_file(
        client_secrets_file, scopes)
    credentials = flow.run_console()

    youtube = googleapiclient.discovery.build(
        api_service_name, api_version, credentials=credentials)

    return youtube
```

Make sure you have your `client_secret.json` file obtained from the Google Cloud Console.

## Uploading a Video

Once authenticated, you can upload a video using the following script:

```python
def upload_video(youtube, video_file, title, description, tags, category_id, privacy_status):
    request = youtube.videos().insert(
        part="snippet,status",
        body={
            "snippet": {
                "title": title,
                "description": description,
                "tags": tags,
                "categoryId": category_id
            },
            "status": {
                "privacyStatus": privacy_status
            }
        },
        media_body=video_file
    )
    response = request.execute()
    return response

if __name__ == "__main__":
    youtube = authenticate()
    video_file = "path_to_your_video_file.mp4"
    title = "Your Video Title"
    description = "Your Video Description"
    tags = ["tag1", "tag2"]
    category_id = "22"  # People & Blogs
    privacy_status = "public"

    response = upload_video(youtube, video_file, title, description, tags, category_id, privacy_status)
    print("Video uploaded successfully! Video ID:", response["id"])
```

## Limitations Before and After Verification

YouTube imposes certain limitations on video uploads, which vary based on whether your account is verified or not.

### Before Verification

- **Video Length**: Unverified accounts can only upload videos that are up to 15 minutes long.
- **File Size**: The maximum file size for uploads is 2 GB.
- **Daily Quota**: Unverified accounts have a lower quota for daily uploads.

### After Verification

- **Video Length**: Verified accounts can upload videos longer than 15 minutes.
- **File Size**: The maximum file size for uploads increases to 128 GB or 12 hours, whichever is less.
- **Daily Quota**: Verified accounts have a higher quota for daily uploads.

To verify your YouTube account, visit the [YouTube account verification page](https://www.youtube.com/verify) and follow the instructions.

## Conclusion

Uploading videos to YouTube using the Python API is a powerful way to automate your content management. However, keep in mind the limitations imposed by YouTube on unverified and verified accounts. By following the steps outlined in this blog, you can efficiently upload videos and manage your YouTube content programmatically.

Happy uploading!