From 45c6c5287e94dd13b5b27405493b4ae8fe2cbcfc Mon Sep 17 00:00:00 2001 From: "Antonio J. Delgado" Date: Sat, 28 Dec 2024 19:57:12 +0200 Subject: [PATCH] Add thumbnail --- get_peertube_videos/get_peertube_videos.py | 41 ++++++++++++++++++++++ requirements.txt | 1 + 2 files changed, 42 insertions(+) diff --git a/get_peertube_videos/get_peertube_videos.py b/get_peertube_videos/get_peertube_videos.py index c3f4c84..5306bcd 100644 --- a/get_peertube_videos/get_peertube_videos.py +++ b/get_peertube_videos/get_peertube_videos.py @@ -10,6 +10,7 @@ import os import json import logging from logging.handlers import SysLogHandler +import subprocess import click import click_config_file import requests @@ -133,11 +134,51 @@ class GetPeertubeVideos: video_bytes = result.content with open(full_file_name, mode='wb') as video_file: video_file.write(video_bytes) + sel_thumb = '' + for thumb in item['media_thumbnail']: + if 'preview' in thumb['url']: + sel_thumb = thumb + if 'url' in sel_thumb: + split_url = sel_thumb['url'].split('.') + thumb_file_name = file_name.replace(file_extension, split_url[len(split_url)-1]) + full_thumb_file_name = os.path.join(self.config['download_dir'], thumb_file_name) + self._add_thumbnail_to_video(full_file_name, full_thumb_file_name) else: self._log.debug( "Item already downloaded, skipping." ) + def _add_thumbnail_to_video(self, video_file, thumb_file): + self._log.debug( + 'Looking for ffmpeg tool...' + ) + result = subprocess.run(["which", "ffmpeg"], capture_output=True, check=False) + if result.stdout == b'': + self._log.error( + "Error finding ffmpeg, thumbnail won't be added to video." + ) + return None + self._log.debug( + "Adding thumbnail '%s' to file '%s'...", + thumb_file, + video_file + ) + result = subprocess.run( + [ + 'ffmpeg', + '-i', video_file, + '-i', thumb_file, + '-map', '1', + '-map', '0', + '-c', 'copy', + '-disposition:0', 'attached_pic', f"{video_file}_with_thumb" + ], + capture_output=True, + check=True + ) + os.remove(video_file) + os.rename(f"{video_file}_with_thumb", video_file) + def _write_downloaded_items(self): with open(self.config['downloaded_database'], 'w', encoding='utf-8') as db_file: for download_item in self.downloaded_items: diff --git a/requirements.txt b/requirements.txt index 328c7f6..9f1dc4f 100644 --- a/requirements.txt +++ b/requirements.txt @@ -3,3 +3,4 @@ click_config_file requests transmission_rpc feedparser +ffmpeg