본문 바로가기

카테고리 없음

파이썬으로 유튜브 영상 다운로드하기

파이썬으로 유튜브 영상을 다운로드 하는 프로그램을 만들었습니다. 

 

 

 

유튜브 주소를 입력하면 됩니다. 

 

그리처럼 유튜브의 주소를 붙여넣고 'OK'버튼을 누르면 됩니다. 

 

그럼 잠시후에 종료가 되고 폴더가 열립니다. 

 

 

# pip install pytube

# https://hleecaster.com/python-pytube/
# https://wikidocs.net/85711
# https://wikidocs.net/21952

# 유튜브에서 주소를 입력받고,
# 영상을 mp3, mp4로 다운로드 함
# 다운로드가 완료되면 폴더가 열림

# Import the necessary libraries
import os
import re
import sys

import pyautogui as pg
import webbrowser

from pytube import YouTube

# Set the download folder
DOWNLOAD_FOLDER = r"C:\youtube_down"

# Prompt the user for the YouTube URL
url = pg.prompt(text='유튜브 주소를 입력하세요. ', title='유튜브 영상 다운로드 ', default='')

if url == None:
    sys.exit('프로그램을 종료합니다. ')

# Create a YouTube object and get the highest resolution stream
yt = YouTube(url)
stream = yt.streams.get_highest_resolution()

# Download the video
stream.download(DOWNLOAD_FOLDER)

# Get the audio-only stream in MP4 format
mp3_file = yt.streams.filter(adaptive=True, file_extension='mp4', only_audio=True)

# Clean up the video title to use as the file name
title_name = re.sub('[\/:*?"<>|_]', '', yt.title)
file_name = re.sub('[\/:*?"<>|_]', '', title_name) + '.mp3'

# Download the audio-only stream
mp3_file.order_by('abr').desc().first().download(DOWNLOAD_FOLDER, file_name)

# Display an alert when the download is complete
pg.alert(text='프로그램 종료 완료', title='유튜브 다운 완료 안내', button='OK')

# Open the download folder
path = os.path.realpath(DOWNLOAD_FOLDER)
os.startfile(path)