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 .
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With