aboutsummaryrefslogtreecommitdiffstats
path: root/youtube_podcaster/youtube/downloader.py
diff options
context:
space:
mode:
Diffstat (limited to 'youtube_podcaster/youtube/downloader.py')
-rw-r--r--youtube_podcaster/youtube/downloader.py40
1 files changed, 33 insertions, 7 deletions
diff --git a/youtube_podcaster/youtube/downloader.py b/youtube_podcaster/youtube/downloader.py
index 529902a..24fa1b0 100644
--- a/youtube_podcaster/youtube/downloader.py
+++ b/youtube_podcaster/youtube/downloader.py
@@ -1,6 +1,7 @@
1#!/usr/bin/env python3 1#!/usr/bin/env python3
2 2
3import os 3import os
4import sys
4import mimetypes 5import mimetypes
5 6
6import youtube_dl 7import youtube_dl
@@ -19,26 +20,51 @@ class Downloader:
19 self.location = location 20 self.location = location
20 self.base_url = base_url 21 self.base_url = base_url
21 22
23 self.downloaded = []
24
22 if file_format == "vorbis": 25 if file_format == "vorbis":
23 self.extension = "ogg" 26 self.extension = "ogg"
27 elif file_format == "opus":
28 self.extension = "opus"
29
30 if sys.platform == "linux" and not hasattr(sys, "real_prefix"):
31 self.tmp_dir = "/tmp/youtube-podcaster"
32 else:
33 self.tmp_dir= "%s/tmp/youtube-podcaster" % (sys.prefix)
34
35 os.makedirs(self.tmp_dir, 0o755, True)
24 36
25 def download(self, video, video_id, feed_id): 37 def download(self, video, video_id, feed_id):
26 output = "%s/%s/%s.%s" % (self.location, feed_id, video_id, self.extension) 38
39 # Real output
40 filename = "%s.%s" % (video_id, self.extension)
41 output_dir = "%s/%s" % (self.location, feed_id)
42 output = "%s/%s" % (output_dir, filename)
43
44 # Tmp output
45 tmp_filename = "%s.webm" % (video_id)
46 tmp_output = "%s/%s" % (self.tmp_dir, tmp_filename)
47
27 options = {"format": "bestaudio/best", 48 options = {"format": "bestaudio/best",
28 "outtmpl": output, 49 "outtmpl": tmp_output,
29 "postprocessors": [{ 50 "postprocessors": [{
30 "key": "FFmpegExtractAudio", 51 "key": "FFmpegExtractAudio",
31 "preferredcodec": self.file_format 52 "preferredcodec": self.file_format
32 }], 53 }]}
33 "nooverwrites": True}
34
35 54
36 video_url = "https://www.youtube.com/watch?v=%s" % (video["snippet"]["resourceId"]["videoId"]) 55 video_url = "https://www.youtube.com/watch?v=%s" % (video["snippet"]["resourceId"]["videoId"])
37 youtube_dl.YoutubeDL(options).download([video_url]) 56 youtube_dl.YoutubeDL(options).download([video_url])
38 57
58 tmp_output = "%s/%s" % (self.tmp_dir, filename)
59
39 url = "%s/%s/%s.%s" % (self.base_url, feed_id, video_id, self.extension) 60 url = "%s/%s/%s.%s" % (self.base_url, feed_id, video_id, self.extension)
40 size = str(os.path.getsize(output)) 61 size = str(os.path.getsize(tmp_output))
41 mime = mimetypes.guess_type(output)[0] 62 mime = mimetypes.guess_type(tmp_output)[0]
63
64 os.makedirs(output_dir, 0o755, True)
65 os.rename(tmp_output, output)
66
67 self.downloaded.append(output)
42 68
43 return (url, size, mime) 69 return (url, size, mime)
44 70