api.video
SearchCtrl K

Features

Developers

Upload a video: Discord

November 30, 2020 - Doug Sillars in Delegated Upload, Video Status, NodeJS

In this post, we'll walk through the steps to upload a video (first explored in uploading large files with JavaScript. 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, and you can try it yourself at upload.a.video/toDiscord

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 broswer uses the File Slice API to break the video into 1MB segments and uploads them to api.video, using byterange 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 asks also 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 the 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 be have at least one video quality transcoded, and 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 desxcriptipon 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 upload.a.video/toDiscord and let us know what you think on out community page.

Doug Sillars

Head of Developer Relations

Create your free account

Start building with video now