-
Notifications
You must be signed in to change notification settings - Fork 0
/
YTDownloader.py
80 lines (60 loc) · 2.66 KB
/
YTDownloader.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
import os
import re
from tkinter import *
from tkinter import filedialog
from moviepy.editor import *
from pytube import YouTube, Playlist
root = Tk()
root.withdraw()
print("Select the folder where you would like to download your stuff")
FOLDER = filedialog.askdirectory()
YOUTUBE_STREAM_AUDIO = '140'
choice = ""
while choice != "0":
try:
choice = input("\nReply with one of the following numbers to choose the operation to perform:"
"\n0 - Terminate the process"
"\n1 - Download a YouTube video"
"\n2 - Download a YouTube Playlist\n")
if choice == "0":
exit(0)
elif choice == "1":
video = YouTube(input("\nPlease input the YouTube video link\n"))
dlType = ""
while dlType != "1" and dlType != "2":
dlType = input(
"\nInsert:\n- '1' to download only audio\n- '2' to download the full video\n")
streams = video.streams
print("\nDownloading...")
path = streams[0].download(output_path=FOLDER)
if dlType == "1":
videoFile = VideoFileClip(path)
videoFile.audio.write_audiofile(FOLDER + "/" + video.title + ".mp3")
videoFile.close()
os.remove(path)
print("Downloaded " + video.title)
print("\nSuccessfully downloaded YouTube video: " + video.title)
elif choice == "2":
playlist = Playlist(input("\nPlease input the YouTube playlist link\n"))
playlist._video_regex = re.compile(r"\"url\":\"(/watch\?v=[\w-]*)") # Fixes empty playlist
dlType = ""
while dlType != "1" and dlType != "2":
dlType = input(
"\nInsert\n- '1' to download only audio\n- '2' to download the full video\n")
print("\nDownloading...")
for video in playlist.videos:
try:
path = video.streams.first().download(output_path=FOLDER)
if dlType == "1":
videoFile = VideoFileClip(path)
videoFile.audio.write_audiofile(FOLDER + "/" + video.title + ".mp3")
videoFile.close()
os.remove(path)
except Exception as e:
print(e)
print("A video of the playlist couldn't be downloaded")
print("Downloaded " + video.title)
print("\nSuccessfully downloaded YouTube playlist: " + playlist.title)
except Exception as e:
print(e)
print("You didn't provide a correct link")