-
Notifications
You must be signed in to change notification settings - Fork 2
/
app.py
157 lines (129 loc) · 4.89 KB
/
app.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
import csv
import os
import shlex
import shutil
import subprocess
import sys
import time
from pathlib import Path
import PySimpleGUI as sg
from pytube import YouTube
def main():
links = []
titles = []
action = ""
# funstion to download videos from youtube
def Download(link, title):
# create a videos folder
Path('Videos/').mkdir(parents=True, exist_ok=True)
# create a temp folder
Path('temp/').mkdir(parents=True, exist_ok=True)
# check if the string exist in the link
if "https://www.youtube.com/watch?v=" in link:
print(
f"Downloading Started for Video with title {title.strip()} \n")
# Ceate Youtube Object
youtubeObject = YouTube(link)
try:
# Youtube maintains Separate video and audio files for high resolution videos
# download Video in a temp location
youtubeObject.streams.get_highest_resolution().download(
output_path="temp/", filename='video.mp4')
# Download Audio in a temp location
youtubeObject.streams.filter(abr="160kbps", progressive=False).first(
).download(output_path="temp/", filename='audio.mp3')
except Exception as e:
print(e)
print("An error has occurred")
sys.exit(1)
# Access the Audio and video file location
audio = "./temp/audio.mp3"
video = "./temp/video.mp4"
# Define the final merged video Location
filepath = f"Videos/{title.strip().replace(' ','_')}.mp4"
# Run FFmpeg in cmd to the audio and video format. this method is used as it is the fastest way to merge the files.
cmd = f'ffmpeg -y -i {audio} -r 30 -i {video} -filter:a aresample=async=1 -c:a flac -c:v copy {filepath} -loglevel quiet -stats'
subprocess.check_call(shlex.split(cmd))
# deleting the temp folder
shutil.rmtree("temp/")
else:
print("Invalid URL")
sg.theme('DarkAmber')
file_list_column = [
[
sg.Text("CSV Folder"),
sg.In(size=(25, 1), enable_events=True, key="-FOLDER-"),
sg.FolderBrowse(),
],
[
sg.Text(action, key="-action-")
],
[
sg.Listbox(
values=[], enable_events=True, size=(100, 25), key="-FILE LIST-"
)
],
[
sg.Text(" ", key="-download-")
],
[
sg.Button('Exit'),
sg.Button('Download')
],
]
layout = [
[
sg.Column(file_list_column)
]
]
window = sg.Window("Youtube Video Downloader", layout, size=(600, 600), icon='icon.ico')
while True:
event, values = window.read()
if event == "Exit" or event == sg.WIN_CLOSED:
break
# Folder name was filled in, make a list of files in the folder
if event == "-FOLDER-":
window.refresh()
window["-action-"].update("CSV Files")
folder = values["-FOLDER-"]
try:
# Get list of files in folder
file_list = os.listdir(folder)
except:
file_list = []
print(file_list)
fnames = [
f
for f in file_list
if os.path.isfile(os.path.join(folder, f))
and f.lower().endswith((".csv"))
]
window["-FILE LIST-"].update(fnames)
for i in fnames:
n = f"{folder}/{i}"
# opening the CSV file
with open(n, mode='r')as file:
# reading the CSV file
csvFile = csv.DictReader(file)
# displaying the contents of the CSV file
for lines in csvFile:
titles.append(f'{i.split(".")[0]}_{lines["Title"]}')
links.append(lines["Video Link"])
file.close()
if event == 'Download':
window["-action-"].update("Downloading Videos")
window["-FILE LIST-"].update(titles)
try:
for j in range(len(links)):
for i in range(len(links)):
window.refresh()
window["-download-"].update(
f"Downloading Started for Video with title {titles[j].strip()}")
Download(links[j], titles[j])
except Exception as e:
print(e)
print("An error has occurred")
sys.exit(1)
window.close()
if __name__ == '__main__':
main()