Creating CCVT Video Effect

Eugene Musebe

Introduction and setup

In this fun simple tutorial, we're going to be using Cloudinary transformations and SvelteKit to apply a CCTV effect to any video.

For those who are not familiar, SvelteKit is a Svelte framework similar to Next.js. If you've already written some Svelte code before, it's fairly easy to get up and running.

You're going to need to have Node.js and NPM installed on your development environment if you want to follow along. It goes without saying that you need some working knowledge of Javascript. It's recommended that you be familiar with the basics of Svelte.

It's important to note that SvelteKit is still in the early development stages. Certain features may be missing or buggy. You can still find some workarounds. Some notable issues include file upload and loading environment variables during SSR. The latter is more of an issue with Vite than with SvelteKit. It has, however, been addressed with Vite.js 2.7.

To create a new SvelteKit project, you can run the following command in your terminal

1npm init svelte@next cctv-video-effect-with-cloudinary

cctv-video-effect-with-cloudinary is the name of our project. You can substitute this for any appropriate name. Just follow the prompts to complete the scaffolding. To keep things simple, choose Skeleton project template, No for typescript, Yes for ESLint, Yes for Prettier. You can, however, use whatever options you want.

Let's understand the process, before proceeding. Ideally, you would want to allow the user to select a video file from their device, upload that video to cloudinary and apply the transformations. Because of the file upload issue that I mentioned earlier, we're not going to be doing this. Instead, we'll use a video file that is static. There's a workaround to the file upload here in case you are interested.

Cloudinary credentials

We need API keys to communicate with the cloudinary API. Open cloudinary and create a free account if you do not have one yet. Proceed to log in then go to the console page.

Open the SvelteKit project we created in your favorite code editor. At the root of your project, create a file called .env.local. We're going to be defining our environment variables in this file. Please read this FAQ and this blog post before proceeding.

Paste the following code inside .env.local

1VITE_CLOUDINARY_CLOUD_NAME=YOUR_CLOUD_NAME
2VITE_CLOUDINARY_API_KEY=YOUR_API_KEY
3VITE_CLOUDINARY_API_SECRET=YOUR_API_SECRET

Make sure to replace YOUR_CLOUD_NAME YOUR_API_KEY and YOUR_API_SECRET with the Cloud name, API Key and API Secret values from the console page.

We also need to install the Cloudinary Node.js SDK. Run the following command in your terminal, at the root of your project.

1npm install cloudinary

Video for upload.

I mentioned that we're not going to be selecting a video via a form, so we need a static video that we can use. It doesn't matter which video you use, you can just download a random video from the internet and save it inside static/videos. Make sure to take note of the file name. To make it easier later, you can rename it to video.mp4. You can also get the full source code on my github with a sample video already downloaded.

Getting started.

The first thing we need is the code that we're going to use to communicate with the cloudinary SDK. Inside the src folder, create a new folder called lib. Create a new file named cloudinary.js inside src/lib and paste the following code inside.

1import { v2 as cloudinary } from 'cloudinary';
2
3cloudinary.config({
4 cloud_name: import.meta.env.VITE_CLOUDINARY_CLOUD_NAME,
5 api_key: import.meta.env.VITE_CLOUDINARY_API_KEY,
6 api_secret: import.meta.env.VITE_CLOUDINARY_API_SECRET
7});
8
9const CLOUDINARY_FOLDER_NAME = 'cctv-effect-videos/';
10
11/**
12 * Get cloudinary upload
13 *
14 * @param {string} id
15 * @returns {Promise}
16 */
17export const handleGetCloudinaryUpload = (id) => {
18 return cloudinary.api.resource(id, {
19 type: 'upload',
20 prefix: CLOUDINARY_FOLDER_NAME,
21 resource_type: 'video'
22 });
23};
24
25/**
26 * Get cloudinary uploads
27 * @returns {Promise}
28 */
29export const handleGetCloudinaryUploads = () => {
30 return cloudinary.api.resources({
31 type: 'upload',
32 prefix: CLOUDINARY_FOLDER_NAME,
33 resource_type: 'video'
34 });
35};
36
37/**
38 * Uploads a video to cloudinary and returns the upload result
39 *
40 * @param {{path: string; transformation?:TransformationOptions;publicId?: string; folder?: boolean; }} resource
41 */
42export const handleCloudinaryUpload = (resource) => {
43 return cloudinary.uploader.upload(resource.path, {
44 // Folder to store video in
45 folder: resource.folder ? CLOUDINARY_FOLDER_NAME : null,
46 // Public id of video.
47 public_id: resource.publicId,
48 // Type of resource
49 resource_type: 'auto',
50 // Transformation to apply to the video
51 transformation: resource.transformation
52 });
53};
54
55/**
56 * Deletes resources from cloudinary. Takes in an array of public ids
57 * @param {string[]} ids
58 */
59export const handleCloudinaryDelete = (ids) => {
60 return cloudinary.api.delete_resources(ids, {
61 resource_type: 'video'
62 });
63};

