-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathUTube2Mp3.py
126 lines (119 loc) · 4.07 KB
/
UTube2Mp3.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
#!/usr/bin/env python
import tornado.httpserver
import tornado.ioloop
import tornado.web
import os
import re
import subprocess
import settings
def update_youtube_dl():
"""Update Youtube -DL"""
try:
print "Checking for youtube-dl updates"
youtube_dl = os.path.join(settings.BASE, "youtube-dl")
args = [youtube_dl,
"-U"
]
subprocess.call(args)
except Exception as exp:
print "\n[ERROR] ", exp
def download_playlist(playlist):
"""Download YouTube Playlist into MP3"""
try:
youtube_dl = os.path.join(settings.BASE, "youtube-dl")
if len(settings.FFMPEG) > 0:
args = [youtube_dl,
"--no-post-overwrites",
"-x",
"--ffmpeg-location",
settings.FFMPEG,
"--prefer-ffmpeg",
"--audio-format",
"mp3",
"--audio-quality",
"0",
"https://www.youtube.com/playlist?list="+playlist,
"-o",
"Music/Playlist/%(playlist)s/%(playlist_index)s - %(title)s.%(ext)s"
]
else:
args = [youtube_dl,
"--no-post-overwrites",
"-x", "--prefer-ffmpeg",
"--audio-format", "mp3",
"--audio-quality",
"0",
"https://www.youtube.com/playlist?list="+playlist,
"-o",
"Music/Playlist/%(playlist)s/%(playlist_index)s - %(title)s.%(ext)s"
]
subprocess.call(args)
except Exception as exp:
print "\n[ERROR] ", exp
def download_mp3(video):
"""Download YouTube Video to MP3"""
try:
youtube_dl = os.path.join(settings.BASE, "youtube-dl")
if len(settings.FFMPEG) > 0:
args = [youtube_dl,
"--no-post-overwrites",
"-x",
"--ffmpeg-location",
settings.FFMPEG,
"--prefer-ffmpeg",
"--audio-format",
"mp3",
"--audio-quality",
"0",
"-o",
"Music/%(title)s.%(ext)s",
video
]
else:
args = [youtube_dl,
"--no-post-overwrites",
"-x",
"--prefer-ffmpeg",
"--audio-format",
"mp3",
"--audio-quality",
"0",
"-o",
"Music/%(title)s.%(ext)s",
video
]
subprocess.call(args)
except Exception as exp:
print "\n[ERROR] ", exp
class DownloadPlaylist(tornado.web.RequestHandler):
"""Download Playlist to MP3"""
def get(self, playlist_id):
"""Download"""
playlist = playlist_id if playlist_id else ''
if len(playlist) > 1 and re.match(VIDEO_REGEX, playlist):
download_playlist(playlist)
self.write("Playlist Download Completed")
else:
self.write("Invalid Request")
class DownloadFile(tornado.web.RequestHandler):
"""Download File to MP3"""
def get(self, video_id):
"""Download"""
video = video_id if video_id else ''
if len(video) > 1 and re.match(VIDEO_REGEX, video):
download_mp3(video)
self.write("MP3 Download Complete")
else:
self.write("Invalid Request")
if __name__ == "__main__":
VIDEO_REGEX = r"^([a-zA-Z0-9\_\-]+)$"
tornado.web.Application([
(r"/playlist/(?P<playlist_id>[^\/]+)", DownloadPlaylist),
(r"/video/(?P<video_id>[^\/]+)", DownloadFile),
]).listen(8080)
tornado.ioloop.PeriodicCallback(update_youtube_dl, 300000).start() #15 mins in milliseconds
tornado.ioloop.IOLoop.instance().start()
'''
http://localhost:8080/playlist/url
http://localhost:8080/video/url
'''