api.video
SearchCtrl K

Features

Developers

Progressive uploads with the api.video Python client

December 15, 2021 - Erikka Innes in Video upload, Python

Today we'll walk through how to use the Python client to do a progressive upload for a video. Progressive uploads are great for large videos. We recommend choosing this type of upload if your video is 200MB or greater.

Prerequisites

  • Sign up for an api.video account - https://dashboard.api.video/register
  • Set up a virtual environment for your project (optional)
  • Install the api.video Python client - pip install api.video
  • Get a video file that's 200MB or greater (you can use a smaller one to see how it works, but it should be at least around 50MB)

Code sample

Here's our code sample for today:

import os
import apivideo
from apivideo.apis import VideosApi
from apivideo.exceptions import ApiAuthException

api_key = "your API key here"

# Set up the authenticated client
client = apivideo.AuthenticatedApiClient(api_key)

# if you rather like to use the sandbox environment:
# client = apivideo.AuthenticatedApiClient(api_key, production=False)
client.connect()

videos_api = VideosApi(client)

video_create_payload = {
    "title": "Progressive Test",
    "description": "test",
    "public": False,
    "tags": ["nature"]
}
# Create the container for your video and print the response
response = videos_api.create(video_create_payload)
print("Video Container", response)

# Retrieve the video ID, you can upload once to a video ID
video_id = response["video_id"]

session = videos_api.create_upload_progressive_session(video_id)

CHUNK_SIZE = 6000000

# This is our chunk reader. This is what gets the next chunk of data ready to send.
def read_in_chunks(file_object, CHUNK_SIZE):
    while True:
        data = file_object.read(CHUNK_SIZE)
        if not data:
            break
        yield data

# Upload your file by breaking it into chunks and sending each piece
def upload(file):
    content_name = str(file)
    content_path = os.path.abspath(file)

    f = open(content_path, "rb")
    index = 0
    offset = 0
    part_num = 1
    headers = {}

    for chunk in read_in_chunks(f, CHUNK_SIZE):
        offset = index + len(chunk)
        index = offset

        with open('chunk.part.' + str(part_num), 'wb') as chunk_file:
            chunk_file.write(chunk)
            chunk_file.close()

        with open('chunk.part.' + str(part_num), 'rb') as chunk_file:
            try:
                if len(chunk) == CHUNK_SIZE:
                    session.uploadPart(chunk_file)
                elif len(chunk) < CHUNK_SIZE:
                    print(session.uploadLastPart(chunk_file))
                chunk_file.close()
            except Exception as e:
                print(e)

        os.remove('chunk.part.' + str(part_num))
        part_num += 1

upload('VIDEO_FILE.mp4')

In this sample we do the following:

  1. Set up the API client.
  2. Create a container for our video with a payload containing our video metadata.
  3. Retrieve the video ID from the new container we made.
  4. Create a session to handle our progressive video upload using the video ID we retrieved.
  5. Set the chunk size we want to use. This value will be used to create chunks of this size. It's set to a smaller number so you can demo with a smaller video. If you want to upload something bigger, you might want to use a bigger number for each chunk.
  6. Set up a method that handles breaking the video into chunks of data.
  7. Set up a progressive video uploader. Of note - you have to take each chunk of data and save it as a clearly numbered part. For example chunk_file.part.1, chunk_file.part.2. End the files with part.* so that the api.video server can figure out the chronological order for each chunk of video. Also of note - you must signal that you're uploading the last chunk of video with the session.uploadLastPart() method. Otherwise, the api.video servers cannot determine if you're still uploading or not, so your upload will fail. You also have to open your file after saving it, using 'rb' or read binary.

Conclusion

If you want to try this upload without the client, see the article about how to do a progressive upload with just Python.

Happy coding!

Erikka Innes

Developer Evangelist

Create your free account

Start building with video now