Innovate anywhere, anytime withruncode.io Your cloud-based dev studio.
Python

Customize and Embed Vimeo Videos using Python Requests

2022-07-19

Using python requests and vimeo endpoints it becomes very easy and simple to upload our videos and customize them.

Vimeo Access token:

Create an account at vimeo, vimeo has different levels of account, so based on your requirement signup for suitable account.

This article assumes pro account.

After you log in, create an app and generate token based on your requirement like editing, uploading, view etc..

Installation:

pip install requests

pip install PyVimeo

Uploading video:

import vimeo, requests

v = vimeo.VimeoClient(token='your_token')

v.upload(video.video.path)

Delete Video:

import requests

delete_url = api + '/videos/' + your_vimeo_video_id

headers = {"Authorization": 'bearer '+'your_vimeo_token'}

rs = requests.session()

rs.delete(video_uri=delete_url, headers=headers)

Replace Video:

import vimeo

api = 'https://api.vimeo.com'

v = vimeo.VimeoClient(token='your_vimeo_access_token')

replace_url = api + '/videos/' + your_vimeo_video_id

v.replace(video_uri=replace_url, filename='your_video_path')

Embedding video:

Get your desired video id and use the following iframe to display video.

<iframe src="https://player.vimeo.com/video/' + your_vimeo_video_id + '" width="500" height="282" frameborder="0" webkitallowfullscreen mozallowfullscreen allowfullscreen></iframe>

Customizing your uploaded video:

Using python requests library you can customize your vimeo video.

Following code will edit description of the video, its name and setting the privacy rules like who can embed your video, who can view your video.

v = vimeo.VimeoClient(token='your_vimeo_token')

api = 'https://api.vimeo.com'

v_upload = v.upload('your_video_file_path')

url = api + v_upload

print 'video uploaded to vimeo.com/%s' % (v_upload)

rs = requests.session()

headers = {"Authorization": 'bearer '+'your_vimeo_token'}

rs.patch(url, data={"name": 'your_video_title', 'description': 'description here', 'privacy.embed': 'whitelist', 'privacy.view': 'anybody'}, headers=headers)

In the above code the patch request sets the video to be viewed by anybody.

The video embed setting as whitelist, which means that the video should be embeded only to the whitelisted domains. The whitelist domains can be added by using put request, shown below.

put_url = "https://api.vimeo.com/videos/%s/privacy/domains/%s" % (<your_vimeo_video_id>, 'whitelist_domain_name_here')

v.put(put_url)

Custom image for the video:

Vimeo sets the image by default but we can customize it as follows.

api = 'https://api.vimeo.com'

headers = {"Authorization": 'bearer '+'your_vimeo_token'}

rs_picture = rs.post('https://api.vimeo.com/videos/'+your_vimeo_video_id+'/pictures', headers=headers)

rs_picture_link = rs_picture.json()['link']

rs_picture_uri = rs_picture.json()['uri']

filepath = 'your_picture_file_path'

with open(filepath) as fh:

    mydata = fh.read()

    response = requests.put(rs_picture_link,

               data=mydata,

               headers=headers,

               params={'file': filepath}
                )
rs_picture_active = rs.patch(api+rs_picture_uri, {'active': 'true'}, headers=headers)

What the above code does is, it gets the link to which our picture should be uploaded, as you can see rs.post does a post request using headers and gets the response, this response will have the link to which our picture should be uploaded.

Finall we send a patch request saying to activate the uploaded picture.

Vimeo Presets:

Vimeo preset helps to customize the vimeo player. First you have to create a preset by doing so a preset id will be created.

To list all the preset id's for a video send get request as below.

presets = rs.get('https://api.vimeo.com/me/presets', headers=headers)

Select your desired preset id and send a put request.

rs.put('https://api.vimeo.com/videos/'+your_video_vimeo_id+'/presets/<your_preset_id>', headers=headers)