Let's go over that code. We import the v2 API from the SDK. You can leave it as v2, however, for readability, we rename it to cloudinary.

1// import { v2 } from 'cloudinary';
2
3import { v2 as cloudinary } from 'cloudinary';

We then proceed to initialize the SDK by calling the .config method and passing the cloud name, api key, and api secret.

1cloudinary.config({
2 cloud_name: import.meta.env.VITE_CLOUDINARY_CLOUD_NAME,
3 api_key: import.meta.env.VITE_CLOUDINARY_API_KEY,
4 api_secret: import.meta.env.VITE_CLOUDINARY_API_SECRET
5});

The way we reference the environment variables looks a bit weird. If you're coming from Node.js, you're probably used to seeing something like

1process.env.VARIABLE_NAME

Like I mentioned before, vite.js had some issue with handling environment variables server-side. Read this guide for more information on how Vite handles these variables.

Make sure not to use this syntax anywhere in your client-side code for sensitive variables.

We also define a folder where cloudinary is going to store all our uploaded videos.

The handleGetCloudinaryUpload function calls the api.resource method to get a single resource from cloudinary. Read about the API and options that you can pass here

handleGetCloudinaryUploads calls the api.resources method to get all resources from a folder on cloudinary. Read about the API and options that you can pass here

handleCloudinaryUpload takes in an object that contains the path to the file that we want to upload and an optional transformation array. It calls the uploader.upload method on the SDK to upload the file. Read the upload api reference.

handleCloudinaryDelete deletes resources from cloudinary by passing an array of public IDs to the api.delete_resources method. Read about it in the cloudianry admin docs.

That is it for the cloudinary bit.


Now let's create some endpoints that we can call from our front-end. A SvelteKit application can have both pages and endpoints. Any file inside the src/routes directory that ends with a .svelte extension is automatically a page. On the other hand, any file inside src/routes that ends with a .js or a .ts extension is an endpoint.

Create a folder called api under src/routes. This folder will hold all of our endpoints. Since SvelteKit uses a file-based routing system, it means that all our endpoints will begin with /api.

Create a folder called videos under src/routes/api. Inside src/routes/api/videos create a new file called index.js. This file will handle HTTP requests to the endpoint /api/videos. Paste the following code inside src/routes/api/videos/index.js

