[이윰] 유튜브 플레이리스트(playlist) 썸네일 추출과 올바르게 적용하기
페이지 정보
shadow2fox
3,650 7 3
2021.09.01 10:20:26
짧은주소
-
https://yadolee.com/tip/905 주소복사
본문
📝 유튜브 플레이리스트(playlist) 썸네일 추출과 올바르게 적용하기
유튜브에서 제공하는 플레이리스트(playlist) 공유 주소에는 세가지 패턴이 있다.
그러므로 언급된 문제점을 해결하기 위해선 YouTube Data API Key가 있어야 하며 YouTube Data API Key가 없다면 https://console.cloud.google.com/를 통해 Key를 발급받아야 한다.
YouTube Data API Key 발급 과정은 구글신을 통해 해결하도록 한다.
그누보드5/eyoom/classes/eyoom.class.php를 열어 // 동영상 경로로 부터 동영상정보 가져오기를 검색한다.
수정 전{code:php}
private function video_from_soruce($src) {
$url = $src[0];
$video_url = trim(strip_tags($url));
$video_url = preg_replace('/amp;/','&',$video_url);
$info = $this->eyoom_host($video_url);
$host = $info['host'];
$query = $info['query'];
$video['host'] = $host;
{/code}수정 후{code:php}
private function video_from_soruce($src) {
$url = $src[0];
$video_url = trim(strip_tags($url));
$video_url = preg_replace('/amp;/','&',$video_url);
$info = $this->eyoom_host($video_url);
if(isset($info['host'])) $host = $info['host'];
if(isset($info['query'])) $query = $info['query'];
$video['host'] = $host ?? '';
if(isset($host)) {
$explode_url = explode($host.'/',$url);
$src[1] = $explode_url[1] ?? '';
//watch?v=가 포함된 공유주소에서 watch?v=제거 후 동영상 key 추출하기
if(stripos($src[1], 'watch?v=') !== false) $src[1] = str_replace('watch?v=', '', $src[1]);
}
{/code}
// 동영상 key 추출하기을 검색한다.
수정 전{code:php}
case 'youtube.com':
if ($src[1]) {
$video['key1'] = $src[1];
} else {
$video['key1'] = $query['v'];;
}
break;
{/code}수정 후{code:php}
case 'youtu.be':
case 'youtube.com':
if (isset($src[1]) && $src[1] && !(isset($query['list']) && $query['list'])) {
$video['key1'] = $src[1];
} else {
//유튜브 재생목록 가져오기
if(isset($query['v']) && $query['v']) {
$video['key1'] = $query['v'];
$explode_key = explode("list=",$video['key1']);
$video['key1'] = $explode_key[0];
$video['vlist'] = $explode_key[1];
} else {
if(isset($query['list'])) $video['vlist'] = $query['list'];
}
}
break;
{/code}
// 수집된 동영상 정보를 iframe source로 구현을 검색한다.
수정 전{code:php}
case 'youtu.be':
case 'youtube.com':
$vlist = $video['key2'] ? '&list='.$video['key2'] : '';
$source = '<iframe width="'.$video['width'].'" height="'.$video['height'].'" src="http://www.youtube.com/embed/'.$video['key1'].'?wmode=opaque&autohide=1'.$vlist.'" frameborder="0" allowfullscreen></iframe>';
break;
{/code}수정 후{code:php}
case 'youtu.be':
case 'youtube.com':
if(isset($video['vlist']) && $video['vlist']) {
$vlist = '&list='.$video['vlist']; //유튜브 재생목록 가져오기
$source = '<iframe width="'.$video['width'].'" height="'.$video['height'].'" src="https://www.youtube.com/embed/?wmode=opaque&autohide=1&showinfo=0&modestbranding=1&rel=0&enablejsapi=1&vq=hd1080'.$vlist.'" frameborder="0" allowfullscreen></iframe>';
} else {
$source = '<iframe width="'.$video['width'].'" height="'.$video['height'].'" src="https://www.youtube.com/embed/'.$video['key1'].'?wmode=opaque&autohide=1&showinfo=0&modestbranding=1&rel=0&enablejsapi=1&vq=hd1080" frameborder="0" allowfullscreen></iframe>';
}
break;
{/code}
URL로부터 동영상 이미지 경로를 찾기를 검색한다.
수정 전{code:php}
case 'youtu.be':
case 'youtube.com':
$path_name = mb_substr($video['key1'],0,11,"utf-8");
$video['img_url'] = "http://img.youtube.com/vi/{$path_name}/maxresdefault.jpg";
break;
{/code}수정 후{code:php}
case 'youtu.be':
case 'youtube.com':
$path_name = mb_substr($video['key1'],0,11,"utf-8");
if(isset($video['key1']) && $video['key1']) {
$video['img_url'] = "https://img.youtube.com/vi/{$path_name}/maxresdefault.jpg";
} else {
$url = 'https://www.googleapis.com/youtube/v3/playlists?part=snippet&id='.$video['vlist'].'&key=YouTube Data API Key';
$data = $this->curl_web_scripping($url);
$json = json_decode($data,true);
//플레이리스트 썸네일 추출 용 Json 데이타에 maxresdefault.jpg 값이 존재하면 maxresdefault.jpg를 이용해 썸네일을 생성하고 존재하지 않으면 sddefault.jpg를 이용해 생성
$maxres = $json['items'][0]['snippet']['thumbnails']['maxres']['url'];
$standard = $json['items'][0]['snippet']['thumbnails']['standard']['url'];
if(isset($maxres)) $video['img_url'] = $maxres;
else $video['img_url'] = $standard;
}
break;
{/code}
- https://youtu.be/QNdFgAAXQ_c?list=PLsOL9rj0pde4hqwb4afL8yhq3cZUFuj3s
- https://www.youtube.com/watch?v=QNdFgAAXQ_c&list=PLsOL9rj0pde4hqwb4afL8yhq3cZUFuj3s
- https://www.youtube.com/playlist?list=PLsOL9rj0pde4hqwb4afL8yhq3cZUFuj3s
그러므로 언급된 문제점을 해결하기 위해선 YouTube Data API Key가 있어야 하며 YouTube Data API Key가 없다면 https://console.cloud.google.com/를 통해 Key를 발급받아야 한다.
YouTube Data API Key 발급 과정은 구글신을 통해 해결하도록 한다.
그누보드5/eyoom/classes/eyoom.class.php를 열어 // 동영상 경로로 부터 동영상정보 가져오기를 검색한다.
수정 전{code:php}
private function video_from_soruce($src) {
$url = $src[0];
$video_url = trim(strip_tags($url));
$video_url = preg_replace('/amp;/','&',$video_url);
$info = $this->eyoom_host($video_url);
$host = $info['host'];
$query = $info['query'];
$video['host'] = $host;
{/code}수정 후{code:php}
private function video_from_soruce($src) {
$url = $src[0];
$video_url = trim(strip_tags($url));
$video_url = preg_replace('/amp;/','&',$video_url);
$info = $this->eyoom_host($video_url);
if(isset($info['host'])) $host = $info['host'];
if(isset($info['query'])) $query = $info['query'];
$video['host'] = $host ?? '';
if(isset($host)) {
$explode_url = explode($host.'/',$url);
$src[1] = $explode_url[1] ?? '';
//watch?v=가 포함된 공유주소에서 watch?v=제거 후 동영상 key 추출하기
if(stripos($src[1], 'watch?v=') !== false) $src[1] = str_replace('watch?v=', '', $src[1]);
}
{/code}
// 동영상 key 추출하기을 검색한다.
수정 전{code:php}
case 'youtube.com':
if ($src[1]) {
$video['key1'] = $src[1];
} else {
$video['key1'] = $query['v'];;
}
break;
{/code}수정 후{code:php}
case 'youtu.be':
case 'youtube.com':
if (isset($src[1]) && $src[1] && !(isset($query['list']) && $query['list'])) {
$video['key1'] = $src[1];
} else {
//유튜브 재생목록 가져오기
if(isset($query['v']) && $query['v']) {
$video['key1'] = $query['v'];
$explode_key = explode("list=",$video['key1']);
$video['key1'] = $explode_key[0];
$video['vlist'] = $explode_key[1];
} else {
if(isset($query['list'])) $video['vlist'] = $query['list'];
}
}
break;
{/code}
// 수집된 동영상 정보를 iframe source로 구현을 검색한다.
수정 전{code:php}
case 'youtu.be':
case 'youtube.com':
$vlist = $video['key2'] ? '&list='.$video['key2'] : '';
$source = '<iframe width="'.$video['width'].'" height="'.$video['height'].'" src="http://www.youtube.com/embed/'.$video['key1'].'?wmode=opaque&autohide=1'.$vlist.'" frameborder="0" allowfullscreen></iframe>';
break;
{/code}수정 후{code:php}
case 'youtu.be':
case 'youtube.com':
if(isset($video['vlist']) && $video['vlist']) {
$vlist = '&list='.$video['vlist']; //유튜브 재생목록 가져오기
$source = '<iframe width="'.$video['width'].'" height="'.$video['height'].'" src="https://www.youtube.com/embed/?wmode=opaque&autohide=1&showinfo=0&modestbranding=1&rel=0&enablejsapi=1&vq=hd1080'.$vlist.'" frameborder="0" allowfullscreen></iframe>';
} else {
$source = '<iframe width="'.$video['width'].'" height="'.$video['height'].'" src="https://www.youtube.com/embed/'.$video['key1'].'?wmode=opaque&autohide=1&showinfo=0&modestbranding=1&rel=0&enablejsapi=1&vq=hd1080" frameborder="0" allowfullscreen></iframe>';
}
break;
{/code}
URL로부터 동영상 이미지 경로를 찾기를 검색한다.
수정 전{code:php}
case 'youtu.be':
case 'youtube.com':
$path_name = mb_substr($video['key1'],0,11,"utf-8");
$video['img_url'] = "http://img.youtube.com/vi/{$path_name}/maxresdefault.jpg";
break;
{/code}수정 후{code:php}
case 'youtu.be':
case 'youtube.com':
$path_name = mb_substr($video['key1'],0,11,"utf-8");
if(isset($video['key1']) && $video['key1']) {
$video['img_url'] = "https://img.youtube.com/vi/{$path_name}/maxresdefault.jpg";
} else {
$url = 'https://www.googleapis.com/youtube/v3/playlists?part=snippet&id='.$video['vlist'].'&key=YouTube Data API Key';
$data = $this->curl_web_scripping($url);
$json = json_decode($data,true);
//플레이리스트 썸네일 추출 용 Json 데이타에 maxresdefault.jpg 값이 존재하면 maxresdefault.jpg를 이용해 썸네일을 생성하고 존재하지 않으면 sddefault.jpg를 이용해 생성
$maxres = $json['items'][0]['snippet']['thumbnails']['maxres']['url'];
$standard = $json['items'][0]['snippet']['thumbnails']['standard']['url'];
if(isset($maxres)) $video['img_url'] = $maxres;
else $video['img_url'] = $standard;
}
break;
{/code}
📌플레이리스트(playlist)가 아닌 단일 동영상이 올바르게 나타나지 않는 문제 해결
📌php8.x 대응 수정
📌글 작성 시에만 적용되던 유튜브 썸네일 생성 개선하기를 글 수정 시와 더불어 플레이리스트(playlist) 썸네일과 단일 동영상 간의 썸네일 교차 변경이 적용되도록 수정
📌2021년 9월 1일
- watch?v=가 포함된 공유주소에서 watch?v=제거 후 동영상 key 추출하기
- 플레이리스트(playlist) 썸네일 추출 용 Json 데이타에 maxresdefault.jpg 값이 존재하면 maxresdefault.jpg를 이용해 썸네일을 생성하고 존재하지 않으면 sddefault.jpg를 이용해 생성
끌어올림|2021.09.01 10:20|횟수 2 회
추천인 3
레벨 154
경험치 3,576,436
Progress Bar 91.21%
- 가입일 : 2015-03-10 12:21:44
- 서명 : 인간에게 가장 큰 선물은
자기 자신에게 기회를 주는 것이다.
- 크리스 가드너, Chris Gardner -
- 자기소개 : There's never a shortcut to happiness.
-
[좋은 생각] 자기를 아는 사람2026-02-02
-
[좋은 생각] 애쓰지 않고 편안하게2026-02-02
-
[좋은 생각] 천성적으로 타고나지 않았더라도 마음을 다해 내 것으로 만들 수 있는 것2026-02-02
-
[좋은 생각] 표를 세는 사람이 모든 것을 결정한다2026-02-02
-
[좋은 생각] 희망 도토리2026-02-02
-
[좋은 생각] 자유는 먹고사는 게 다 해결되고 나서야 시작됩니다2026-02-02
-
[생활 정보] [판매중] 햇반 윤기가득쌀밥 210g 24입 (1박스) (20,900원 / 무료)2026-02-02
-
[생활 정보] [판매중] 실속형 신고배 4.5kg 중대과(7-9과 내외) (10,740원 / 무료)2026-02-02
-
[생활 정보] 가격이 착하다 싶더니 할인이 사라지고 일반가 30,900원으로 판매하네요2026-01-28
-
[일일 메모장] 안타까운 소식입니다. 민주 진영의 거목이였던 이해찬 전 대표님 정말 고생 많으셨습니다. 이제 편히 쉬십시오.2026-01-25
-
[자유 게시판] 올 한 해도 잘 부탁드립니다. 새해 복 많이 받으세요.2026-01-06
-
[일일 메모장] 무슨 말이 필요할까요 삼가 조의를 표하며 고인의 명복을 빕니다 영면하세요2026-01-05
-
[생활 정보] 이번 년도도 잘 정리해 주셨네요 감사드립니다2026-01-01
-
[자유 게시판] 새해 맞기 전 마지막 날을 좀 더 멋지게 보내고 싶은데 늘 마음 뿐이네요 활기차고 신난 새해로 시작되시길 빌어요 올 한 해도 화이팅입니다2026-01-01
-
[자유 게시판] 메리 크리스마스 행복이 넘쳐 흘러 기쁨을 만끽하는 하루 그런 날 되세요2025-12-25
-
[자유 게시판] 1990년대를 살았던 우리들의 꽃피던 시절은 시끌벅쩍하고 그토록 화려했던 것 같습니다. 세상에 맞선 용기는 사라지고 조심성만 커져 버린 오늘의 삶이지만 어제보다 더 나은 오늘, 오늘 보다 더 나은 미래를 그리는 그런 삶을 그려 나가길 기원합니다. 메리 크리스마스! 행복한 성탄절되세요!!2025-12-24



댓글7
내사랑님의 댓글
축하합니다. 지뢰폭탄 제거로 11경험치를 획득하였습니다.
하늘구장님의 댓글
shadow2fox님의 댓글
하늘구장님의 댓글
내사랑님의 댓글
축하합니다. 첫 댓글로 18경험치를 획득하였습니다.
shadow2fox님의 댓글
내사랑님의 댓글