After videos have been transcoded on api.video, it’s natural that you want to give your users the ability to download their videos, or just download the video for your own use, possibly for backup.
api.video allows you and your users to download videos directly from the player or programmatically through the API.
The video object can be created or updated with the mp4Support tag. The tag will make the video downloadable and display a button on the api.video player.
By default, the mp4Support tag is set to true, hence the video is downloadable. If you want to disable the download ability from the api.video player, you have to set the mp4Support tag to false
If you don’t want the user to be able to download the video, do not serve the mp4 asset.
You can also find more information on the /videos endpoints on the API reference page.
Disable download for a video object
How to create a video object with disabled download
Once the object is created, all you have to do is just upload the video to the object:
Disable download for an existing video object
It’s also possible to disable the download ability for the users, after the video was already uploaded and transcoded. For that, you just need to update the video object.
Creating a custom download button
Most players support download, however, if you would like to add a download button to a custom player that doesn’t have a download button, it’s also possible.
By leveraging the API, you can create a request to api.video, which will get the video id and then render a download button serving the mp4 asset.
In this example, you will learn how to create a custom download button with React and Node.js backend
After you have the React project ready, navigate to the project root folder:
$ cd my-react-project
Install the file-saver module dependency
$ npm install --save file-saver
We will need to add a new component to React, that will make a request and get the video mp4 asset url. For your convenience, we’ve created the component for you. All you have to do is add the component below.
import{saveAs}from'file-saver';// here you specify how you want to call the downloaded file
constvideoFileName="source.mp4";constserverHostname="localhost:5500"// on the click handle, get the mp4 asset of the video
consthandleClick=async(sourceVideoId)=>{try{// replace the server hostname with your
constresponse=awaitfetch(`http://${serverHostname}/download?videoId=${sourceVideoId}`,{method:'GET',});if(!response.ok){thrownewError(`Error! status: ${response.status}`);}constresult=awaitresponse.json();if(result.videoUrl){awaitfetchVideoUrl(result.videoUrl);}}catch(err){console.error(`error: ${err}`);}};// download the mp4 asset using file-saver library
constfetchVideoUrl=async(mp4Uri)=>{try{constresponse=awaitfetch(mp4Uri,{method:"GET",header:{"Content-Type":"video/mp4",}})if(!response.ok){thrownewError(`Error while fetching file`);}constblob=awaitresponse.blob()// here we specify the name of the
saveAs(blob,videoFileName);}catch(error){console.error(`Unable to download video. ${error}`);}}constDownloadVideo=({videoId})=>{return(<div><header><div><buttononClick={() =>handleClick(videoId)}>DownloadVideo</button>
</div></header>
</div>);}exportdefaultDownloadVideo;
As a best practice, you need to add the component in the Component folder (inside src).
Now let’s add the component to our App.js.
You will notice that you pass a videoId as a prop, for this example, just grab any video id from your dashboard or from the API.
In order to make sure that your request is not made from the frontend which might expose your API key, we need to create a server that will make the actual API request.
In the example, we are using Node.js with express server. Let’s create a Node.js server first
Now edit the index.js file that you’ve created, and copy this code.
Make sure that you copy your API key from the api.video dashboard and replace the port to the port you would like to run the server on.
constexpress=require('express');// cors can be commented out in prod
constcors=require('cors')constApiVideoClient=require('@api.video/nodejs-client')constapp=express()// change the port to whatever port you would like to run the server on
constport=5500app.use(cors())constapivideoClient=newApiVideoClient({apiKey:"you_api_key"});// get the video id from the request
constgetVideoIdFromReq=(req)=>{if(req&&req.query&&req.query.videoId){returnreq.query.videoId}else{thrownewError(`Unable to find video id in the request`)}}// get the video mp4 asset url
constgetVideoUrlAsset=async(videoId)=>{if(videoId){try{console.log(videoId)constvideoMp4Asset=awaitapivideoClient.videos.get(videoId);if(videoMp4Asset&&videoMp4Asset.assets&&videoMp4Asset.assets.mp4){returnvideoMp4Asset.assets.mp4;}else{thrownewError(`Mp4 asset not found`);}}catch(error){console.error(`Unable to retrieve video. ${error}`)}}}app.get('/download',async(req,res)=>{constvideoId=getVideoIdFromReq(req);constmp4VideoAssetUrl=awaitgetVideoUrlAsset(videoId);res.json({videoUrl:mp4VideoAssetUrl});})app.listen(port,()=>{console.log(`Example app listening on port ${port}`)})
The next step is to install the modules. Make sure that you are in the server directory
$ npm install express$ npm install @api.video/nodejs-client$ npm install cors
Run the server
$ node index.js
Starting up React
Depending on the port and hostname that you’ve set, replace the port in the React component (DownloadVideo) with the port that you’ve set for the server:
constserverHostname="localhost:5500"
Start the React frontend from the React project directory
$ npm start
The result that you should see is a button that will download the source.mp4 of the video once the button is clicked.