1import { handleCloudinaryUpload, handleGetCloudinaryUploads } from '$lib/cloudinary';
2
3export async function get() {
4 try {
5 const uploads = await handleGetCloudinaryUploads();
6
7 return {
8 status: 200,
9 body: {
10 result: uploads
11 }
12 };
13 } catch (error) {
14 console.error(error);
15 return {
16 status: error?.statusCode ?? 400,
17 body: {
18 error
19 }
20 };
21 }
22}
23
24export async function post() {
25 function generateLayer(text, gravity, color = '#ffffff') {
26 return [
27 {
28 color,
29 overlay: {
30 font_family: 'Courier',
31 font_size: 15,
32 font_weight: 'bold',
33 text
34 }
35 },
36 {
37 flags: 'layer_apply',
38 gravity,
39 x: '15',
40 y: '15'
41 }
42 ];
43 }
44
45 try {
46 const date = new Date();
47
48 // Path to the video.
49 const videoPath = 'static/videos/video.mp4';
50
51 // Upload the video to Cloudinary
52 const uploadResponse = await handleCloudinaryUpload({
53 path: videoPath,
54 folder: true,
55 transformation: [
56 // Crop the video
57 {
58 width: 500,
59 crop: 'scale'
60 },
61 // Add a border
62 {
63 border: '5px_solid_rgb:00ffffff'
64 },
65 // Add some visual noise
66 {
67 effect: 'noise:50'
68 },
69 // Reduce the saturation
70 {
71 effect: 'saturation:-100'
72 },
73 // Modify the contrast
74 {
75 effect: 'contrast:50'
76 },
77 ...generateLayer(
78 `${date.getDate()}-${date.getMonth() + 1}-${date.getFullYear()}`,
79 'north_west'
80 ),
81 ...generateLayer('REC', 'north_east', '#ff0000'),
82 ...generateLayer('Camera 1', 'south_east')
83 ]
84 });
85
86 return {
87 status: 200,
88 body: {
89 result: uploadResponse
90 }
91 };
92 } catch (error) {
93 return {
94 status: error?.statusCode ?? 400,
95 body: {
96 error
97 }
98 };
99 }
100}

When handling requests of a particular verb/type we need to export a function corresponding to the HTTP verb. For example, to handle GET requests we need to export a function called get, to handle POST requests we need to export a function called post and so on. The only exception is the DELETE verb where we use del instead since delete is a reserved keyword. You can get more information on this in the docs.

In our case, when we receive GET requests we want to get all uploads by calling the handleGetCloudinaryUploads function that we created earlier.

When we receive POST requests we want to upload the video that we saved inside static/videos. Inside our post function, we have a function called generateLayer. This function will generate the transformation objects we need to pass to cloudinary. The transformation objects are passed to the transformation array in the handleCloudinaryUpload function that we created earlier. This is then passed to the uploader.upload method on the cloudinary SDK. Read about the transformation api. Our CCTV video will have some text: the date, recording status, and camera number. These are the layers that we are generating using the generateLayer function. Read this guide on adding text overlays to videos with cloudinary transformations.

If you downloaded a video to static/videos and gave it a name other than video.mp4 remember to change the following line to match the name of your video.

1// Path to the video.
2const videoPath = 'static/videos/video.mp4';

The following piece of code is responsible for making the upload to cloudinary.

1// Upload the video to Cloudinary
2const uploadResponse = await handleCloudinaryUpload({
3 path: videoPath,
4 folder: true,
5 transformation: [
6 // Crop the video
7 {
8 width: 500,
9 crop: 'scale'
10 },
11 // Add a border
12 {
13 border: '5px_solid_rgb:00ffffff'
14 },
15 // Add some visual noise
16 {
17 effect: 'noise:50'
18 },
19 // Reduce the saturation
20 {
21 effect: 'saturation:-100'
22 },
23 // Modify the contrast
24 {
25 effect: 'contrast:50'
26 },
27 ...generateLayer(
28 `${date.getDate()}-${date.getMonth() + 1}-${date.getFullYear()}`,
29 'north_west'
30 ),
31 ...generateLayer('REC', 'north_east', '#ff0000'),
32 ...generateLayer('Camera 1', 'south_east')
33 ]
34});

Pay attention to the transformation array. The first thing we do is crop the video so that we have a consistent width for all videos. Read about this here. Next, we add a border to our video. Read about how to do that here. We then proceed to add some visual noise to the video. Read about how to do that here. The other thing is to modify the saturation and contrast so that we have a black and white effect. Read about that here and here. We also have the last three transformations that are being generated by the generateLayer function. The first displays the date at the top left, the second displays the text REC on the top right, and the third displays the text Camera 1 on the bottom right.

We then return a successful response or an error. Read about this from the SvelteKit docs.


