Batch Upload

Batch video upload using a bash script

In this tutorial, we will set up a script to upload a large number of videos.. Even though you can upload your videos through the Cloudflare Dashboard, we recommend using the command line terminal for mass operations.

To make API requests you will need your Cloudflare API key, your email address and one of your domain’s zone ID.

You will also need to download a tus client. Tus is an open source protocol that Cloudflare uses to upload large files, such as your videos. This tutorial will use the tus python client, available through pip, pythons’s package manager. You can get help from the Python documentation.

pip install -U tus.py

This script assumes you are using bash terminal on Mac or Linux and have find and sed command available. It will find all files ending in .mp4 within a directory and upload them to Stream.

IFS=$'\n'; set -f
for f in $(find /{upload-directory} -name '*.mp4')
do
echo "now uploading $f"
tus-upload --header X-Auth-Key {api-key} --header X-Auth-Email {email} "$f"  https://api.cloudflare.com/client/v4/zones/{zone-id}/media --chunk-size 5242880 2>&1 | grep 'Created' | sed -n -e 's:^.*media\/::p'
done
unset IFS; set +f

Output will look like:

now uploading /Users/myuser/Desktop/example.mp4
1fb73a5a76ac5a06c8e1794a8c93b975
now uploading /Users/myuser/Desktop/movies/movie1.mp4
61724df7fba9eb8930441806c4f16875
now uploading /Users/myuser/Desktop/movies/movie2.mp4
05ba9361f8952b2b89b808084ca44d77
...

Using the video ID returned after each upload, you can view each video at https://watch.cloudflarestream.com/{VIDEO-ID}