-
Notifications
You must be signed in to change notification settings - Fork 1
/
youtube-dlr.py
73 lines (57 loc) · 1.58 KB
/
youtube-dlr.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
# import required modules
from __future__ import unicode_literals
import youtube_dl
import os
class MyLogger(object):
def debug(self, msg):
pass
def warning(self, msg):
pass
def error(self, msg):
print(msg)
# define progress hooks
def my_hook(d):
global flag
if d['status'] == 'downloading':
if(flag == 0):
print('Source size: {0:.2f} MB' .format(d['total_bytes']/(1024*1024)))
print('Downloading: ' + d['filename'])
flag = 1
print('File Progress: {0:.2f}%' .format(d['downloaded_bytes'] / d['total_bytes']*100), end='\r')
if d['status'] == 'finished':
flag = 0
print('Done downloading, now converting ...')
print()
# read links.txt file
fname = "links.txt"
with open(fname) as f:
content = f.readlines()
content = [x.strip() for x in content]
# create download directory
directory = '\download\\'
if not os.path.exists(directory):
os.makedirs(directory)
print("{0} file(s) queued for download" .format(len(content)))
flag = 0
# setup download configurations
'''ydl_opts = {
'outtmpl': '\download-vid\%(title)s-%(id)s.%(ext)s',
'format': 'bestvideo/best',
'logger': MyLogger(),
'progress_hooks': [my_hook],
}'''
ydl_opts = {
'outtmpl': '\download\%(title)s.%(ext)s',
'format': 'bestaudio/best',
'postprocessors': [{
'key': 'FFmpegExtractAudio',
'preferredcodec': 'mp3',
'preferredquality': '320',
}],
'logger': MyLogger(),
'progress_hooks': [my_hook],
}
# begin the download now
with youtube_dl.YoutubeDL(ydl_opts) as ydl:
ydl.download(content)
print("All Done!")