ClearCode

YouTube

Apr 1st, 2022 (edited)
1,981
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.77 KB | None | 0 0
  1. import PySimpleGUI as sg
  2. from pytube import YouTube
  3.  
  4. def progress_check(stream, chunk, bytes_remaining):
  5.     window['-DOWNLOADPROGRESS-'].update(100 - round(bytes_remaining / stream.filesize * 100))
  6.  
  7. def on_complete(stream, file_path):
  8.     window['-DOWNLOADPROGRESS-'].update(0)
  9.  
  10. sg.theme('Darkred1')
  11. start_layout = [
  12.     [sg.Input(key = '-INPUT-'),sg.Button('submit')],
  13. ]
  14.  
  15. info_tab = [
  16.     [sg.Text('Title:'),sg.Text('',key = '-TITLE-')],
  17.     [sg.Text('Length:'),sg.Text('', key = '-LENGTH-')],
  18.     [sg.Text('Views:'),sg.Text('', key = '-VIEWS-')],
  19.     [sg.Text('Author:'),sg.Text('', key = '-AUTHOR-')],
  20.     [sg.Text('Description:'),sg.Multiline('', key = '-DESCRIPTION-',size = (40,20),no_scrollbar = True, disabled = True)]
  21. ]
  22.  
  23. download_tab = [
  24.     [sg.Frame('Best Quality',[[sg.Button('Download', key = '-BEST-'),sg.Text('',key = '-BESTRES-'),sg.Text('',key = '-BESTSIZE-')]])],
  25.     [sg.Frame('Worst Quality',[[sg.Button('Download', key = '-WORST-'),sg.Text('',key = '-WORSTRES-'),sg.Text('',key = '-WORSTSIZE-')]])],
  26.     [sg.Frame('Audio',[[sg.Button('Download', key = '-AUDIO-'),sg.Text('',key = '-AUDIOSIZE-')]])],
  27.     [sg.VPush()],
  28.     [sg.Progress(100,orientation='h', size=(20, 20), key='-DOWNLOADPROGRESS-', expand_x = True)]
  29. ]
  30.  
  31. main_layout = [
  32.     [sg.TabGroup([
  33.         [sg.Tab('info',info_tab),sg.Tab('download',download_tab)]])]
  34.     ]
  35.  
  36. window = sg.Window('Youtube Downloader', start_layout)
  37.  
  38.  
  39. while True:
  40.     event, values = window.read()
  41.     if event == sg.WIN_CLOSED:
  42.         break
  43.     if event == 'submit':
  44.         video_object = YouTube(values['-INPUT-'], on_progress_callback = progress_check, on_complete_callback = on_complete)
  45.         window.close()
  46.        
  47.         # main window info setup
  48.         window = sg.Window('Converter',main_layout,finalize = True)
  49.         window['-TITLE-'].update(video_object.title)
  50.         window['-LENGTH-'].update(f'{round(video_object.length / 60,2)} minutes')
  51.         window['-VIEWS-'].update(video_object.views)
  52.         window['-AUTHOR-'].update(video_object.author)
  53.         window['-DESCRIPTION-'].update(video_object.description)
  54.  
  55.         # main window download setup
  56.         window['-BESTSIZE-'].update(f'{round(video_object.streams.get_highest_resolution().filesize / 1048576,1)} MB')
  57.         window['-BESTRES-'].update(video_object.streams.get_highest_resolution().resolution)       
  58.  
  59.         window['-WORSTSIZE-'].update(f'{round(video_object.streams.get_lowest_resolution().filesize / 1048576,1)} MB')
  60.         window['-WORSTRES-'].update(video_object.streams.get_lowest_resolution().resolution)
  61.  
  62.         window['-AUDIOSIZE-'].update(f'{round(video_object.streams.get_audio_only().filesize / 1048576,1)} MB')
  63.  
  64.     if event == '-BEST-':
  65.         video_object.streams.get_highest_resolution().download()
  66.  
  67.     if event == '-WORST-':
  68.         video_object.streams.get_lowest_resolution().download()
  69.  
  70.     if event == '-AUDIO-':
  71.         video_object.streams.get_audio_only().download()
  72.  
  73. window.close()
Add Comment
Please, Sign In to add comment