-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdl.py
More file actions
86 lines (65 loc) · 2.09 KB
/
Copy pathdl.py
File metadata and controls
86 lines (65 loc) · 2.09 KB
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
import os
import tempfile
import shutil
import traceback
from threading import Thread
from yt_dlp import YoutubeDL
from flask import Flask, request
from qtfaststart import processor, exceptions
app = Flask(__name__)
VID_PATH = b"/mnt/Data/Public/Videos/Other/Youtubes/vids/"
TMP_PATH = b"/mnt/Data/Public/Videos/Other/Youtubes/downloader/temp"
log_file = None
active_urls = []
def log(txt):
global log_file
log_file.write(txt)
log_file.flush()
def download(url):
global ydl
global active_urls
if url in active_urls:
print("Double download attempt. Blocking.")
return
active_urls.append(url)
print(url)
try:
info_dict = ydl.extract_info(url, download=False)
filename = ydl.prepare_filename(info_dict)
filename = filename.encode('UTF-8')
if os.path.isfile(filename):
print("File already downloaded")
return ""
ydl.download([url])
tmp, outfile = tempfile.mkstemp(dir=TMP_PATH)
os.close(tmp)
try:
processor.process(VID_PATH + filename, outfile)
shutil.move(outfile, VID_PATH + filename)
except exceptions.FastStartException:
# stupid library throws exception if file is already setup properly
print("Ignoring moov failure for already setup file")
log("Downloaded " + url + '\n')
except Exception as e:
log("FAILED " + url + '. Reason: ' + str(e) + '\n')
log(traceback.format_exc())
active_urls.remove(url)
@app.route("/dlvid", methods=["GET"])
def dlvid():
url = request.args.get('url')
if "results" in url and "search_query" in url:
return ""
if "/channel/" in url or "/user/" in url or "/playlist" in url or "/c/" in url:
return ""
Thread(target=download, args=(url,)).start()
return ""
def init():
global ydl, log_file
ydl_opts = { 'continuedl': True, 'outtmpl': {'default': '%(title)s-%(id)s.%(ext)s'}, 'format': 'bestvideo[ext=mp4]+bestaudio[ext=m4a]', 'noplaylist': True}
ydl = YoutubeDL(ydl_opts)
ydl.add_default_info_extractors()
log_file = open("log.txt", "a")
os.chdir(VID_PATH)
if __name__ == "__main__":
init()
app.run(host="0.0.0.0", port=8080, threaded=True)