apyhub-logo
Tutorials

Video Watermarking: A Step-by-Step Guide Using Node.js and ApyHub API

In this blog post, you will find a step-by-step guide on how to use Node.js and ApyHub API to implement video watermarking in your application.
blog thumbnail
SO
Sohail Pathan
Last updated on July 19, 2023
In this series so far, we have explored the benefits, techniques, and best practices for video watermarking, and how it can be useful for copyright protection, branding and promotion, proof of ownership, as well as content monitoring. We also looked at the types of watermarks including visible watermarks, timecode watermarks and audio watermarks.
Here we explored in detail some of the top tools and ways you can effectively watermark your videos, including using FFmpeg (or ffmpeg command), OpenCV or MoviePy. Cloud-based APIs like Cloudinary, Zencoder or ApyHub can also be particularly beneficial for content creators, online education providers, or websites with various types of video content.
In this article, we delve into how you can effectively use the ApyHub Video Watermarking API using Node JS.
To add an image watermark to a video using Node.js, follow the step-by-step tutorial below:

1. Install the required libraries:

To install all the required dependencies, run this command on the terminal of your project folder.
1
npm install axios fs
Once we run npm install in the terminal, the command triggers the installation of the specified dependencies listed in the project's package.json file. The following dependencies are commonly installed.

axios:

A popular HTTP client library for making HTTP requests in Node.js. It simplifies the process of sending HTTP requests and handling responses. In this case, axios is used to make a POST request to the Video Watermarking API, sending the video URL and watermark image URL.

fs (File System):

A built-in module in Node.js that provides functionalities for working with the file system. In this context, fs is used to create a write stream and save the watermarked video file. The createWriteStream function allows you to write the response data from the API request directly to a file on the local file system.

2. Create a new file:

The next step is to create a new file; for example, create a file videoWatermark.js and add the following code to import the required libraries we installed.:
1
const axios = require('axios');
2
const fs = require('fs');

3. Define a function to add the image watermark to the video:

This function encapsulates the logic for making the API request to add the image file watermark to the video, saving the watermarked video to a file using a write stream, and handling potential errors during the process. We will need apytoken which you can find from ApyHub Video Watermarking API documentation.
1
async function addWatermark(videoURL, watermarkImageURL, outputFileName) {
2
3
const apiURL= 'https://api.apyhub.com/generate/video/watermark/url/file?output=${outputFileName}.mp4'
4
5
const requestData = {
6
video_url: videoURL,
7
watermark_image_url: watermarkImageURL
8
};
9
10
try {
11
const response = await axios.post(apiURL, requestData, {
12
headers: {
13
'Content-Type': 'application/json',
14
'apy-token': '**** ADD-YOUR-SECRET-APY-TOKEN-HERE ****'
15
},
16
responseType: 'stream' // To get the response as a stream
17
});
18
19
const writeStream = fs.createWriteStream('${outputFileName}.mp4'); // Path where the watermarked video will be saved
20
response.data.pipe(writeStream);
21
22
return new Promise((resolve, reject) => {
23
writeStream.on('finish', resolve); // Resolve the promise when writing is finished
24
writeStream.on('error', reject); // Reject the promise if an error occurs
25
});
26
} catch (error) {
27
throw new Error('Failed to add watermark. ' + error.response.data.error.message);
28
}
29
}

4. Invoke the addWatermark function

After invoking the addWatermark function, pass the video URL and the watermark image URL as arguments. This function sends a request to the Video Watermarking API, which adds the watermark to the video and streams the watermarked video as a response. The response is then saved to a file using a write stream.
1
const videoURL = '***** PROVIDE THE URL OF VIDEO *****';
2
const watermarkImageURL = '***** PROVIDE THE URL OF IMAGE *****';
3
const outputFilename = '***** PROVIDE THE OUTPUT FILE NAME *****';
4
addWatermark(videoURL, watermarkImageURL, outputFilename)
5
.then(() => {
6
console.log('Watermark added successfully!');
7
})
8
.catch((error) => {
9
console.error('Error:', error.message);
10
});

5. Save file and execute script

Save the file and open your terminal, navigate to the project directory, and run the following command to execute the script:
1
node videoWatermark.js

6. Send a request to the API

The script will send a request to the API, download the watermarked video, and save it as provided file name [${outputFileName}.mp4] in your project directory. That's it! You have successfully added an image watermark to a video using Node.js.
You can use the ApyHub video watermarking tool to effectively watermark your video content, whether in the form of files or URLs. This utility can provide a variety of benefits, especially for businesses or individuals who want to protect their videos from unauthorized use and want to promote a brand or business.
ApyHub also has similar services and tools to help watermark pdfs, add footers to documents, and apply watermarks to images, which can help depending on your use case.
If you’re looking to edit video content however, you can convert video formats, or easily generate thumbnails from videos using ApyHub utilities.