How to Download mp4 video in JS/React?

9k views Asked by At

This might have been asked early, but I am unable to figure it out, please help me and thanks in advance.

Problem:

I have a link to mp4 video (ex: https://www.learningcontainer.com/wp-content/uploads/2020/05/sample-mp4-file.mp4)

I want to download this video from front end.

I have tried the following method:

 const videoHref ='https://www.learningcontainer.com/wp-content/uploads/2020/05/sample-mp4-file.mp4';
 const a = Object.assign(document.createElement('a'), {
 href: videoHref,
 style: 'display: none',
 download: 'video.mp4'
 });
 document.body.appendChild(a);
 a.click();
 a.remove();

But when I execute this code,

the download will start and fails immediately with error

Failed - No file

Please help me resolve this.

5

There are 5 answers

2
mmantach On
fetch('https://www.learningcontainer.com/wp-content/uploads/2020/05/sample-mp4-file.mp4', {
    method: 'GET',
    headers: {
      'Content-Type': 'application/mp4',
    },
  })
  .then((response) => response.blob())
  .then((blob) => {
    const url = window.URL.createObjectURL(
      new Blob([blob]),
    );
    const link = document.createElement('a');
    link.href = url;
    link.setAttribute(
      'download',
      `FileName.pdf`,
    );
    document.body.appendChild(link);
    link.click();
    link.parentNode.removeChild(link);
  });

let me know if it worked thanks

1
Anil On

I solved it using following code,

let xhr = new XMLHttpRequest();
xhr.open('GET', 'path/videoLink', true);
xhr.responseType = 'blob';
xhr.onload = function () {
let urlCreator = window.URL || window.webkitURL;
let videoUrl = urlCreator.createObjectURL(this.response);
let tag = document.createElement('a');
tag.href = videoUrl;
tag.target = '_blank';
tag.download = skillName.includes('.mp4') ? skillName : skillName + '.mp4';
document.body.appendChild(tag);
tag.click();
document.body.removeChild(tag);
};
xhr.onerror = (err) => {};
xhr.send();
1
Kosti On

Such function work for me, but there's a catch:

with that approach ur browser will first store the video in the RAM and when the video is too big it will crash. We're creating a blob here, because a tag download attribute needs the origin to be ur domain, when u test it on localhost and you try to download from another origin it would throw an error.

const downloadVideo = (urls: string) => {
  axios({
    url,
    method: 'GET',
    responseType: 'blob',
  }).then((response) => {
    const urlObject = window.URL.createObjectURL(new Blob([response.data]));
    const link = document.createElement('a');
    link.href = urlObject;
    link.setAttribute('download', 'recording.mp4');
    document.body.appendChild(link);
    link.click();
    document.body.removeChild(link);
  });
};

To download video without creating a blob it needs to be from ur origin or the server serving it needs to append Content-Disposition and Allow Origin headers, then u can just download it with a with target="_blank" property

0
Gabriel Arghire On

Efficient solution, with fetch

const handleDownloadVideo = async () => {
    try {
      const videoUrl = 'https://www.pexels.com/video/1093662/download/';
      const videoRequest = new Request(videoUrl);
      fetch(videoRequest)
        .then(() => {
          const link = document.createElement('a');
          link.href = videoUrl;
          link.setAttribute('download', 'waterfall.mp4');
          document.body.appendChild(link);
          link.click();
          document.body.removeChild(link);
        });
    } catch (error) {
      console.error(error);
    }
  };

Note: this answer is similar to one above, but with fetch.

1
Abhinav Sancheti On

Add this into your server side

import axios from 'axios';

const downloadVideo = async (req, res) => {
  try {
    const videoUrl = req.query.url; // The video URL passed as a query parameter

    const response = await axios({
      url: videoUrl,
      method: 'GET',
      responseType: 'stream',
    });

    // Set the response headers to indicate the video file as an attachment
    res.setHeader('Content-Disposition', `attachment; filename="video.mp4"`);

    // Pipe the video data directly to the response object
    response.data.pipe(res);
  } catch (error) {
    console.error('Error downloading the video:', error);
    res.status(500).json({ success: false, error: 'Failed to download the video.' });
  }
};

export default downloadVideo;

Add add this code into your client side application on the click of your button.

                  try {
                    fetch(`/api/download-video?url=${videoUrl}`)
                    .then(response => response.blob())
                      .then((blob) => {
                        const url = window.URL.createObjectURL(blob);
                        const a = document.createElement('a');
                        a.href = url;
                        a.download = 'video.mp4'; // Replace with the desired filename for the downloaded file
                        a.click();
                        window.URL.revokeObjectURL(url);
                      });
                  } catch (error) {
                    console.error(error);
                  }