youtube-thumbnail.zip
0.00MB

 

gist.github.com/protrolium/8831763

 

YouTube API — getting video thumbnail

YouTube API — getting video thumbnail. GitHub Gist: instantly share code, notes, and snippets.

gist.github.com

 

js를 이용해서 youtube 섬네일 가져온다.

원하는 유튜브의 주소만 넣어주면 자동적으로 썸네일 생성한다.

썸네일의 화질의 경우 원하면 js쪽만 바꿔주면된다.

 

html (bootstrap)

<!DOCTYPE html>
<html lang="en">
<head>
   <meta charset="UTF-8">
   <meta name="viewport" content="width=device-width, initial-scale=1.0">
   <title>Youtube Thumbnail</title>
   <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
   <link rel="stylesheet" href="style.css" />
</head>
<body>
   <div class="container">
      <h1 class="text-center text-info m-5">Youtube Thumbnail</h1>
      <hr />
      <div class="form-group mt-5">
         <label for="youtubeId">Youtube URL</label>
         <input type="text" class="form-control m-3 youtube-url" />
         <input type="text" class="form-control m-3 youtube-url" />
         <input type="text" class="form-control m-3 youtube-url" />
         <input type="text" class="form-control m-3 youtube-url" />
         <input type="text" class="form-control m-3 youtube-url" />
         <button class="btn btn-primary btn-block mt-3" id="getBtn">Get Thumbnail</button>
      </div>
      <div class="thumbnail-wrap">
         
      </div>
   </div> 
   <script src="app.js"></script>
</body>
</html>

 

js

const getButton = document.querySelector("#getBtn");
const youtubeUrl = document.querySelectorAll(".youtube-url");
const thumbnailWrap = document.querySelector(".thumbnail-wrap");

getButton.addEventListener('click', getThum);

function getThum() {
   let thumArr = [];
   youtubeUrl.forEach(url => {
      if (url.value !== "") {
         let replaceUrl = url.value;
         let finUrl = '';
         replaceUrl = replaceUrl.replace("https://youtu.be/", '');
         replaceUrl = replaceUrl.replace("https://www.youtube.com/embed/", '');
         replaceUrl = replaceUrl.replace("https://www.youtube.com/watch?v=", '');
         finUrl = replaceUrl.split('&')[0];
         thumArr.push(finUrl);
      }
   });
   thumArr.forEach(thum => {
      let img = document.createElement("img");
      img.setAttribute("src", `https://img.youtube.com/vi/${thum}/mqdefault.jpg`)
      thumbnailWrap.appendChild(img);
   });
};

 

css

@import url("https://fonts.googleapis.com/css2?family=Poppins:ital,wght@200,400&display=swap");

* {
	box-sizing: border-box;
}

body {
	font-family: "Poppins", sans-serif;
	margin: 0;
}

img { 
	width: 100%;
	margin: 30px 0;
}