Upload Videos To Kaltura Using Python


#1

I am new to Pyhton/Kaltura api, trying to upload videos on to their platform. I have a simple Flask application with the code below.

    from flask import Flask
    from KalturaClient import *
    from KalturaClient.Plugins.Core import *
    import os
    
    app = Flask(__name__)
    
    config = KalturaConfiguration(partner_id)
    config.serviceUrl = "https://www.kaltura.com/"
    client = KalturaClient(config)
    ks = client.session.start(
          "some_secret",
          "username@user.com",
          KalturaSessionType.ADMIN,
          'client-id')
    client.setKs(ks)
    
    uploadToken = KalturaUploadToken()
    
    uploadTokenId = "upload id"
    fileData = open('/path/to/file', 'r')
    resume = False
    finalChunk = True
    resumeAt = -1
    
    client.uploadToken.upload(uploadTokenId, fileData, resume, finalChunk, resumeAt)
    # print(result);
    
    entry = KalturaMediaEntry()
    entry.mediaType = KalturaMediaType.VIDEO
    entry.name = "going home 6"
    entry.description = "none"
    
    client.media.add(entry)
    # print(result);
    
    
    entryId = "entry_id"
    resource = KalturaUploadedFileTokenResource()
    resource.token = "sometoken"
    
    result = client.media.addContent(entryId, resource)
    print(result)
    
    if __name__ == '__main__':
        app.run(debug=True)

I am following Kaltura API Workflow in order to upload a simple video to my Kaltura dashboard. I can follow the work flow on Kaltura console but cannot replicate the process in Flask. When I run the code, I am getting the error below

File “app.py”, line 26, in
client.uploadToken.upload(uploadTokenId, fileData, resume, finalChunk, resumeAt)
File “/Users/dsingh/.virtualenvs/Kaltura_API/lib/python3.6/site-packages/KalturaClient/Plugins/Core.py”, line 61905, in upload
resultNode = self.client.doQueue()
File “/Users/dsingh/.virtualenvs/Kaltura_API/lib/python3.6/site-packages/KalturaClient/Client.py”, line 340, in doQueue
postResult = self.doHttpRequest(url, params, files)
File “/Users/dsingh/.virtualenvs/Kaltura_API/lib/python3.6/site-packages/KalturaClient/Client.py”, line 293, in doHttpRequest
url, params, files, self.requestHeaders, requestTimeout)
File “/Users/dsingh/.virtualenvs/Kaltura_API/lib/python3.6/site-packages/KalturaClient/Client.py”, line 275, in openRequestUrl
e, KalturaClientException.ERROR_CONNECTION_FAILED)
KalturaClient.Base.KalturaClientException: ‘utf-8’ codec can’t decode byte 0x85 in position 26: invalid start byte (-4)


#2

Hello @dsingh1,

First of all, note that I’ve masked your personal information [partner ID, secret, token ID, etc]. When posting on the forum, such information should never be provided.

In your original code, I see:

uploadTokenId = "upload id"

This is wrong as it should contain the ID returned from client.uploadToken.add(uploadToken), I also see that you’ve used a hard coded entryId when you should get the ID from the object returned from client.media.add(entry)

However, I believe the real problem is with this line:

fileData = open('/path/to/file', 'r')

This would work with Python 2.n but will fail with 3.n. Instead, you should open the file in binary mode, like so:

fileData = open('/path/to/file', 'rb')

Below is a full script for ingesting a video asset onto Kaltura using the Python client library. You can run it like this:

$ ./upload.py <partner id> <admin secret> </path/to/vid_file>

For illustration purposes, I’ve used pprint() to dump the objects returned from each request.

#!/usr/bin/python
from KalturaClient import *
from KalturaClient.Plugins.Core import *
from pprint import pprint
import sys,os


if len(sys.argv) < 3:
        print ("Usage: " + sys.argv[0] + " <partner id> <admin secret> </path/to/vid_file>")
        exit (1)


partnerId=sys.argv[1]
partnerAdminSecret=sys.argv[2]
vidFile=sys.argv[3]
config = KalturaConfiguration()
config.serviceUrl = "https://www.kaltura.com/"
client = KalturaClient(config)
ks = client.session.start(
      partnerAdminSecret,
      "",
      KalturaSessionType.ADMIN,
      partnerId)
client.setKs(ks)

uploadToken = KalturaUploadToken()

result = client.uploadToken.add(uploadToken);
pprint(vars(result));
uploadTokenId = result.id
fileData = open(vidFile, 'rb')
resume = False
finalChunk = True
resumeAt = -1

result = client.uploadToken.upload(uploadTokenId, fileData, resume, finalChunk, resumeAt);
pprint(vars(result));
entry = KalturaMediaEntry()
entry.mediaType = KalturaMediaType.VIDEO
entry.name = "Test"

result = client.media.add(entry);
pprint(vars(result));
entryId = result.id
print(entryId)
resource = KalturaUploadedFileTokenResource()
resource.token = uploadTokenId

result = client.media.addContent(entryId, resource);
pprint(vars(result));

#3

Hi Jess,

Thank you so much for replying. I managed to upload the video locally based on your suggestions. I am able to open/upload a file from my local machine, however when I am taking a base64 encoded string of a video, and convert it to something that Kaltura api can accept as a parameter. Below is my attempt at convering the string after my configuration is completed.

result = client.uploadToken.add(uploadToken)
uploadTokenId = result.id

binary = ‘converted string’

binary = base64.b64decode(binary)

fileData = io.BytesIO(binary)

resume = False
finalChunk = True
resumeAt = -1

result = client.uploadToken.upload(uploadTokenId, fileData, resume, finalChunk, resumeAt)
entry = KalturaMediaEntry()
entry.mediaType = KalturaMediaType.VIDEO
entry.name = “Video Player”

result = client.media.add(entry)
entryId = result.id
resource = KalturaUploadedFileTokenResource()
resource.token = uploadTokenId

result = client.media.addContent(entryId, resource)

My compiler is throwing this error
KalturaClient.Base.KalturaException: Missing parameter “fileData” (MISSING_MANDATORY_PARAMETER)