Samuel Elh Blog

WordPress, PHP, Python and JavaScript tutorials and snippets

Bulk Download Wistia Account Videos

In this quick tip, we’ll learn about downloading your Wistia account videos in batch, and saving to your local or preferred drive.

We’ll be using Python for this as it is easy and quick to implement, but you can also use the scripting language of your choice.

As there is no official feature from Wistia that covers this, their API suggests that this is possible, as you can obtain a full list of your videos and their metadata following this route:

https://api.wistia.com/v1/medias.json?api_password=xyz123

The video metadata include the video name, direct download link in different formats (we’ll focus on MP4 for this tutorial), which you could gather and then iterate through the list to download everything.

It’s also worth mentioning that you should add pagination arguments to the API route, in order to obtain 100 videos per page which is the maximum value.

First we want to get the full list of videos.

To get the full list of videos (which we’ll refer to as items), you’ll want to iterate through all the available pages and gather the items.

Here’s the code necessary to do this:

import urllib2
import json
 
api_token = '<REST-TOKEN>'
 
def fetch_videos(page):
    try:
        return urllib2.urlopen('https://api.wistia.com/v1/medias.json?per_page=100&page=%d&api_password=%s' % (
            page, api_token
        )).read()
    except:
        pass
 
items = []
 
for i in xrange(1, 99):
    print( "Fetching videos at page %d.." % i )
    res = fetch_videos(i)
 
    if not res or not len( str(res) ):
        print( "Pagination has stopped at %d." % i )
        break;
 
    data = json.loads(res)
 
    for item in data:
        items.append({
            'name': item['name'],
            'url' : item['assets'][0]['url']
        })
 
print( 'Successfully fetched %d items.' % len(items) )
print( 'Saving to items.json file..' )
 
with open('items.json', 'w') as outfile:
    json.dump(items, outfile)

Please insert your API token (password) in the place of `<REST-TOKEN>`, which you can create from your Wistia account.

After saving the code into a given file, say `get_video_urls.py`, we’ll run it with Python to fetch the items:

python get_video_urls.py

And it should create a JSON file named items.json which contains the videos and their titles and direct download links. That’s a good start.

Downloading Wistia videos

Next up is downloading the videos. This can be faster or the opposite, it all depends on your internet connection speed, and also the size of the downloads. But you can download videos in batches and create multiple processes, each process handling a group of videos, say for instance 10 processes to download 1000 videos quickly (which may take 1-2 hours). You can use screen or tmux to have multiple terminal windows if you decided to work in batches.

Here’s the necessary code ( `download_videos.py` file ):

import json
import urllib
downloader = urllib.URLopener()
 
# start by getting the full list of videos
with open('items.json') as json_data:    
    items = json.load(json_data)
 
# where to save the videos (directory)
save_to_dir = './'
 
def batch(startAt, stopAt):
    index=0
 
    for item in items:
        index+=1
        if index < startAt or index > stopAt: continue
        print( '[%d] Downloading %s..' % (index, item['name']) )
        downloader.retrieve(item['url'], '%s%s.mp4' % (
            save_to_dir, item['name']
        ))
 
# download videos from 1 to 999
batch(1, 999)

You can pass the start number and finish number to the `batch` method if you want to create multiple processes for downloading these videos. After that, just run it with Python again:

python download_videos.py

The videos should start downloading, and the screen should also indicate the current download index, so if you run into any errors you can resume the process at the video index where it stopped.

Download tutorial source code.

Digital Ocean

Cheap Cloud SSD Hosting

Get a VPS now starting at $5/m, fast and perfect for WordPress and PHP applications

Sign Up with $10 Credit

3 Comments

  1. I’m trying to use your code but i get a syntax error on line 11 in the get_video_urls.py and in line 18 in the download_videos.py

    Why?

Leave a Reply

Your email address will not be published.

*

© 2018 Samuel Elh - Powered by WordPress, DigitalOcean & NameCheap

Theme by Anders NorenUp ↑

Subscribe to our mailing list

Sign up to receive updates about WordPress, free and premium plugins and themes in general and tips and tricks

* indicates required