diff options
| author | 2015-10-10 22:06:02 +0200 | |
|---|---|---|
| committer | 2015-10-10 22:06:02 +0200 | |
| commit | 2f2bd135d3dfd34cfef327b2d35012cfa2062701 (patch) | |
| tree | be1343e8a6e6a9246d584b8e6e55687dfca7c8d6 /youtube/downloader.py | |
| parent | 7778f0a6a01d5b8c9a9cd1547623bc0ba46eaff5 (diff) | |
| download | youtube-podcaster-2f2bd135d3dfd34cfef327b2d35012cfa2062701.tar.gz youtube-podcaster-2f2bd135d3dfd34cfef327b2d35012cfa2062701.tar.bz2 youtube-podcaster-2f2bd135d3dfd34cfef327b2d35012cfa2062701.zip | |
Initial code import
Diffstat (limited to 'youtube/downloader.py')
| -rw-r--r-- | youtube/downloader.py | 42 |
1 files changed, 42 insertions, 0 deletions
diff --git a/youtube/downloader.py b/youtube/downloader.py new file mode 100644 index 0000000..ca1327b --- /dev/null +++ b/youtube/downloader.py | |||
| @@ -0,0 +1,42 @@ | |||
| 1 | #!/usr/bin/env python3 | ||
| 2 | |||
| 3 | import youtube_dl | ||
| 4 | import os | ||
| 5 | |||
| 6 | |||
| 7 | class Downloader: | ||
| 8 | instance = None | ||
| 9 | |||
| 10 | def get_instance(file_format, location, base_url): | ||
| 11 | if Downloader.instance: | ||
| 12 | return Downloader.instance | ||
| 13 | else: | ||
| 14 | Downloader.instance = Downloader(file_format, location, base_url) | ||
| 15 | return Downloader.instance | ||
| 16 | |||
| 17 | def __init__(self, file_format, location, base_url): | ||
| 18 | self.file_format = file_format | ||
| 19 | self.location = location | ||
| 20 | self.base_url = base_url | ||
| 21 | |||
| 22 | def download(self, video, video_id, feed_id): | ||
| 23 | output = "%s/%s/%s.ogg" % (self.location, feed_id, video_id) | ||
| 24 | options = {"format": "bestaudio/best", | ||
| 25 | "outtmpl": output, | ||
| 26 | "postprocessors": [{ | ||
| 27 | "key": "FFmpegExtractAudio", | ||
| 28 | "preferredcodec": self.file_format | ||
| 29 | }], | ||
| 30 | "nooverwrites": True | ||
| 31 | } | ||
| 32 | |||
| 33 | video_url = "https://www.youtube.com/watch?v=%s" % (video["snippet"]["resourceId"]["videoId"]) | ||
| 34 | youtube_dl.YoutubeDL(options).download([video_url]) | ||
| 35 | |||
| 36 | url = "%s/%s/%s.ogg" % (self.base_url, feed_id, video_id) | ||
| 37 | size = str(os.path.getsize(output)) | ||
| 38 | mime = "audio/ogg" | ||
| 39 | |||
| 40 | return (url, size, mime) | ||
| 41 | |||
| 42 | # vim: set ts=8 sw=4 tw=0 et : | ||
