logo api.video

Upload a video: Discord

November 30, 2020 - Doug Sillars in Delegated UploadVideo Status,NodeJS

In this post, we'll walk through the steps to upload a video. We've extended the application to not only upload the video to api.video, but to also post it to a Discord channel!

<iframe src="https://embed.api.video/vod/vi60o6eEvTO6GU59BzLfqqyq#autoplay" width="100%" height="600" frameborder="0" scrolling="no" allowfullscreen="true"></iframe>

The sample code is on Github.

Connecting to Discord

Before we can upload the video and send the file to Discord, we need to prep our channel to receive the messages. To this end, I created the apivideo_upload bot. To add this bot to your channel, simply visit: https://discord.com/oauth2/authorize?client_id=780911671153000479&scope=bot to add it to your server (this requires "Manage Server" permissions.)

Uploading the video

NOTE: Since this post was written, we've published a library to simplify JavaScript upload of videos read the blog post to learn more.

As discussed in the last post, the browser uses the File Slice API to break the video into 1MB segments and uploads them to api.video, using byte range requests and a videoId to identify the segments. Upon completion, the video is reassembled and encoded. The final API response lists all the details of the video.

The form to upload the video now also asks for a video name, description and a channelId.

The channelId is the Id of the Discord channel that will receive the video. To obtain the channelId, you must have developer mode enabled (user settings: appearance: advanced.

screenshot of Discord settings

Once developer mode is enabled, you can right click the channel name to copty the ID number:

copying channel id

Once the video has been uploaded and transcoded, we send a POST request to NodeJS. server. The Node JS server updates the video name and description (and tags the video as a Discord upload) using the update video endpoint.

let result = client.videos.update(videoId, {	title: videoName, 
																							description: videoDesc,					
																					tags: [tag]

We're not quite ready to send the video to Discord yet - for the embed to play properly in the Discord application - the video must have at least one video quality transcoded, and be able to be played. So we use the video status, and ping it every 2 seconds until the video is playable.

Once the video is playable - we can send the link to Discord. We identify the channel using the channelID provided in the form, and simply send the video description and the url as a message.

function checkPlayable(videoId) {
			console.log("checking encoding status");
			let status = client.videos.getStatus(videoId);
			 // console.log(videoStats);
			  let playable = videoStats.encoding.playable;
			  let qualitylist = videoStats.encoding.qualities;
			  console.log("is video playable?", playable);
			  //only look for the mp4 if the video is playable
			  //when still encoding, sometimes the mp4 status does not appear immediately
				 console.log("video is playable");
				//send to discord
				//send 200 back to page
				var channel = discordClient.channels.cache.get(discordChannel);
				channel.send( videoDesc + playerUrl);

		  }).catch((error) => {
video uploaded to discord

Bam! or "bloop bloop"! The video is posted right into Discord.

Simply adding a few lines of code has allowed us to create a video uploader that will automatically post the video to Discord.

Try it out and let us know what you think on our community page.


Doug Sillars

Head of Developer Relations

Doug Twitter