Customize and Embed Vimeo Videos using Python Requests.

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)

 

    By Posted On
SENIOR DEVELOPER at MICROPYRAMID

Latest Comments
Related Articles
Working with python collections part 1 Rakesh babu Podishetty

Python Collections - named tuple is to access by the names specified and deque is to append and pop the elements from both sides of ...

Continue Reading...
scrape website using scrapy python Jagadeesh V

In this Blog post, we will look into setting up scrapy. Later we will scrape from imdb.

Continue Reading...
How to implement Case Insensitive CSV DictReader in python Chaitanya Kattineni

In general use cases we upload the CSV files to the system to store huge amount of data by uploading single file. For example in ...

Continue Reading...

Subscribe To our news letter

Subscribe to our news letter to receive latest blog posts into your inbox. Please fill your email address in the below form.
*We don't provide your email contact details to any third parties