-
Notifications
You must be signed in to change notification settings - Fork 0
/
youtubedl-dash.py
128 lines (116 loc) · 3.45 KB
/
youtubedl-dash.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
#!/usr/bin/python3.4
# coding: utf-8
import subprocess
import os
import sys
import getopt
import re
import errno
from multiprocessing import Pool
def pretty_print(str):
"""
:param arg1: replace unwanted character by _
:type arg1: string
"""
return '_'.join(str.split())
def gather_title(link):
"""
:param arg1: url of the media to download with youtube-dl
:type arg1: str
"""
command = "youtube-dl --get-filename -o '%(title)s' {}".format(link)
title = subprocess.check_output(command.split(), shell=False)
# convert Byte to String
title = title.decode("utf-8")
# remove the Quotes
return pretty_print(title[1:-2])
def create_directory(title, path):
"""
:param arg1: title of the output folder to create
:type arg1: string
"""
# create directory with the previous name
path = "{path}{title}".format(path=path, title=title)
mkdir_p(path)
return path + "/"
def list_yt_medias(link):
"""
:param arg1: url of the media to download with youtube-dl
:type arg1: string
"""
listVideo = "youtube-dl -F {}".format(link)
output = subprocess.check_output(listVideo, shell=True)
output = output.decode("utf-8")
results = []
for row in output.split("\n"):
wordList = re.findall("(?=\S*['-]?)([a-zA0-9-Z'-]+)", row)
for i in range(len(wordList)):
if (wordList[i] == "mp4"):
results.append(wordList[i-1])
return results
def process_file(command):
"""
:param arg1: command to execute
:type arg1: string
"""
print("-> Start execute command : {}".format(command))
subprocess.check_output(command, shell=True)
def download_qualities(path, results, title, link):
"""
:param arg1: url path where the output folder is
:param arg2: differents file qualities
:param arg3: media's title
:param arg4: url of the media to download
:type arg1: string
:type arg2: string[]
:type arg3: string
:type arg4: string
"""
print("path : {}".format(path))
print("title : {}".format(title))
commands = []
for result in results:
commands.append(
"youtube-dl -f {result} --output {path}{title}_'%(format)s.%(ext)s' {link}".format(result=result, path=path, title=title, link=link)
)
p = Pool(10)
p.map(process_file, commands)
p.close()
p.join()
# for i in commands:
# print("\ncommands: {}".format(i))
def mkdir_p(path):
"""
:param arg1: url path where the output folder is
:type arg1: string
"""
if not os.path.exists(path):
try:
os.makedirs(path)
except OSError as err:
if err.errno != 17:
raise
def main(args):
"""
:param arg1: url of the media to download with youtube-dl
:type arg1: string
"""
path = ""
link = args[1]
if (len(args) > 2):
path = args[2]
path = path if (path.endswith("/")) else path + "/"
# fetch the youtube video's Name
title = gather_title(link)
print("title : {}".format(title))
# create directory
path = create_directory(title, path)
# list all the available video
results = list_yt_medias(link)
print("RESULT: ", results)
# download files
download_qualities(path, results, title, link)
if __name__ =="__main__":
# import pdb; pdb.set_trace()
print("Download every mp4 files of the youtube link: {}".format(sys.argv[1]))
main(sys.argv)