Skip to content

Instantly share code, notes, and snippets.

@manuelricci
Created December 30, 2018 15:12
Show Gist options
  • Save manuelricci/d95a1c40f2e8d721719bb9d6c4535e3e to your computer and use it in GitHub Desktop.
Save manuelricci/d95a1c40f2e8d721719bb9d6c4535e3e to your computer and use it in GitHub Desktop.
Programma per scaricare video da YouTube. Ho realizzato questo piccolo software per mio nonno che voleva i video del karaoke. Mio nonno è il TOP!
"""
Project Name: Progetto Acquarius
Description: Un semplice downloader per YouTube
"""
from pytube import YouTube
import os
import re
from tkinter import *
import tkinter.messagebox
def file_path():
home = os.path.expanduser('~')
download_path = os.path.join(home, 'Desktop')
return download_path
# ********* Window *********
window = Tk()
window.title("German Downloader")
def progress_check(stream = None, chunk = None, file_handle = None, remaining = None ):
# Calcola la percentuale del download
percent = ( 100 * ( file_size-remaining ) ) / file_size
list1.update()
list1.insert(END, "{:00.0f}% downloaded \n\n".format(percent))
list1.update()
list1.see(END)
def start(yt_url, download_type):
list1.insert(END, "Your video will be saved to: {}\n\n".format(file_path()))
# Input
# yt_url = input("Copy and paste your YouTube URL here: ")
print(yt_url)
list1.insert(END, "Accessing YouTube URL...\n\n")
list1.update()
try:
video = YouTube(yt_url, on_progress_callback = progress_check)
except:
print("ERROR. Check your:\n -connection \n -url is a YouTube url\n\nTry again.")
redo = start(yt_url, download_type)
# Get the first video type and check what type of download need to do
if download_type is 1:
video_type = video.streams.filter(progressive = True, file_extension = "mp4").first()
else:
video_type = video.streams.filter(subtype='mp4', only_audio=True).first()
# Get the title of the video
title = video.title
#Prepares the file for download
list1.insert(END,"Fetching: {}...".format(title))
global file_size
file_size = video_type.filesize
# Inizia il download
video_type.download(file_path())
list1.update()
list1.insert(END,"Pronto per scaricare un nuovo video.\n\n")
list1.update()
file_size = 0
def check_inputs(yt_url, download_type):
check_video_url = re.match('http[s]?://(?:[a-zA-Z]|[0-9]|[$-_@.&+]|[!*\(\),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+', yt_url)
print(download_type)
if check_video_url is None:
tkinter.messagebox.showerror("Errore", "L'indirizzo del video sembra non essere corretto, ricontrolla.")
return
if not download_type:
tkinter.messagebox.showerror("Errore", "Devi selezionare cosa vuoi scaricare.")
return
start(yt_url, download_type)
checkbox_value = IntVar()
input_value = StringVar()
# var = IntVar()
# s = StringVar()
# Video URL Input
l1 = Label(window, text="URL del video")
l1.grid(row = 0, column = 0, padx = 10, pady = 10)
e1 = Entry(window, textvariable = input_value, width = 50)
e1.grid(row = 0, column = 1, padx = 10, pady = 10)
# Download type Input
e2 = Radiobutton(window, text = "Video", variable = checkbox_value, value = 1)
e2.grid(row = 1, column = 0, padx = 10, pady = 10)
e3 = Radiobutton(window, text = "Musica", variable = checkbox_value, value = 2)
e3.grid(row= 1, column = 1, padx = 10, pady = 10)
# Console
list1 = Text(window, width = 50, height = 10)
list1.grid(row = 2, column = 0, columnspan = 2, rowspan = 1, padx = 10, pady = 10)
# Send Button
b1 = Button(window, text="Scarica", width = 50, padx = 10, pady = 5, borderwidth = 0, background = "#1de9b6", command = lambda: check_inputs(input_value.get(), checkbox_value.get()))
b1.grid(row = 4, column = 0, columnspan = 2, padx = 10, pady = 10)
window.mainloop()
# If user want mp4
# yt.streams.first().download()
# If user want mp3
# yt.streams.filter(subtype='mp4', only_audio=True).first().download()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment