|
| 1 | +# -*- coding: UTF-8 -*- |
| 2 | +import PySimpleGUI as sg |
| 3 | +from pytube import YouTube |
| 4 | +import webbrowser |
| 5 | +from typing import Any |
| 6 | + |
| 7 | + |
| 8 | +class VideoDownloadWindow: |
| 9 | + def __init__(self, link: str) -> None: |
| 10 | + self.__link: str = link |
| 11 | + self.video_obj: YouTube = YouTube(self.link, on_progress_callback=self.__progress_check, on_complete_callback=self.__on_complete) |
| 12 | + |
| 13 | + # -------------------- defining layouts |
| 14 | + info_tab = [ |
| 15 | + [sg.Text('Link:'), sg.Text(self.link, enable_events=True, key='-LINK-')], |
| 16 | + [sg.Text('Title:'), sg.Text(self.video_obj.title)], |
| 17 | + [sg.Text('Length:'), sg.Text(f'{round(self.video_obj.length / 60,2)} minutes')], |
| 18 | + [sg.Text('Views:'), sg.Text(self.video_obj.views)], |
| 19 | + [sg.Text('Creator:'), sg.Text(self.video_obj.author)], |
| 20 | + [sg.Text('Description:'), sg.Multiline(self.video_obj.description, size = (40, 20), no_scrollbar=True, disabled=True)] |
| 21 | + ] |
| 22 | + |
| 23 | + download_tab = [ |
| 24 | + [sg.Text('Download Folder'), sg.Input(size=(27, 1), enable_events=True, key='-FOLDER-'), sg.FolderBrowse()], |
| 25 | + [sg.Frame('Best Quality', [[sg.Button('Download', key='-BEST-'), sg.Text(self.video_obj.streams.get_highest_resolution().resolution), sg.Text(f'{round(self.video_obj.streams.get_highest_resolution().filesize / 1048576,1)} MB')]])], |
| 26 | + [sg.Frame('Worst Quality', [[sg.Button('Download', key='-WORST-'), sg.Text(self.video_obj.streams.get_lowest_resolution().resolution), sg.Text(f'{round(self.video_obj.streams.get_lowest_resolution().filesize / 1048576,1)} MB')]])], |
| 27 | + [sg.Frame('Audio', [[sg.Button('Download', key='-AUDIO-'), sg.Text(f'{round(self.video_obj.streams.get_audio_only().filesize / 1048576,1)} MB')]])], |
| 28 | + [sg.VPush()], |
| 29 | + [sg.Text('', key='-COMPLETED-', size=(40, 1), justification='c', font='underline')], |
| 30 | + [sg.Progress(100, orientation='h', size=(20, 20), key='-DOWNLOADPROGRESS-', expand_x=True, bar_color='Black')] |
| 31 | + ] |
| 32 | + |
| 33 | + self.main_layout = [ |
| 34 | + [sg.TabGroup([ |
| 35 | + [sg.Tab('info', info_tab), sg.Tab('download', download_tab)] |
| 36 | + ]) |
| 37 | + ] |
| 38 | + ] |
| 39 | + |
| 40 | + # -------------------- defining windows and popups |
| 41 | + self.download_window: sg.Window = sg.Window('Youtube Downloader', self.main_layout, modal=True) |
| 42 | + self.download_dir_popup = lambda: sg.Popup('Please select a download directory', title='Info') |
| 43 | + |
| 44 | + |
| 45 | + def create(self) -> None: |
| 46 | + # -------------------- download event loop |
| 47 | + while True: |
| 48 | + self.event, self.values = self.download_window.read() |
| 49 | + if self.event == sg.WIN_CLOSED: |
| 50 | + break |
| 51 | + |
| 52 | + if self.event == '-LINK-': |
| 53 | + webbrowser.open(self.link) |
| 54 | + |
| 55 | + if self.event == '-BEST-': |
| 56 | + self.download(self.video_obj.streams.get_highest_resolution().download) |
| 57 | + |
| 58 | + if self.event == '-WORST-': |
| 59 | + self.download(self.video_obj.streams.get_lowest_resolution().download) |
| 60 | + |
| 61 | + if self.event == '-AUDIO-': |
| 62 | + self.download(self.video_obj.streams.get_audio_only().download) |
| 63 | + self.download_window.close() |
| 64 | + |
| 65 | + |
| 66 | + def download(self, option: Any) -> None: |
| 67 | + if not self.values['-FOLDER-']: |
| 68 | + self.download_dir_popup() |
| 69 | + else: |
| 70 | + option(self.values['-FOLDER-']) |
| 71 | + |
| 72 | + @property |
| 73 | + def link(self) -> str: |
| 74 | + return self.__link |
| 75 | + |
| 76 | + |
| 77 | + def __progress_check(self, stream: Any, chunk: Any, bytes_remaining: Any) -> None: |
| 78 | + self.download_window['-DOWNLOADPROGRESS-'].update(100 - round(bytes_remaining / stream.filesize * 100)) |
| 79 | + self.download_window['-COMPLETED-'].update(f'{100 - round(bytes_remaining / stream.filesize * 100)}% completed') |
| 80 | + |
| 81 | + |
| 82 | + def __on_complete(self, stream: Any, file_path: Any) -> None: |
| 83 | + self.download_window['-DOWNLOADPROGRESS-'].update(0) |
| 84 | + self.download_window['-COMPLETED-'].update('') |
| 85 | + sg.Popup('Download completed') |
| 86 | + |
| 87 | + |
| 88 | + |
0 commit comments