forked from ravinet/mahimahi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
youtube_download.py
136 lines (126 loc) · 4.98 KB
/
youtube_download.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
#!/usr/bin/python
# This script downloads video from youtube for the youtube emulation server
# It is meant to be run by youtube_config.py, not by the user.
# USAGE: python youtube_download.py 'youtube_url'
import sys
import subprocess
import re
import os
import shutil
def get_yes_or_no(message):
while(True):
response = raw_input(message)
if(response == "y" or response == "Y"):
return True
if(response == "n" or response == "N"):
return False
print "please response with y or n "
def delete_filesys_subtree(root_directory):
for the_file in os.listdir(root_directory):
file_path = os.path.join(root_directory, the_file)
try:
if os.path.isfile(file_path):
os.unlink(file_path)
elif os.path.isdir(file_path): shutil.rmtree(file_path)
except Exception, e:
print e
def get_media_resolution(line):
match_object = re.search("DASH video", line)
if(match_object):
match_object = re.search("([0-9]+x[0-9]+)", line)
if(match_object):
return match_object.group(1)
#match_object = re.search("DASH audio", line)
#if(match_object):
# match_object = re.search("DASH audio\t[0-9]+k , (?:m4a_dash container, )?(.+)", line)
# if(match_object):
# return match_object.group(1)
return ""
def main():
print
youtube_url = sys.argv[1]
match_object = re.search("/embed/([_a-zA-Z0-9\-]+)", youtube_url)
video_id = ""
if not match_object:
print "ERROR: " + youtube_url + " is not a valid embed youtube url"
will_provide_video_id = get_yes_or_no("Would you like to provide a video id for your custom url? y/n\n")
if will_provide_video_id:
video_id = raw_input("Please provide the video id in the terminal...\n")
youtube_url = "https://www.youtube.com/embed/" + video_id
else:
print "Without a video id we cannot download your video from YouTube. Either use a correctly formatted YouTube embed url or provide a video id for your custom url."
sys.exit(1)
else:
video_id = match_object.group(1)
print "Running youtube-dl on video id " + video_id + "......"
print
print "Available formats are as follows: "
formats_command = "youtube-dl -F " + youtube_url
print "Running command " + formats_command + "......"
proc = subprocess.Popen(formats_command, stdout=subprocess.PIPE, shell=True)
(out, err) = proc.communicate()
print out
if proc.returncode:
print "youtube-dl seems to have failed. Error messages have been posted above. Please fix the errors and run the config script again."
sys.exit(1)
print "Grabbing all mp4, webm, and m4a DASH files for video id " + video_id + "......"
print
newpath = os.path.dirname(os.path.realpath(__file__)) + "/media_files/" + video_id
print "Storing media files in " + newpath + "......"
print
if os.path.exists(newpath):
delete_filesys_subtree(newpath)
os.makedirs(newpath + "/audio")
os.makedirs(newpath + "/video")
os.makedirs(newpath + "/audio/mp4")
os.makedirs(newpath + "/video/mp4")
os.makedirs(newpath + "/video/webm")
os.makedirs(newpath + "/audio/webm")
for lno, line in enumerate(out.splitlines()):
if(re.search("DASH video", line) or re.search("DASH audio", line)):
print "Downloading file specified by " + line
download_id_match_object = re.match("([0-9]+)", line)
if not download_id_match_object:
print
print "ERROR: Line " + line + " has no download identifier."
else:
download_id = download_id_match_object.group(1)
mime_prefix = ""
mime_suffix = ""
video_resolution = ""
if(re.search("audio", line)):
mime_prefix = "audio"
if(re.search("video", line)):
mime_prefix = "video"
video_resolution_match_object = re.search("([0-9]+p)", line)
if(video_resolution_match_object):
video_resolution = video_resolution_match_object.group(1)
if(re.search("webm", line)):
mime_suffix = "webm"
if(re.search("mp4", line)):
mime_suffix = "mp4"
if(re.search("m4a", line)):
mime_suffix = "mp4"
if(mime_prefix == "" or mime_suffix == ""):
print
print "ERROR: Could not parse line " + line + " for mime format."
sys.exit(1)
mime_format = mime_prefix + "/" + mime_suffix
destination_filename = newpath + "/" + mime_format + "/" + download_id
download_command = "youtube-dl --fixup never -o " + destination_filename + " -f " + download_id + " " + youtube_url
print "Running command " + download_command + "......"
proc = subprocess.Popen(download_command, stdout=subprocess.PIPE, shell=True)
(out, err) = proc.communicate()
print out
if proc.returncode:
print "youtube-dl seems to have failed. Error messages have been posted above. Please fix the errors and run the config script again."
sys.exit(1)
filename_suffix = get_media_resolution(line);
if(filename_suffix == ""):
filename_suffix = download_id
new_filename = newpath + "/" + mime_format + "/" + filename_suffix
os.rename(destination_filename, new_filename)
# Standard boilerplate to call the main() function to begin
# the program.
if __name__ == '__main__':
main()