为什么嵌入的 Youtube 视频在上周工作时显示视频不可用?没有代码更改和 Youtube Data V3 API
Why are embedded Youtube videos saying Video Unavailable when they were working last week? No code changes and Youtube Data V3 API
我正在从我的网页流式传输一堆音乐视频。
我写了一个脚本来挑选可用于流式传输的 Youtube 音乐视频。我使用 requests.get(url).json() 获取每个视频的 json 信息,然后从那里使用这行代码
if url_get.json()['items'] != [] and url_get.json()['items'][0]['status']['uploadStatus'] == "processed" and url_get.json()['items'][0]['status']['embeddable'] == "true":
决定 Youtube 视频是否可以流式传输以及我是否可以嵌入它。
但是,无论我先嵌入哪个视频,当我测试我的网页时,它都显示为视频不可用。
相同的网页代码上周实际上运行良好。剧本我一直在改。
在从我的网页嵌入和流式传输时,我没有向 Youtube 提供任何 API 密钥,所以我想知道为什么尽管检查了视频是否 "embeddable" 和 "processed".
这是我正在使用的脚本的相关部分:
#takes all files and checks if available to stream on youtube
ids = [id.strip("_arousal.csv") for id in glob.glob("*_arousal.csv")]
available_ids = []
unavailable_ids = []
for i in ids:
url = f'https://www.googleapis.com/youtube/v3/videos?id={i}&part=status&key={your_api_key}'
url_get = requests.get(url)
print(url_get.json())
if url_get.json()['items'] != [] and url_get.json()['items'][0]['status']['uploadStatus'] == "processed" and url_get.json()['items'][0]['status']['embeddable'] == "true":
assign_score(i)
available_ids.append(i)
else:
unavailable_ids.append(i)
这是在我网页上的 Youtube 视频中加载的代码:
//-------------------------- step 3: instantiating javascript objects, data collection
// 2. This code loads the IFrame Player API code asynchronously.
var tag = document.createElement('script');
tag.src = "https://www.youtube.com/iframe_api";
var firstScriptTag = document.getElementsByTagName('script')[0];
firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);
// 3. This function creates an <iframe> (and YouTube player)
// after the API code downloads.
var players = [];
var i;
function onYouTubeIframeAPIReady() {
for (i = 0; i < num_media; i++) {
if (document.getElementById('video_'+i.toString()) != null) {
let end_time = 60; //show first minute, or for debugging purposes 10s
if (randomized[i][1] < 60) {
end_time = randomized[i][1]; //unless video is shorter than a minute
}
players.push(new YT.Player('video_'+i.toString(), {
height: '315',
width: '420',
videoId: randomized[i][0],
playerVars: {'controls':0, 'autoplay':0, 'fs':0, 'end': end_time},
events: {
'onError':onPlayerError,
'onStateChange':onPlayerStateChange
}
}));
} else {
//music at trial i, just leave player[i] blank
players.push(null);
}
}
}
//player.getDuration()?
function onPlayerStateChange(event) {
let id = event.target.getIframe().id.split("_")[1]; //string
let num = parseInt(id, 10); //int
let canvas = canvases[num];
if (event.data == YT.PlayerState.ENDED) {
clearInterval(canvas.data_collection);
//double clearing the interval, b/c couldn't find documentation about what data_collection is set to after clearInterval
canvas.data_collection = null;
canvas.start_time = null;
let stringv = "";
let stringa = "";
for (var i = 0; i < canvas.valences.length; i+=1) {
stringv = stringv.concat(canvas.valences[i].join(',') + ";");
}
stringv.trimRight(";");
for (var i = 0; i < canvas.arousals.length; i+=1) {
stringa = stringa.concat(canvas.arousals[i].join(',') + ";");
}
stringa.trimRight(";");
$( "#valences_"+id).attr('value', stringv);
$( "#arousals_"+id ).attr('value', stringa);
showSubmit(num);
} else if (event.data == YT.PlayerState.BUFFERING) {
clearInterval(canvas.data_collection); //don't record
//double clearing the interval, b/c couldn't find documentation about what data_collection is set to after clearInterval
canvas.data_collection = null;
} else if (event.data == YT.PlayerState.PLAYING && canvas.data_collection == null) {
//may need to check start_time and devise a timeout that would shift the sample to right times
if (canvas.start_time == null) {
canvas.start_time = new Date();
}
//setInterval doesn't record starting element, so I do that here
canvas.valences.push([0, canvas.outputX]);
canvas.arousals.push([0, canvas.outputY]);
canvas.data_collection = setInterval(function(){ logKey(num); }, 500);
} else if (event.data == YT.PlayerState.CUED) {
//cued doesn't seem to be getting called
canvas.start_time = new Date();
canvas.valences.push([0, canvas.outputX]);
canvas.arousals.push([0, canvas.outputY]);
canvas.data_collection = setInterval(function(){ logKey(num); }, 500);
} else if (event.data == YT.PlayerState.PAUSED) {
//forces the user to keep watching and rating - no pauses allowed! :D
event.target.playVideo();
}
}
//throw out data if there is a player error???
function onPlayerError() {
let num = parseInt(event.target.getIframe().parentNode.split("_")[1], 10); //int
let canvas = canvases[num];
canvas.valences = [];
canvas.arousals = [];
}
请让我知道为什么视频仍然显示为“视频不可用”的任何建议。谢谢!
通常,当代码正在运行时,YouTube 数据 API 突然停止正常运行,最好的选择是在 Issue Tracker 中报告此类问题 - 这是因为它可能在API.
另外,如果这些视频确实不可用,您是否在浏览器中检查过? - 有时,某些视频会更改其属性并且 API might
不会更新,或者拥有该视频的用户决定允许 (or not)
嵌入其视频。
我正在从我的网页流式传输一堆音乐视频。
我写了一个脚本来挑选可用于流式传输的 Youtube 音乐视频。我使用 requests.get(url).json() 获取每个视频的 json 信息,然后从那里使用这行代码
if url_get.json()['items'] != [] and url_get.json()['items'][0]['status']['uploadStatus'] == "processed" and url_get.json()['items'][0]['status']['embeddable'] == "true":
决定 Youtube 视频是否可以流式传输以及我是否可以嵌入它。
但是,无论我先嵌入哪个视频,当我测试我的网页时,它都显示为视频不可用。
相同的网页代码上周实际上运行良好。剧本我一直在改。
在从我的网页嵌入和流式传输时,我没有向 Youtube 提供任何 API 密钥,所以我想知道为什么尽管检查了视频是否 "embeddable" 和 "processed".
这是我正在使用的脚本的相关部分:
#takes all files and checks if available to stream on youtube
ids = [id.strip("_arousal.csv") for id in glob.glob("*_arousal.csv")]
available_ids = []
unavailable_ids = []
for i in ids:
url = f'https://www.googleapis.com/youtube/v3/videos?id={i}&part=status&key={your_api_key}'
url_get = requests.get(url)
print(url_get.json())
if url_get.json()['items'] != [] and url_get.json()['items'][0]['status']['uploadStatus'] == "processed" and url_get.json()['items'][0]['status']['embeddable'] == "true":
assign_score(i)
available_ids.append(i)
else:
unavailable_ids.append(i)
这是在我网页上的 Youtube 视频中加载的代码:
//-------------------------- step 3: instantiating javascript objects, data collection
// 2. This code loads the IFrame Player API code asynchronously.
var tag = document.createElement('script');
tag.src = "https://www.youtube.com/iframe_api";
var firstScriptTag = document.getElementsByTagName('script')[0];
firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);
// 3. This function creates an <iframe> (and YouTube player)
// after the API code downloads.
var players = [];
var i;
function onYouTubeIframeAPIReady() {
for (i = 0; i < num_media; i++) {
if (document.getElementById('video_'+i.toString()) != null) {
let end_time = 60; //show first minute, or for debugging purposes 10s
if (randomized[i][1] < 60) {
end_time = randomized[i][1]; //unless video is shorter than a minute
}
players.push(new YT.Player('video_'+i.toString(), {
height: '315',
width: '420',
videoId: randomized[i][0],
playerVars: {'controls':0, 'autoplay':0, 'fs':0, 'end': end_time},
events: {
'onError':onPlayerError,
'onStateChange':onPlayerStateChange
}
}));
} else {
//music at trial i, just leave player[i] blank
players.push(null);
}
}
}
//player.getDuration()?
function onPlayerStateChange(event) {
let id = event.target.getIframe().id.split("_")[1]; //string
let num = parseInt(id, 10); //int
let canvas = canvases[num];
if (event.data == YT.PlayerState.ENDED) {
clearInterval(canvas.data_collection);
//double clearing the interval, b/c couldn't find documentation about what data_collection is set to after clearInterval
canvas.data_collection = null;
canvas.start_time = null;
let stringv = "";
let stringa = "";
for (var i = 0; i < canvas.valences.length; i+=1) {
stringv = stringv.concat(canvas.valences[i].join(',') + ";");
}
stringv.trimRight(";");
for (var i = 0; i < canvas.arousals.length; i+=1) {
stringa = stringa.concat(canvas.arousals[i].join(',') + ";");
}
stringa.trimRight(";");
$( "#valences_"+id).attr('value', stringv);
$( "#arousals_"+id ).attr('value', stringa);
showSubmit(num);
} else if (event.data == YT.PlayerState.BUFFERING) {
clearInterval(canvas.data_collection); //don't record
//double clearing the interval, b/c couldn't find documentation about what data_collection is set to after clearInterval
canvas.data_collection = null;
} else if (event.data == YT.PlayerState.PLAYING && canvas.data_collection == null) {
//may need to check start_time and devise a timeout that would shift the sample to right times
if (canvas.start_time == null) {
canvas.start_time = new Date();
}
//setInterval doesn't record starting element, so I do that here
canvas.valences.push([0, canvas.outputX]);
canvas.arousals.push([0, canvas.outputY]);
canvas.data_collection = setInterval(function(){ logKey(num); }, 500);
} else if (event.data == YT.PlayerState.CUED) {
//cued doesn't seem to be getting called
canvas.start_time = new Date();
canvas.valences.push([0, canvas.outputX]);
canvas.arousals.push([0, canvas.outputY]);
canvas.data_collection = setInterval(function(){ logKey(num); }, 500);
} else if (event.data == YT.PlayerState.PAUSED) {
//forces the user to keep watching and rating - no pauses allowed! :D
event.target.playVideo();
}
}
//throw out data if there is a player error???
function onPlayerError() {
let num = parseInt(event.target.getIframe().parentNode.split("_")[1], 10); //int
let canvas = canvases[num];
canvas.valences = [];
canvas.arousals = [];
}
请让我知道为什么视频仍然显示为“视频不可用”的任何建议。谢谢!
通常,当代码正在运行时,YouTube 数据 API 突然停止正常运行,最好的选择是在 Issue Tracker 中报告此类问题 - 这是因为它可能在API.
另外,如果这些视频确实不可用,您是否在浏览器中检查过? - 有时,某些视频会更改其属性并且 API might
不会更新,或者拥有该视频的用户决定允许 (or not)
嵌入其视频。