forked from glubsy/livestream_saver
-
Notifications
You must be signed in to change notification settings - Fork 0
/
stream_downloader.py
69 lines (58 loc) · 2.46 KB
/
stream_downloader.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
#!/bin/env python3
from os import sep
import logging
import argparse
import livestream_saver.download
from livestream_saver.merge import merge
from livestream_saver.util import YoutubeUrllibSession
logger = logging.getLogger("livestream_saver")
logger.setLevel(logging.DEBUG)
def parse_args():
parser = argparse.ArgumentParser()
parser.add_argument('url', type=str, help='Youtube URL to download.')
parser.add_argument('-c', '--cookie', action='store',
default=None, type=str,
help='Path to Netscape formatted cookie file.')
parser.add_argument('-q', '--max_video_quality', action='store',
default=None, type=int,
help='Use best available video resolution up to this height in pixels.')
parser.add_argument('-o', '--output_dir', action='store',
default="./", type=str,
help='Output directory where to write downloaded chunks.')
parser.add_argument('-d', '--delete_source', action='store_true',
help='Delete source files once final merge has been done.')
levels = ('DEBUG', 'INFO', 'WARNING', 'ERROR', 'CRITICAL')
parser.add_argument('--log', action='store', default="INFO", choices=levels,
help='Log level.')
parser.add_argument('--scan_delay', action='store',
default=120.0, type=float,
help='Interval in seconds to scan for status update (default 120.0).')
args = parser.parse_args()
return args
if __name__ == "__main__":
args = parse_args()
# FIXME Not a very useful logger, might delete later.
logfile = logging.FileHandler(\
filename=args.output_dir + sep + "downloader.log", delay=True)
logfile.setLevel(logging.DEBUG)
formatter = logging.Formatter(\
'%(asctime)s - %(levelname)s - %(name)s - %(message)s')
logfile.setFormatter(formatter)
logger.addHandler(logfile)
conhandler = logging.StreamHandler()
conhandler.setLevel(args.log)
conhandler.setFormatter(formatter)
logger.addHandler(conhandler)
session = YoutubeUrllibSession(args.cookie)
dl = livestream_saver.download.YoutubeLiveStream(
url=args.url,\
output_dir=args.output_dir,\
session=session,\
max_video_quality=args.max_video_quality,\
log_level=args.log
)
dl.download(args.scan_delay)
# TODO make sure number of segment match the last numbered segment
# Merge segments into one file
if dl.done:
merge(info=dl.video_info, data_dir=dl.output_dir, delete_source=args.delete_source)