Create a new file called [...id].js under src/routes/api/videos. This file will handle all HTTP requests made to the endpoint /api/videos/:id. Paste the following code inside src/routes/api/videos/[...id].js.

1import { handleCloudinaryDelete, handleGetCloudinaryUpload } from '$lib/cloudinary';
2
3export async function get({ params }) {
4 try {
5 const result = await handleGetCloudinaryUpload(params.id);
6
7 return {
8 status: 200,
9 body: {
10 result
11 }
12 };
13 } catch (error) {
14 return {
15 status: error?.statusCode ?? 400,
16 body: {
17 error
18 }
19 };
20 }
21}
22
23export async function del({ params }) {
24 try {
25 const result = await handleCloudinaryDelete([params.id]);
26
27 return {
28 status: 200,
29 body: {
30 result
31 }
32 };
33 } catch (error) {
34 return {
35 status: error?.statusCode ?? 400,
36 body: {
37 error
38 }
39 };
40 }
41}

This is very similar to src/routes/api/videos/index.js save for the file name syntax and that we're handling GET requests and DELETE requests.

get calls handleGetCloudinaryUpload to get a specific resource using its public ID.

del passes a public id to handleCloudinaryDelete and deletes the resource with that public ID.

Regarding the syntax, you need to understand rest parameters. We could have used the syntax for dynamic routes and named it as [id].js but this would have only matched /api/videos/:id. We want to match all routes that come after /api/videos/ for example /api/videos/:id, /api/videos/:id/someAction, /api/videos/:id/someAction/:anotherId. In other words, we want to use the rest parameters syntax when we expect a route to have multiple dynamic parameters.

That's it for the backend.


Remember that client-side pages and components end in the extension .svelte.

Open src/app.html and add the following style tag to the head.

1<style>
2 body {
3 font-family: sans-serif;
4 margin: 0;
5 padding: 0;
6 }
7
8 :root {
9 --color-primary: #ff00ff;
10 }
11
12 * {
13 box-sizing: border-box;
14 }
15
16 button {
17 padding: 0 20px;
18 height: 50px;
19 border: 1px solid #ccc;
20 background-color: #ffffff;
21 font-size: 1.2rem;
22 font-weight: bold;
23 cursor: pointer;
24 }
25
26 button:disabled {
27 background-color: #cfcfcf;
28 }
29
30 button:hover:not([disabled]) {
31 color: #ffffff;
32 background-color: var(--color-primary);
33 }
34</style>

Create a file called __layout.svelte inside src/routes/ folder. Paste the following code inside src/routes/__layout.svelte

1<nav>
2 <ul>
3 <li><a href="/">Home</a></li>
4 <li><a href="/videos">Videos</a></li>
5 </ul>
6</nav>
7
8<main>
9 <slot />
10</main>
11
12<style>
13 nav {
14 background-color: #000000;
15 color: #fff;
16 display: flex;
17 height: 100px;
18 }
19
20 nav ul {
21 display: flex;
22 flex: 1;
23 justify-content: center;
24 align-items: center;
25 list-style: none;
26 gap: 8px;
27 margin: 0;
28 padding: 0;
29 }
30
31 nav ul li a {
32 padding: 10px 20px;
33 color: #000000;
34 display: block;
35 background-color: #ffffff;
36 text-decoration: none;
37 font-weight: bold;
38 }
39
40 nav ul li a:hover {
41 color: #ffffff;
42 background-color: var(--color-primary);
43 }
44</style>

This is what's called a layout component. This component will be applied to every page. In case you're not familiar with svelte slots and component composition, check out this tutorial from the svelte website. Similarly, we also need an error page layout to show whenever there's an error. Create a file called __error.svelte inside src/routes and paste the following code inside.

1<script>
2 function tryAgain() {
3 window.location.reload();
4 }
5</script>
6
7<div class="wrapper">
8 <b>Something went wrong</b>
9 <br />
10 <button on:click|preventDefault={tryAgain}>Try Again</button>
11</div>
12
13<style>
14 .wrapper {
15 display: flex;
16 flex-direction: column;
17 align-items: center;
18 justify-content: center;
19 height: calc(100vh - 100px);
20 }
21</style>

