본문 바로가기
수고한일

파이썬을 이용한 영상 다운로드

by UrimStory 2024. 7. 2.

Chatgtp 에게 파이썬을 이용하여 영상 다운로드 GUI 프로그램을 하나 짜 달라고 했더니 짜 주었다... ^^ 

 

VideoDownLoad.py 

import os
from tkinter import Tk, Label, Entry, Button, StringVar, filedialog, messagebox, ttk
from yt_dlp import YoutubeDL

def download_youtube_video(url, folder_path, progress_callback):
    try:
        # yt-dlp 옵션 설정
        ydl_opts = {
            'outtmpl': os.path.join(folder_path, '%(title)s.%(ext)s'),
            'format': 'best',
            'noplaylist': True,
            'progress_hooks': [progress_callback],
        }
        
        # yt-dlp로 동영상 다운로드
        with YoutubeDL(ydl_opts) as ydl:
            ydl.download([url])
        
    except Exception as e:
        progress_bar.stop()
        progress_bar['value'] = 0
        messagebox.showerror("Error", f"An error occurred: {e}")

def browse_folder():
    folder_selected = filedialog.askdirectory()
    folder_path.set(folder_selected)

def start_download():
    url = url_entry.get()
    folder = folder_path.get()
    if not url or not folder:
        messagebox.showwarning("Input Error", "Please provide both URL and folder path.")
        return
    progress_bar.start()
    download_youtube_video(url, folder, progress_hook)

def progress_hook(d):
    if d['status'] == 'downloading':
        downloaded_bytes = d.get('downloaded_bytes', 0)
        total_bytes = d.get('total_bytes', 0)
        if total_bytes > 0:
            percent = (downloaded_bytes / total_bytes) * 100
            progress_bar['value'] = percent
            root.update_idletasks()
    elif d['status'] == 'finished':
        progress_bar.stop()
        progress_bar['value'] = 100
        messagebox.showinfo("Success", "다운로드가 완료되었습니다.")
    elif d['status'] == 'error':
        progress_bar.stop()
        progress_bar['value'] = 0
        messagebox.showerror("Error", "An error occurred during download.")

# GUI 설정
root = Tk()
root.title("YouTube Video Downloader")

# URL 입력
Label(root, text="YouTube URL:").grid(row=0, column=0, padx=10, pady=10)
url_entry = Entry(root, width=50)
url_entry.grid(row=0, column=1, padx=10, pady=10)

# 폴더 선택
Label(root, text="Save Folder:").grid(row=1, column=0, padx=10, pady=10)
folder_path = StringVar()
folder_entry = Entry(root, textvariable=folder_path, width=50)
folder_entry.grid(row=1, column=1, padx=10, pady=10)
Button(root, text="Browse", command=browse_folder).grid(row=1, column=2, padx=10, pady=10)

# 다운로드 버튼
download_button = Button(root, text="Download", command=start_download)
download_button.grid(row=2, column=0, columnspan=3, pady=20)

# 프로그레스 바
progress_bar = ttk.Progressbar(root, length=400, mode='determinate')
progress_bar.grid(row=3, column=0, columnspan=3, pady=10)

# GUI 시작
root.mainloop()

 

 

제법 빠르게 된다..^^

728x90

'수고한일' 카테고리의 다른 글

Anaconda 설치  (0) 2024.06.17
슈빵 앱  (0) 2024.03.16
Flask 환경 구축  (0) 2024.02.12
Microsoft Clipchamp  (0) 2024.01.04
HashiCorp Vault  (0) 2023.08.21