File size: 7,313 Bytes
a2beb26 9466647 29ab239 9466647 29ab239 9466647 29ab239 9466647 a2beb26 29ab239 a2beb26 29ab239 eff419d 29ab239 a2beb26 29ab239 a2beb26 29ab239 a2beb26 29ab239 a2beb26 9466647 29ab239 9466647 a2beb26 29ab239 a2beb26 975d782 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 |
<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>YouTube 埋め込みと関連動画表示</title>
<style>
body {
font-family: Arial, sans-serif;
background-color: #f4f4f4;
color: #333;
margin: 0;
padding: 0;
}
h1 {
text-align: center;
margin-top: 30px;
font-size: 2rem;
color: #444;
}
input[type="text"] {
width: 80%;
padding: 10px;
margin: 20px auto;
display: block;
font-size: 1rem;
border: 2px solid #ccc;
border-radius: 5px;
box-sizing: border-box;
}
button {
padding: 10px 20px;
font-size: 1rem;
background-color: #007bff;
color: white;
border: none;
border-radius: 5px;
cursor: pointer;
display: block;
margin: 10px auto;
transition: background-color 0.3s ease;
}
button:hover {
background-color: #0056b3;
}
#video-container, #mp4-container {
text-align: center;
margin-top: 30px;
}
iframe, video {
border-radius: 10px;
box-shadow: 0 0 15px rgba(0, 0, 0, 0.1);
width: 560px;
height: 315px;
}
#related-videos {
display: grid;
grid-template-columns: repeat(auto-fill, minmax(250px, 1fr));
gap: 20px;
margin-top: 30px;
padding: 0 20px;
}
.related-video {
background-color: #fff;
border-radius: 10px;
box-shadow: 0 0 15px rgba(0, 0, 0, 0.1);
overflow: hidden;
text-align: center;
transition: transform 0.3s ease;
}
.related-video:hover {
transform: translateY(-5px);
}
.related-video img {
width: 100%;
border-bottom: 2px solid #f4f4f4;
}
.related-video h4 {
font-size: 1.1rem;
margin: 10px 0;
color: #007bff;
font-weight: normal;
}
.related-video p {
font-size: 0.9rem;
color: #555;
margin: 0 10px;
}
pre {
background-color: #2c3e50;
color: #ecf0f1;
padding: 20px;
border-radius: 10px;
white-space: pre-wrap;
word-wrap: break-word;
font-size: 0.9rem;
margin-top: 30px;
}
</style>
</head>
<body>
<h1>YouTubeのURLを入力してください</h1>
<input type="text" id="youtube-url" placeholder="https://www.youtube.com/watch?v= のURLを入力">
<button onclick="processVideo()">処理</button>
<br><br>
<div id="video-container"></div>
<div id="mp4-container"></div>
<br>
<h3>関連動画:</h3>
<div id="related-videos"></div>
<br>
<h3>End Screenデータ(JSON):</h3>
<pre id="json-data"></pre>
<script>
async function processVideo() {
// 入力されたYouTube URLを取得
const url = document.getElementById('youtube-url').value;
// 正しいYouTube URLであることを確認
const regex = /https:\/\/www\.youtube\.com\/watch\?v=([a-zA-Z0-9_-]{11})/;
const matches = url.match(regex);
if (!matches || !matches[1]) {
alert("YouTubeのURLが正しくありません。");
return;
}
// YouTubeの動画IDを取得
const videoId = matches[1];
// MP4動画のURLを生成
const mp4Url = `https://inv-cl2-c.nadeko.net:8443/latest_version?itag=18&id=${videoId}`;
// YouTube動画の埋め込みiframeを生成
const iframeHtml = `<iframe width="560" height="315" src="https://www.youtube.com/embed/${videoId}?rel=0&modestbranding=1" frameborder="0" allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>`;
document.getElementById('video-container').innerHTML = iframeHtml;
// MP4動画を表示
const mp4Html = `<video controls><source src="${mp4Url}" type="video/mp4">このブラウザはMP4動画をサポートしていません。</video>`;
document.getElementById('mp4-container').innerHTML = mp4Html;
// corsproxy.io を使って YouTube のページを取得
const proxyUrl = `https://corsproxy.io/?https://www.youtube.com/watch?v=${videoId}`;
try {
const response = await fetch(proxyUrl);
const html = await response.text();
// ytInitialData の JSON 部分を抽出
const scriptRegex = /<script nonce=".*?">(.+?)<\/script>/g;
let match;
let ytInitialData = null;
while ((match = scriptRegex.exec(html)) !== null) {
if (match[1].includes('ytInitialData')) {
const jsonDataMatch = match[1].match(/var ytInitialData = ({.*?});/);
if (jsonDataMatch && jsonDataMatch[1]) {
ytInitialData = JSON.parse(jsonDataMatch[1]);
break;
}
}
}
if (ytInitialData) {
const endScreenResults = ytInitialData.playerOverlays?.playerOverlayRenderer?.endScreen?.watchNextEndScreenRenderer?.results;
if (endScreenResults) {
const relatedVideosContainer = document.getElementById('related-videos');
relatedVideosContainer.innerHTML = '';
endScreenResults.forEach((video) => {
const videoId = video.endScreenVideoRenderer.videoId;
const thumbnailUrl = video.endScreenVideoRenderer.thumbnail.thumbnails[0].url;
const title = video.endScreenVideoRenderer.title.simpleText;
const videoHtml = `
<div class="related-video">
<a href="https://www.youtube.com/watch?v=${videoId}" target="_blank">
<img src="${thumbnailUrl}" alt="${title}" />
</a>
<h4>${title}</h4>
</div>
`;
relatedVideosContainer.innerHTML += videoHtml;
});
document.getElementById('json-data').textContent = JSON.stringify(endScreenResults, null, 2);
}
}
} catch (error) {
alert("データの取得中にエラーが発生しました。");
console.error(error);
}
}
</script>
</body>
</html>
|