Embed a YouTube channel's current live stream without the video ID - youtube-api

Every time a user starts a live stream on YouTube, a new ID is generated for the stream, along with the corresponding video and URL. Currently, if I want to embed a live stream, I can use YouTube's sharing functions to add the video to my page in an iFrame.
When the user stops streaming, embeds of the live stream automatically switch to showing a recording of that stream. However, if the user starts broadcasting again later on, the embed will continue to show the old recording instead of switching to the new stream. This is because the video ID in the embed is hard-coded and each stream generates a new video ID.
My goal is to create an embed that will automatically display a user's live stream whenever they are streaming, and show an indication of whether they're online or offline. Is there an embed URL that would allow me to do this, or is there something in the API that might help?
I want to embed other streams that aren't just my own, so I need to do this in a way that doesn't require the streamer to log in or authenticate on my site.

If you know the ID of a YouTube channel, and if that channel streams a livestream set to Public, an iframe with this URL will show it:
https://www.youtube.com/embed/live_stream?channel=YOUR_CHANNEL_ID_HERE
See https://stackoverflow.com/a/39582176/470749
Unfortunately I haven't found a similarly simple way to permanently embed the YouTube chat for that livestream.

As far as I can tell, there's nothing built into the YouTube API that would allow you to embed a channel's current live stream automatically without knowing its ID. That said, it's possible to implement this yourself by writing a custom API and hosting it on your own server.
I recognize that this can look like a daunting task, so I've laid out some rough steps below to get you started.
Set up an endpoint on your own server. You could accept a
channelId argument or hard-code one, depending on how extensible
you want this to be.
Query YouTube's search endpoint1 for the specified channelId and eventType=live. An HTTPS request for this will look something like this:https://www.googleapis.com/youtube/v3/search?part=snippet&channelId=[CHANNEL_ID]&eventType=live&maxResults=1&order=date&type=video&key=[YOUR_API_KEY]
Check the search JSON response. If it returns any results (data.pageInfo.totalResults > 0), you know that the channel is live.
If the channel is live, redirect the request to your server directly to the live video's embed URL based on the video's ID in the query response (data.items[0].id.videoId).
If the channel isn't live, create a placeholder as you see fit, or make a second request to search for eventType=completed for past broadcasts, eventType=upcoming for scheduled broadcasts, or remove the eventType parameter to get the most recent video.
Once you have a server that can respond and redirect requests, you can embed an iFrame in your page that points directly to your API URL. Your server will handle the logic and, using the redirect, change the iFrame to a YouTube video player automatically, without requiring you to perform client-side logic or expose your API key2.
1 As with all YouTube API requests, search#list queries will count towards your daily quota. If you intend for this to be a high-traffic endpoint, you could either request an increased quota from YouTube, or implement a caching solution on your end to cut down on the number of requests you make.
2 GCP (Google Cloud Platform), which you'll use to manage your access the YouTube Data API, has pretty good protections against API key theft for times when you do have to expose it on the client side. That being said, best practice is to keep your key secret by storing it only on the server whenever possible.

Related

How to get YouTube channel's trailer and featured video via the Data API?

I'm trying to use the YouTube Data API to get a channel's Trailer and Featured Video but can't find anything in the [API docs][1] or responses that might provide it. Ideally, there'd be something that would return the ID of whichever videos had been selected here: https://studio.youtube.com/channel/[CHANNEL_ID]/editing/sections
Maybe it's buried in an obscure endpoint. Maybe the API simply doesn't include this anywhere. Can anyone please point me in the right direction?
Update: I've found the channel trailer as unsubscribedTrailer under the channels:list endpoint with the brandingSettings part. However, for some reason Google has decided not to include the featured video with it.
One more time YouTube Data API v3 doesn't provide a basic feature.
As Video spotlight you can have:
Featured video for returning subscribers
Highlight a video for your subscribers to watch. This video won’t be shown again at the top of your page for subscribers who have watched it. Learn more
Source: https://studio.youtube.com/channel/CHANNEL_ID/editing/sections
For the featured video:
you first need to subscribe to the give YouTube channel. To do so in an automatic way, use YouTube Data API v3 Subscriptions: insert endpoint.
then open your web-browser Network developer tool tab (Ctrl + Shift + E on FireFox) and filter HTML requests, then visit https://www.youtube.com/channel/CHANNEL_ID and copy the initial request to CHANNEL_ID as cURL, that way you can re-execute this cURL request for any channel you are subscribed to by changing the URL in the cURL request to https://www.youtube.com/channel/ANOTHER_CHANNEL_ID. Furthermore you'll find the featured video id in the JavaScript variable ytInitialData in the JSON entry
contents/twoColumnBrowseResultsRenderer/tabs/0/tabRenderer/content/sectionListRenderer/contents/0/itemSectionRenderer/contents/0/channelFeaturedContentRenderer/items/0/videoRenderer/videoId.
The channel with id UCv_LqFI-0vMVYgNR3TeB3zQ have both a channel trailer (1RHxvM8mQS4) and a featured video (rFuip5CSWcA).

