add optional EmbedThumbnail postprocessor

This commit is contained in:
simon 2021-10-07 22:28:32 +07:00
parent 4d152e1e54
commit 744780f4bd
3 changed files with 40 additions and 7 deletions

View File

@ -18,7 +18,8 @@
"limit_speed": false, "limit_speed": false,
"sleep_interval": 3, "sleep_interval": 3,
"format": false, "format": false,
"add_metadata": false "add_metadata": false,
"add_thumbnail": false
}, },
"application": { "application": {
"cache_dir": "/cache", "cache_dir": "/cache",

View File

@ -470,8 +470,8 @@ class VideoDownloader:
} }
RedisArchivist().set_message("progress:download", mess_dict) RedisArchivist().set_message("progress:download", mess_dict)
def dl_single_vid(self, youtube_id): def build_obs(self):
"""download single video""" """build obs dictionary for yt-dlp"""
obs = { obs = {
"default_search": "ytsearch", "default_search": "ytsearch",
"merge_output_format": "mp4", "merge_output_format": "mp4",
@ -485,6 +485,7 @@ class VideoDownloader:
"quiet": True, "quiet": True,
"continuedl": True, "continuedl": True,
"retries": 3, "retries": 3,
"writethumbnail": False,
} }
if self.config["downloads"]["format"]: if self.config["downloads"]["format"]:
obs["format"] = self.config["downloads"]["format"] obs["format"] = self.config["downloads"]["format"]
@ -505,15 +506,29 @@ class VideoDownloader:
} }
) )
if self.config["downloads"]["add_thumbnail"]:
postprocessors.append(
{
"key": "EmbedThumbnail",
"already_have_thumbnail": True,
}
)
obs["writethumbnail"] = True
obs["postprocessors"] = postprocessors obs["postprocessors"] = postprocessors
return obs
def dl_single_vid(self, youtube_id):
"""download single video"""
dl_cache = self.config["application"]["cache_dir"] + "/download/"
obs = self.build_obs()
# check if already in cache to continue from there # check if already in cache to continue from there
cache_dir = self.config["application"]["cache_dir"] all_cached = ignore_filelist(os.listdir(dl_cache))
cached = os.listdir(cache_dir + "/download/")
all_cached = ignore_filelist(cached)
for file_name in all_cached: for file_name in all_cached:
if youtube_id in file_name: if youtube_id in file_name:
obs["outtmpl"] = cache_dir + "/download/" + file_name obs["outtmpl"] = os.path.join(dl_cache, file_name)
with youtube_dl.YoutubeDL(obs) as ydl: with youtube_dl.YoutubeDL(obs) as ydl:
try: try:
ydl.download([youtube_id]) ydl.download([youtube_id])
@ -522,6 +537,14 @@ class VideoDownloader:
sleep(10) sleep(10)
ydl.download([youtube_id]) ydl.download([youtube_id])
if obs["writethumbnail"]:
# webp files don't get cleaned up automatically
all_cached = ignore_filelist(os.listdir(dl_cache))
to_clean = [i for i in all_cached if not i.endswith(".mp4")]
for file_name in to_clean:
file_path = os.path.join(dl_cache, file_name)
os.remove(file_path)
def move_to_archive(self, vid_dict): def move_to_archive(self, vid_dict):
"""move downloaded video from cache to archive""" """move downloaded video from cache to archive"""
videos = self.config["application"]["videos"] videos = self.config["application"]["videos"]

View File

@ -88,6 +88,15 @@
<option value="1">embed metadata</option> <option value="1">embed metadata</option>
</select> </select>
</div> </div>
<div class="settings-item">
<p>Current thumbnail embed setting: <span class="settings-current">{{ config.downloads.add_thumbnail }}</span></p>
<i>Embed thumbnail into the mediafile.</i><br>
<select name="downloads.add_thumbnail" id="downloads.add_thumbnail"">
<option value="" disabled selected> -- change thumbnail embed -- </option>
<option value="0">don't embed thumbnail</option>
<option value="1">embed thumbnail</option>
</select>
</div>
</div> </div>
<button type="submit">Update Settings</button> <button type="submit">Update Settings</button>
</form> </form>