Paste the following code inside src/routes/index.svelte

1<script>
2 import { goto } from '$app/navigation';
3
4 export let isLoading;
5
6 async function generateVideo() {
7 try {
8 isLoading = true;
9
10 const response = await fetch('/api/videos', {
11 method: 'POST'
12 });
13
14 const data = await response.json();
15
16 if (!response.ok) {
17 throw data;
18 }
19
20 goto('/videos/', { replaceState: false });
21 } catch (error) {
22 console.error(error);
23 } finally {
24 isLoading = false;
25 }
26 }
27</script>
28
29<div class="wrapper">
30 {#if isLoading}
31 <div class="loading">
32 <i>Loading. Please be patient.</i>
33 <hr />
34 </div>
35 {/if}
36 <h1>CCTV Video effect with cloudinary</h1>
37 <p>Apply CCTV effect to any video using cloudinary transformations</p>
38
39 <p>You can change the video by editing the video, video.mp4 inside /static/videos/</p>
40
41 <div class="actions">
42 <button on:click|preventDefault={generateVideo} disabled={isLoading}>Convert video</button>
43 </div>
44
45 <br />
46 <p>or</p>
47 <br />
48 <a href="/videos">View generated videos</a>
49</div>
50
51<style>
52 div.loading {
53 color: var(--color-primary);
54 }
55 div.wrapper {
56 min-height: 100vh;
57 width: 100%;
58 display: flex;
59 flex-flow: column;
60 justify-content: center;
61 align-items: center;
62 background-color: #ffffff;
63 }
64</style>

The generateVideo function is called when the user clicks on the Convert Video button. The function makes a POST request to the /api/videos endpoint that we created. The endpoint will make the upload to cloudinary and apply the transformations we need to achieve the CCTV effect.

I am assuming that you're familiar with the syntax for svelte components. For this reason, I won't go into too much detail.


Create a folder called videos under src/routes/videos. Please note that this is a different videos folder from the one inside the api folder. Create a file called index.svelte inside src/routes/videos and paste the following code inside.

1<script>
2 import { onMount } from 'svelte';
3
4 let isLoading = false;
5 let videos = [];
6
7 onMount(async () => {
8 try {
9 isLoading = true;
10
11 const response = await fetch('/api/videos', {
12 method: 'GET'
13 });
14
15 const data = await response.json();
16
17 if (!response.ok) {
18 throw data;
19 }
20
21 videos = data.result.resources;
22 } catch (error) {
23 console.error(error);
24 } finally {
25 isLoading = false;
26 }
27 });
28</script>
29
30{#if videos.length > 0}
31 <div class="wrapper">
32 <div class="videos">
33 {#each videos as video (video.public_id)}
34 <div class="video">
35 <div class="thumbnail">
36 <img src={video.secure_url.replace('.mp4', '.jpg')} alt={video.secure_url} />
37 </div>
38
39 <div class="actions">
40 <a href={`/videos/${video.public_id}`}>Open Video</a>
41 </div>
42 </div>
43 {/each}
44 </div>
45 </div>
46{:else}
47 <div class="no-videos">
48 <b>No videos yet</b>
49 <br />
50 <a href="/">Generate video</a>
51 </div>
52{/if}
53
54{#if isLoading && videos.length === 0}
55 <div class="loading">
56 <b>Loading...</b>
57 </div>
58{/if}
59
60<style>
61 div.wrapper div.videos {
62 display: flex;
63 flex-flow: row wrap;
64 gap: 20px;
65 padding: 20px;
66 }
67
68 div.wrapper div.videos div.video {
69 flex: 0 1 400px;
70 border: #ccc 1px solid;
71 border-radius: 5px;
72 }
73
74 div.wrapper div.videos div.video div.thumbnail {
75 width: 100%;
76 }
77
78 div.wrapper div.videos div.video div.thumbnail img {
79 width: 100%;
80 }
81
82 div.wrapper div.videos div.video div.actions {
83 padding: 10px;
84 }
85
86 div.loading,
87 div.no-videos {
88 height: calc(100vh - 100px);
89 display: flex;
90 flex-flow: column;
91 align-items: center;
92 justify-content: center;
93 }
94</style>

The onMount function is run when a svelte component is mounted to the DOM. Read about it here. The function makes a GET request to our /api/videos endpoint and fetches all uploaded videos. It then updates the video's array/state with the result. On this page, we're not actually showing the videos because we don't want to load all those videos on one page. Instead, we're using a clever cloudinary trick to generate thumbnails for the videos. By changing the extension of the video from .mp4 to .jpg, cloudinary generates a thumbnail for us.


Let's now create a page to show individual videos. Create a new file called [...id].svelte under src/routes/videos/. This syntax, just like the one for src/routes/api/videos/[...id].js, is for multiple dynamic parameters, only this time we're doing it on the client-side. Paste the following inside src/routes/videos/[...id].svelte

1<script context="module">
2 export async function load({ page, fetch }) {
3 try {
4 const response = await fetch(`/api/videos/${page.params.id}`, {
5 method: 'GET'
6 });
7
8 const data = await response.json();
9
10 if (!response.ok) {
11 throw data;
12 }
13
14 return {
15 props: {
16 video: data.result
17 }
18 };
19 } catch (error) {
20 return {
21 status: error?.statusCode ?? 400,
22 error: error?.message ?? 'Something went wrong'
23 };
24 }
25 }
26</script>
27
28<script>
29 import { goto } from '$app/navigation';
30
31 let isLoading = false;
32 export let video;
33
34 async function deleteVideo() {
35 try {
36 isLoading = true;
37 const response = await fetch(`/api/videos/${video.public_id}`, {
38 method: 'DELETE'
39 });
40
41 const data = await response.json();
42
43 if (!response.ok) {
44 throw data;
45 }
46
47 goto('/videos', { replaceState: true });
48 } catch (error) {
49 console.error(error);
50 } finally {
51 isLoading = false;
52 }
53 }
54</script>
55
56<div class="wrapper">
57 <div class="video">
58 <video src={video.secure_url} controls>
59 <track kind="captions" />
60 </video>
61 <div class="actions">
62 <button on:click|preventDefault={deleteVideo} disabled={isLoading}>Delete</button>
63 </div>
64 </div>
65</div>
66
67<style>
68 div.wrapper {
69 min-width: 100vh;
70 display: flex;
71 justify-content: center;
72 align-items: center;
73 }
74
75 div.wrapper div.video {
76 width: 80%;
77 }
78
79 div.wrapper div.video video {
80 width: 100%;
81 object-fit: fill;
82 }
83</style>

Let's talk about the first script tag. Note that this script tag is marked with context="module", this is because it's supposed to run before the component is rendered. If you've used Next.js before, you probably know about getStaticProps or getServerSideProps. The load function that is used inside this script tag is very similar to Next.js getStaticProps or getServerSideProps. One difference is that in SvelteKit, the method runs on both the server and the client. Have a read of these docs to avoid some common pitfalls. Since the load function also runs on the client, avoid accessing sensitive environment variables here.

The load function, in this case, makes a GET request to the /api/videos/:id endpoint. This returns the video with the specified ID. For the component, we have also have a deleteVideo function which makes a DELETE request to /api/videos/:id and deletes the video with the specified ID from cloudinary. For the component body, we just have a video element that shows the video to the user.


Good news! šŸ˜ƒ You made it to the end šŸ„³. You can now run your application and try it out.

1npm run dev -- --open

Remember that this is a simple implementation for demonstration purposes. You can make a few optimizations for a production environment. Also, keep in mind that SvelteKit is still early in development. Issues such as native file upload are being sorted out and might be ready with the first stable version.

Codesandbox

The final project can be viewed on Codesandbox.

Thank you for your time. You can find the full source code on my Github.

Eugene Musebe

Software Developer

Iā€™m a full-stack software developer, content creator, and tech community builder based in Nairobi, Kenya. I am addicted to learning new technologies and loves working with like-minded people.