Does YouTube Data API v3 provide video stream URLs?

Using the YouTube Data API v3, is it actually able to return to me the URL for a video (by ID) of all the available video and audio streams? I have the YouTube video ID. I want to choose a video stream to play in a client app. I've read all the YouTube Data API documentation on this site:
https://developers.google.com/youtube/v3
And this question is not answered; in fact it seems to intentionally avoid this whole topic.
Sorry for having to give you a negative response:
As far as I know, the answer to your question is no, there's no such API (and API endpoint for that matter) that will provide the URLs of the streams that YouTube does yield to its video players (embedded or not).
Of course, one may scrape this kind of info out of the YouTube's Web UI (like many tools available on the Internet do), but, if one does observe the DTOS specifications, will eventually come across the paragraph E.6 of section III. General Developer Policies, that explicitly forbids this activity.
An argument against the existence of such a API can also be derived from the DTOS document, same section III. General Developer Policies, but, in this case, from paragraph E.1:
E. Handling YouTube Data and Content
Aside from the permissions and rights granted in this section, you and your API Clients have no further permissions or rights to API Data, including to temporarily stored API Data.
Audiovisual Content
You and your API Clients must not, and must not encourage, enable, or require others to:
a. download, import, backup, cache, or store copies of YouTube audiovisual content without YouTube's prior written approval,
b. make content available for offline playback
c. [...]
Since is illegal to download video content from YouTube, it follows by simple formal logic, that it cannot exist an API that would provide URLs to the video content itself (under the assumption that the official APIs are formally consistent with the DTOS specifications).
No the YouTube API doesn't give you the video url, otherwise it would be too easy to copy the file.
Also, YouTube videos can only be played inside the YouTube iFrame player and you can't have access to the HTML5 video object (to retrieve the video url) because of cross-domain policy.

MVC Event User Online Accessing Specific HLS Video Streaming

I may have several HLS streaming video address. I'd like to have a real time event telling any change of how many user online who access each of the streaming file.
User no need to pass in login page. They may retrieve the video streaming url directly and put into their player.
How do I accomplish this task in MVC?
If you allow them to play the URL in their own player, you'll need software on the media server that's creating the HLS streams to keep track of it for you.
If you were providing users with a web page that had a player on it, and not giving them the URL directly, you would just need to count how many people are on the page, and you wouldn't have to worry about the video at all.

Service to host streaming videos for mobile access where urls are not trackable

I'm building an iOS app and Android app that will display a series of private videos. Someone will purchase the app for x amount and then have access to the videos through that app only.
I already know a couple of ways to do that part. But the real trick is hiding the video urls to traffic sniffers/etc. I don't want anyone to be able to detect the video urls, or at least the endpoint will reject a request without an auth token.
So I could build my own Node/Express server, incorporate wowza maybe with Amazon to store the files - but that is a lot of work.
So what is the simplest solution to stream my videos to mobile without people being able to load up the videos outside of the app?
It looks like you'll need to implement some sort of authentication system, so that even if they get the video url somehow, they will be unable to view it without the authentication key.
Your videos should be hosted in a directory on your server that is inaccessible from the web. Then use some sort of index page which takes a parameter for the video ID and does the authentication before serving up the video file contents.

Google Play Web Video Player API

Is there an API for the Google Play online video playback of content? Would the YouTube API work?
I am wanting to embed Google Play content into a website I am building.
Thank you,
Joseph Irvine
Google Play movie purchases and rentals do also show up as YouTube videos (with a unique YouTube video ID), and so it is possible; obviously, you'd need to use oAuth2 authentication so when a user logs in, YouTube can verify that the user has the permission to see that film/TV show.
The real trick, however, is getting the right YouTube ID. They show up through search results via the search endpoint (so, for example, https://www.googleapis.com/youtube/v3/search?part=snippet&q=Monsters+University&key={YOUR_API_KEY} would be such a search), but that endpoint only gives you access to the "snippet" content type, which doesn't include the parameter "licensedContent" to let you know if it's a for-pay video (that parameter is found in the "contentDetails" type, which is only available from a video list call).

Resources