-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathyoutubesaver.py
58 lines (47 loc) · 2.04 KB
/
youtubesaver.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
from __future__ import unicode_literals
import json as _json
import os as _os
import sys as _sys
import fire as _fire
import youtube_dl as _youtube_dl
from Solos import SOLOS_IDS_PATH
__all__ = ['YouTubeSaver']
class YouTubeSaver(object):
"""Load video from YouTube using an auditionDataset.json """
def __init__(self):
self.outtmpl = '%(id)s.%(ext)s'
self.ydl_opts = {
'format': 'bestvideo+bestaudio',
'outtmpl': self.outtmpl,
"""
'postprocessors': [{
'key': 'FFmpegExtractAudio',
'preferredcodec': 'mp3',
'preferredquality': '192',
}],
"""
'logger': None
}
def from_json(self, dataset_dir, json_path=SOLOS_IDS_PATH):
dataset = _json.load(open(json_path))
for instrument in dataset.keys():
if not _os.path.exists(_os.path.join(dataset_dir, instrument)):
_os.makedirs(_os.path.join(dataset_dir, instrument))
self.ydl_opts['outtmpl'] = _os.path.join(dataset_dir, instrument, self.outtmpl)
with _youtube_dl.YoutubeDL(self.ydl_opts) as ydl:
for i, video_id in enumerate(dataset[instrument]):
try:
ydl.download(['https://www.youtube.com/watch?v=%s' % video_id])
del dataset[video_id]
except OSError:
with open(_os.path.join(dataset_dir, 'backup.json'), 'w') as dst_file:
_json.dump(dataset, dst_file)
print('Process failed at video {0}, #{1}'.format(video_id, i))
print('Backup saved at {0}'.format(_os.path.join(dataset_dir, 'backup.json')))
ydl.download(['https://www.youtube.com/watch?v=%s' % video_id])
except KeyboardInterrupt:
_sys.exit()
if __name__ == '__main__':
_fire.Fire(YouTubeSaver)
# USAGE
# python youtubesaver.py from_json /path_to_your_dst