Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

html5 a download attribute doesn't work when the url is a blob url

We're creating a chrome extension to download videos, currently I have this function :

function downloadvideo(video)
{
    const url = URL.createObjectURL(video.captureStream());
    const aelem = document.createElement('a');
    document.body.appendChild(aelem);
    aelem.setAttribute("href",url);
    aelem.setAttribute("download","video.mp4");
    aelem.click();
    //URL.revokeObjectURL(url);
}

Here video parameter is a html5 video element, I'm using caputreStream because some websites(notably youtube) uses blob url which are revoked apparently So I create a new Blob url from MediaStream.

The function above is executed as part of video's onloadeddata event.

The dialog is shown and the filename is correct but when I click "Save" chrome says "failed : could not find the file".

So how to make it actually work ?

By the way I tried to do ajax against url but chrome refuses with the message : "Cross-origin is only supported for schemes http,https,chrome,chrome-extension" :3 .

like image 994
niceman Avatar asked Apr 24 '17 09:04

niceman


People also ask

Why download attribute is not working in HTML?

The download attribute only works for same-originl URLs. So if the href is not the same origin as the site, it won't work. In other words, you can only download files that belongs to that website. This attribute follows the same rules outline in the same-origin policy.

How does download attribute work?

The download attribute specifies that the target (the file specified in the href attribute) will be downloaded when a user clicks on the hyperlink. The optional value of the download attribute will be the new name of the file after it is downloaded.

Is Blob a URL?

Blob URLs contain pseudo protocols that can create a temporary URL to audio and video files. This type of URL essentially acts as a fake source for the media on the website, so you can't download it directly. Instead, you have to use third-party conversion tools. First, however, you have to find the blob URL itself.


1 Answers

URL.createObjectURL()

Note: The use of a MediaStream object as an input to this method is in the process of being deprecated. Discussions are ongoing about whether or not it should be removed outright. As such, you should try to avoid using this method with MediaStreams, and should use HTMLMediaElement.srcObject() instead.

Ref : https://developer.mozilla.org/en-US/docs/Web/API/URL/createObjectURL#Browser_compatibility

https://developer.mozilla.org/en-US/docs/Web/API/HTMLMediaElement/srcObject#Browser_compatibility

Code :

function downloadvideo(video) {

var mediaStream = video.captureStream();
//adding a dom element and fetching it in HTMLMediaElement.

HTMLMediaElement.srcObject = mediaStream;
var url = HTMLMediaElement.currentSrc;
const aelem = document.createElement('a');
document.body.appendChild(aelem);
aelem.setAttribute("href",url);
aelem.setAttribute("download","video.mp4");
aelem.click();
//URL.revokeObjectURL(url);

}

See if this works for you.

like image 88
Akash Shah Avatar answered Oct 22 '22 01:10

Akash Shah