I've got a script that goes through the page and returns all selected elements - in this case, <embed...>
For example, when a YouTube video is found in a blog, it returns an iframe. I can take the iframe src, pass it through a function, and regex the video ID. However, when the YouTube video in within a YouTube page, its embedded differently and the video ID is found within the "flashvars" attribute.
Here's an example video embed : http://pastie.org/7155840
What would you guys recommend the best way of confirming the src as YouTube, and getting the video ID qlo5XHZ8-zI from it?
Here's a solution that doesn't use a regex.
First confirm it as youtube:
if (e.src.substr(0, 19) != "http://s.ytimg.com/") return false
Then extract the video id:
// get the flashvars
var flashvars = e.getAttribute("flashvars")
// convert flashvars string to key-value pairs
flashvars = flashvars.split("&").map(function(entry) {
return entry.split("=", 2)
})
// look up the value of the "video_id" key
var video_id = flashvars.filter(function(entry) {
return entry[0] == "video_id"
})[0]
video_id should now be the video's ID.
Related
I'm trying to implement amazon Polly in an application. It is an MVC application.
I
was able to retrieve the audio from the text and it works fine. And am trying to highlight the respective text in the webpage while playing that audio. like the rear speaker does.
My aim is to implement it without a third-party application. I went through the documentation but I can't find anything useful. I didn't find any options that automatically highlight the text in amazon Polly itself.
Can we do anything with Speech marks for this? is there any way to do this?
Thanks in Advance :)
Edit
I have the speech mark JSON result. Now am stuck on how to sync this result with an HTML audio tag.
{"time":6,"type":"word","start":0,"end":2,"value":"Hi"}
{"time":587,"type":"word","start":4,"end":6,"value":"my"}
{"time":754,"type":"word","start":7,"end":11,"value":"name"}
{"time":1147,"type":"word","start":12,"end":14,"value":"is"}
{"time":1305,"type":"word","start":15,"end":19,"value":"John"}
I have the speech mark JSON result. Now am stuck on how to sync this result with an HTML audio tag.
You can use the timeupdate event on and audio element and sync the audio.currentTime property with the best matching speech mark from Polly.
This assumes that audio references the JavaScript variable for the HTML audio element who has it's src set to the audio file returned from Polly that corresponds to the text's speech marks:
function getSpeechMarkAtTime(speechMarks, time) {
const length = speechMarks.length
let match = speechMarks[0]
let found = false
let i = 1
while (i < length && !found) {
if (speechMarks[i].time <= time) {
match = speechMarks[i]
} else {
found = true
}
i++
}
return match
}
function onTimeUpdate(speechMark) {
/**
* Update your HTML and CSS based on the attributes
* of the speech mark at the audio's current time.
*/
}
audio.addEventListener('timeupdate', () => {
// Polly Speech Marks use milliseconds
const currentTime = audio.currentTime * 1000
const speechMark = getSpeechMarkAtTime(speechMarksJSONResult, currentTime)
// Some custom callback
onTimeUpdate(speechMark)
})
I want to reproduce all youtube files embedded on a site using SoundManager2 audio player. How can I get access to Youtube file URL from video ID?
Note: Soundcloud offers the possibility to get the direct streaming URL from the song URL. Here is a working example with Soundmanager2 and Angular. Now I want to do the same for Youtube.
Your title said using API. It's not possible to get access to YouTube file URL from video ID by that. By mean, with file URL you can also download it, distribute content etc while it's against YouTube Terms. So there is no such API for that.
Since you're tagged with Javascript, there is possibility to get direct access to the file URL. This probably is not as you wanted exactly but it worked flawlessly from the developer tools console available in any browser. For details Here you go
const videoUrls = ytplayer.config.args.url_encoded_fmt_stream_map
.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 + ':' + curr.type.split(';')[0]]: curr
}), {});
console.log(videoUrls);
One slightly hacky way - You could use Youtube's OEmbed API, however you need to provide a URL, so you'd need to add the ID onto the end.
https://www.youtube.com/oembed?url=http%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3DiwGFalTRHDA
This will consistently return an embed iframe, which you could regex the URL out of.
var regex = /<iframe.*?src='(.*?)'/;
var src = regex.exec(str)[1];
I have an HLS stream that streams a live feed with embedded ID3 PRIV Tag to mark a certain points in stream. Is it possible to get notified or somehow retrieved the data from these tags in Safari, ideally via Javascript?
Sure, this is possible using JavaScript functions, like:
video.textTracks.addEventListener('addtrack', function(addTrackEvent) {
var track = addTrackEvent.track;
track.mode = 'hidden';
track.addEventListener('cuechange', function(cueChangeEvent) {
// some processing here...
});
});
This is basically based on an answer from this question about ID3 tags on iOS.
hello I found lots of threads here which explains how to parse youtube id from url but nothing I found which can extract youtube video id from string.
I need a function which can return me youtube video id from url
For Example I have string
> Hello this is my string and this is my youtube video : http://youtube.com/w=abcdef and this is my blog http://stackoverflow.com etc etc.
and it should return
> Hello this is my string and this is my youtube video : abcdef and this is my blog http://stackoverflow.com etc etc.
Trying this function but not working properly
function linkifyYouTubeURLs(text) {
var re = /https?:\/\/(?:[0-9A-Z-]+\.)?(?:youtu\.be\/|youtube(?:-nocookie)?\.com\S*[^\w\s-])([\w-]{11})(?=[^\w-]|$)(?![?=&+%\w.-]*(?:['"][^<>]*>|<\/a>))[?=&+%\w.-]*/ig;
return text.replace(re,
'YouTube link: $1');
}
Try this:
var re = /(?:youtube\.com\/(?:[^\/]+\/.+\/|(?:v|e(?:mbed)?)\/|.*[?&]v=)|youtu\.be\/)([^"&?\/ ]{11})/i;
REGEX DEMO
how to get youtube video id via javascript can anyone help me
here is the youtube http://www.youtube.com/watch?v=mG6DRzIyhOo
For your simple case; use the = javascript String.substr function :
var url = "http://www.youtube.com/watch?v=mG6DRzIyhOo";
var video_id = url.substr(url.indexOf("=")+1);
But since youtube can have a variety of different video url formats, I would recommend using the following solution using regular expressions :
https://stackoverflow.com/a/8260383/3303171