Tutorials · 3 min read

api.video and Discord

Upload a video: Discord

Extending the "upload a video" HTML page to send the video to Discord.

Doug Sillars

November 30, 2020

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!

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);
			status.then(function(videoStats){
			 // 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
			  if(playable){
				 console.log("video is playable");
				//send to discord
				//send 200 back to page
				var channel = discordClient.channels.cache.get(discordChannel);
				channel.send( videoDesc + playerUrl);
				res.sendStatus(200);

			  }else{
				  setTimeout(checkPlayable,2000,videoId);
			  }
		  }).catch((error) => {
				console.log(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.

Try out more than 80 features for free

Access all the features for as long as you need.
No commitment or credit card required

Video API, simplified

Fully customizable API to manage everything video. From encoding to delivery, in minutes.

Built for Speed

The fastest video encoding platform. Serve your users globally with 140+ points of presence. 

Let end-users upload videos

Finally, an API that allows your end-users to upload videos and start live streams in a few clicks.

Affordable

Volume discounts and usage-based pricing to ensure you don’t exceed your budget.