How can I get direct video url from YouTube video URL?
Solution 1
Finally, I was able to get youtube video file reference url from KeepVid website.
Because I don't think the website supports restful api, I fetched its html content and parsed to get download urls. I didn't write code here as it is very simple. I would be glad if it helped you.
Solution 2
You can obtain video urls of YouTube by this code:
// ES6 version
const videoUrls = ytplayer.config.args.adaptive_fmts
.split(',')
.map(item => item
.split('&')
.reduce((prev, curr) => (curr = curr.split('='),
Object.assign(prev, {[curr[0]]: decodeURIComponent(curr[1])})
), {})
)
.reduce((prev, curr) => Object.assign(prev, {
[curr.quality_label || curr.type]: curr
}), {});
console.log(videoUrls);
// ES5 version
var videoUrls = ytplayer.config.args.adaptive_fmts
.split(',')
.map(function (item) {
return item
.split('&')
.reduce(function (prev, curr) {
curr = curr.split('=');
return Object.assign(prev, {[curr[0]]: decodeURIComponent(curr[1])})
}, {});
})
.reduce(function (prev, curr) {
return Object.assign(prev, {
[curr.quality_label || curr.type]: curr
});
}, {});
console.log(videoUrls);
Comments
-
Oscar almost 2 years
I'm going to play YouTube and Vimeo videos using React-Native. For now I implemented React-Native-Video and it does not support to play YouTube or Vimeo urls while can play direct video urls. So I just want to get a direct reference to a file (or a stream?) from YouTube Video URL. e.g from
https://www.youtube.com/watch?v=PCwL3-hkKrg
Is there any good third party services or websites to solve this problem? No matter what types of code or ways you will use though I'm using React-Native to play YouTube videos currently.