-
Notifications
You must be signed in to change notification settings - Fork 11
/
dlfe.py
184 lines (163 loc) · 6.12 KB
/
dlfe.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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
#!/usr/bin/env python3
"""DownLoad FrontEnd
A GUI for yt-dlp that allows to easily select options.
"""
from types import SimpleNamespace
import os
import sys
import subprocess as sp
import tkinter as tk
from tkinter import ttk
from tkinter.font import nametofont
__version__ = "2024.01.07"
# Namespace for widgets that need to be accessed by callbacks.
w = SimpleNamespace()
# State that needs to be accessed by callbacks.
state = SimpleNamespace()
def create_widgets(root, w):
"""Create the window and its widgets.
Arguments:
root: the root window.
w: SimpleNamespace where widgets can be added to.
"""
# Set the font.
default_font = nametofont("TkDefaultFont")
default_font.configure(size=12)
root.option_add("*Font", default_font)
# General commands and bindings
root.wm_title("DownLoad FrontEnd v" + __version__)
root.bind("q", do_exit)
# Row
ttk.Label(root, text="URL:").grid(row=0, column=0, sticky="w")
state.url = tk.StringVar()
ttk.Entry(root, textvariable=state.url, width=40).grid(row=0, column=1, sticky="ew")
# Row
ttk.Label(root, text="Output name:").grid(row=1, column=0, sticky="w")
state.outname = tk.StringVar()
ttk.Entry(root, textvariable=state.outname).grid(row=1, column=1, sticky="ew")
# Row
ttk.Label(root, text="Output directory:").grid(row=2, column=0, sticky="w")
state.outdir = tk.StringVar(value="~/tmp")
ttk.Entry(root, textvariable=state.outdir).grid(row=2, column=1, sticky="ew")
# Row
ttk.Label(root, text="Resolution (vertical):").grid(row=3, column=0, sticky="w")
state.resolution = tk.StringVar(value="720")
w.resolution = ttk.Combobox(root, textvariable=state.resolution)
w.resolution.grid(row=3, column=1, sticky="ew")
w.resolution["values"] = ("720", "1080")
# Row
ttk.Label(root, text="Chapters:").grid(row=4, column=0, sticky="w")
state.chapters = tk.BooleanVar(value=False)
ttk.Checkbutton(root, variable=state.chapters).grid(row=4, column=1, sticky="w")
# Row
ttk.Label(root, text="Subtitles:").grid(row=5, column=0, sticky="w")
state.subs = tk.BooleanVar(value=False)
subs = ttk.Checkbutton(root, command=do_subs_changed, variable=state.subs)
subs.grid(row=5, column=1, sticky="w")
# Row
ttk.Label(root, text="Subtitle language:").grid(row=6, column=0, sticky="w")
state.sublang = tk.StringVar(value="en")
w.sublang = ttk.Entry(root, textvariable=state.sublang, state="disabled")
w.sublang.grid(row=6, column=1, sticky="ew")
# Row
w.run = ttk.Button(root, text="Run", command=do_run)
w.run.grid(row=7, column=0, sticky="w")
w.cancel = ttk.Button(root, text="Cancel", command=do_cancel, state="disabled")
w.cancel.grid(row=7, column=1, sticky="w")
# Rows
ttk.Label(root, text="Downloader output:").grid(row=8, column=0, sticky="w")
w.text = tk.Text(root, width=40, height=10)
w.text.grid(row=9, column=0, columnspan=2, sticky="ew")
# Row
ttk.Button(root, text="Quit", command=do_exit).grid(row=10, column=0, sticky="w")
# Callbacks
def do_subs_changed():
"""Enable or disable subtitle language entry box depending on the value
associated withthe checkbutton for subtitles."""
if state.subs.get() is True:
w.sublang["state"] = "enabled"
else:
w.sublang["state"] = "disabled"
def do_run():
"""Start the download process."""
w.text.delete("1.0", "end")
res = state.resolution.get()
args = ["yt-dlp", "--newline", "-S", f"res:{res}"]
if state.chapters.get() is True:
args += ["--embed-chapters"]
if state.subs.get() is True:
sublang = state.sublang.get()
args += ["--sub-langs", f"{sublang}", "--write-subs", "--embed-subs"]
url = state.url.get()
if not url:
w.text.insert("1.0", "No URL supplied.")
return
outdir = state.outdir.get()
if outdir.startswith("~"):
outdir = os.environ["HOME"] + outdir[1:]
outname = state.outname.get()
tt = {ord(j): "_" for j in ",.;:'|"}
if outname:
outname = outname.replace(" - ", "-")
outname = outname.translate(tt)
outname = outname.replace("___", "_")
outname = outname.replace("__", "_")
args += ["-o", outname]
args.append(url)
w.text.insert("1.0", "Starting download...\n")
try:
state.process = sp.Popen(args, stdout=sp.PIPE, stderr=sp.DEVNULL, cwd=outdir)
w.text.insert("end", "Download started...\n")
w.run["state"] = "disabled"
w.cancel["state"] = "enabled"
# Submit callback to monitor the progress of the download.
root.after(20, monitor)
except OSError as e:
w.text.insert("1.0", f"Error when starting download: “{e}”.")
state.process = None
def monitor():
"""Callback to monitor the progress of the yt-dlp run."""
if state.process is None:
return
rc = state.process.poll()
if rc is None:
# Process is still running
text = state.process.stdout.read()
w.text.insert("end", text)
w.text.see("end")
# Re-submit callback
root.after(20, monitor)
return
elif rc == 0:
# Process finished normally
w.text.insert("end", "Download finished normally")
else:
w.text.insert(
"end", f"Download process returned error code {state.process.returncode}."
)
w.run["state"] = "enabled"
w.cancel["state"] = "disabled"
def do_cancel():
state.process.terminate()
state.process = None
w.run["state"] = "enabled"
w.cancel["state"] = "disabled"
def do_exit(arg=None):
root.destroy()
# Helper functions
# Program starts here.
if __name__ == "__main__":
# Detach from the command line on UNIX systems.
if os.name == "posix":
if os.fork():
sys.exit()
# Initialize global state
state.red, state.green, state.blue = 0, 0, 0
# Create the GUI.
root = tk.Tk(None)
if os.name == "posix":
# Make a floating window even if using a tiling window manager.
# This “-type” is unknown on ms-windows.
root.attributes("-type", "dialog")
create_widgets(root, w)
root.mainloop()