Know if a video Youtube is unavailable with the API
Asked Answered
S

3

11

Via the Youtube API, how can I detect if a video Youtube is unavailable (ex : https://www.youtube.com/watch?v=5nRZlcB2jPY) ?

Thanks

Silicate answered 9/9, 2015 at 12:51 Comment(2)
welcome to stack please read stackoverflow.com/help/how-to-askInterior
If a video is not available it will return 403 error code...you can do a callback if the response is error freeSharonsharona
B
3

You would make an API call for the video status.

https://www.googleapis.com/youtube/v3/videos?id=VIDEOID&part=status&key=APIKEY

Then check the uploadStatus in the json result:

"status": {
"uploadStatus": "processed",
"privacyStatus": "public",
"license": "youtube",
"embeddable": true,
"publicStatsViewable": true
}
Blinker answered 10/9, 2015 at 13:20 Comment(5)
Great ! That's it ! ThanksSilicate
Well the video: youtube.com/watch?v=6k4108PYgOk is not available on YouTube but the YouTube API reports the following status for the video:"status": { "uploadStatus": "processed", "privacyStatus": "public", "license": "youtube", "embeddable": true, "publicStatsViewable": true }. What should be the correct status for an available video ?Sedgewake
It's unavailable on YouTube for your region. Check region restrictions too: unblockvideos.com/youtube-video-restriction-checker/…Blinker
Vincent, video 8EzdmE4Ewqk is fine, and don't hijack someone's thread.Blinker
@Blinker comments useful for me. Thanks alot.Scrag
D
2

This is also partially possible without API. Let's say you want to see if the following video is available:

https://www.youtube.com/watch?v=esDJPiGu5x0

The ID of the video is shown as the GET parameter v. Use this one to request the following thumbnail:

https://img.youtube.com/vi/esDJPiGu5x0/0.jpg

If the content of the response has a length of 0 and/or the http response code by youtube.com is 404, then the video is not available anymore.

Demolish answered 27/6, 2017 at 9:14 Comment(2)
Can't confirm. It just worked for me. Perhaps the preview images were not generated yet?Demolish
If image doesn't exist, then video doesn't exist. But if the image exists, it is no guarantee that the video is availableSybilsybila
C
1

You can check it without API.

const https = require('https');

// Change your videoId that you want to check here
const url = 'https://www.youtube.com/oembed?url=http://www.youtube.com/watch?v=videoID&format=json';

https.get(url, (response) => {
  if (response.statusCode === 200) {
    console.log('Video available');
  } else {
    console.log('Video not available');
  }
}).on('error', (error) => {
  console.error('Error:', error);
});

If you get a response of 200, the video is available. If not, the video is unavailable

Charleton answered 13/6, 2023 at 21:11 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.