Skip to content

Instantly share code, notes, and snippets.

@partrita
Last active January 6, 2026 07:01
Show Gist options
  • Select an option

  • Save partrita/289b42b5c6e9d36e384e3729907dfb34 to your computer and use it in GitHub Desktop.

Select an option

Save partrita/289b42b5c6e9d36e384e3729907dfb34 to your computer and use it in GitHub Desktop.
Collect YouTube video URLs to add them as input for NotebookLM.
# /// script
# requires-python = ">=3.12"
# dependencies = [
# "click>=8.3.1",
# "scrapetube>=2.6.0",
# ]
# ///
import scrapetube
import click
import os
import itertools
from urllib.parse import urlparse, parse_qs
@click.command()
@click.option(
"--target",
prompt="대상 입력 (채널 핸들 '@User' 또는 재생목록 URL)",
help="YouTube 채널 핸들, ID, 또는 재생목록 URL을 입력하세요.",
)
@click.option(
"--filename",
prompt="저장할 파일 이름",
default="youtube_urls.txt",
help="결과를 저장할 파일 이름입니다.",
)
@click.option(
"--path",
prompt="저장할 경로",
default=".",
help="파일을 저장할 폴더 경로를 입력하세요.",
)
@click.option(
"--limit",
prompt="최대 수집 한도",
default=300,
type=int,
help="수집할 최대 영상 개수입니다.",
)
def get_video_urls(target, filename, path, limit):
"""YouTube 채널 또는 재생목록에서 비디오 URL을 수집하여 파일로 저장합니다."""
print(f"데이터를 분석하는 중: {target} (최대 {limit}개)...")
try:
videos = None
# 입력값에 'list='가 포함된 경우 재생목록으로 처리합니다.
if "list=" in target and "youtube.com" in target:
query = urlparse(target).query
playlist_id = parse_qs(query).get("list", [None])[0]
if playlist_id:
print(f"재생목록 ID 감지: {playlist_id}")
videos = scrapetube.get_playlist(playlist_id=playlist_id)
else:
print("유효하지 않은 재생목록 URL입니다.")
return
# 그 외의 경우 채널로 처리합니다.
else:
if target.startswith("@"):
channel_url = f"https://www.youtube.com/{target}"
videos = scrapetube.get_channel(channel_url=channel_url)
else:
videos = scrapetube.get_channel(channel_username=target)
urls = []
# itertools.islice를 사용하여 입력받은 limit 만큼만 반복합니다.
for video in itertools.islice(videos, limit):
video_id = video["videoId"]
url = f"https://www.youtube.com/watch?v={video_id}"
urls.append(url)
if not urls:
print("비디오를 찾을 수 없거나 정보가 올바르지 않습니다.")
return
if not os.path.exists(path):
os.makedirs(path)
full_path = os.path.join(path, filename)
with open(full_path, "w", encoding="utf-8") as f:
for url in urls:
f.write(url + "\n")
print(f"성공적으로 {len(urls)}개의 URL을 {full_path}에 저장했습니다.")
except Exception as e:
print(f"오류가 발생했습니다: {e}")
if __name__ == "__main__":
get_video_urls()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment