yt-if / index.html
soiz1's picture
Update index.html
eff419d verified
<!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>