From a39120c064ad7d4851c83368f926439ecabf1e23 Mon Sep 17 00:00:00 2001 From: Federico Date: Thu, 11 Apr 2024 12:43:42 +0200 Subject: [PATCH] Added option to merge subtitles into one mp4 file or separate into .vtt files --- README.md | 1 + Src/Lib/FFmpeg/my_m3u8.py | 37 ++++++++++++++++++++++++++----------- config.json | 1 + 3 files changed, 28 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index d493b3a..d361fac 100644 --- a/README.md +++ b/README.md @@ -147,6 +147,7 @@ You can change some behaviors by tweaking the configuration file. | M3U8_OPTIONS | | Contains options specific to M3U8 file format. | | | download_audio | true | Indicates whether audio files should be downloaded or not. | false | | download_subtitles | true | Indicates whether subtitles should be downloaded or not. | false | +| merge_subtitles | true | Indicates if you want to merge subs in mp4 file or separate files .vtt| false | | specific_list_audio | ["ita"] | A list of specific audio languages to download. | ["eng", "fra"] | | specific_list_subtitles | ["eng"] | A list of specific subtitle languages to download. | ["spa", "por"] | |map_episode_name |%(tv_name)_S%(season)E%(episode)_%(episode_name)| Mapping to choose the name of all episode of TV Show (see [Episode Name Usage](#Episode-name-usage)). diff --git a/Src/Lib/FFmpeg/my_m3u8.py b/Src/Lib/FFmpeg/my_m3u8.py index 4078ff4..c8119e1 100644 --- a/Src/Lib/FFmpeg/my_m3u8.py +++ b/Src/Lib/FFmpeg/my_m3u8.py @@ -54,6 +54,7 @@ DOWNLOAD_AUDIO = config_manager.get_bool('M3U8_OPTIONS', 'download_audio') DOWNLOAD_SUBTITLES = config_manager.get_bool('M3U8_OPTIONS', 'download_subtitles') DOWNLOAD_SPECIFIC_AUDIO = config_manager.get_list('M3U8_OPTIONS', 'specific_list_audio') DOWNLOAD_SPECIFIC_SUBTITLE = config_manager.get_list('M3U8_OPTIONS', 'specific_list_subtitles') +MERGE_SUBTITLES = config_manager.get_bool('M3U8_OPTIONS', 'merge_subtitles') TQDM_MAX_WORKER = config_manager.get_int('M3U8', 'tdqm_workers') TQDM_PROGRESS_TIMEOUT = config_manager.get_int('M3U8', 'tqdm_progress_timeout') COMPLETED_PERCENTAGE = config_manager.get_float('M3U8', 'download_percentage') @@ -823,19 +824,33 @@ class Downloader(): # Check if there are any downloaded subtitles if len(self.downloaded_subtitle) > 0: - # Log adding subtitles - console.log(f"[cyan]Add subtitles.") + if MERGE_SUBTITLES: + # Log adding subtitles + console.log(f"[cyan]Add subtitles.") - # If no audio tracks were joined, use the original video path - if path_video_and_audio is None: - path_video_and_audio = self.video_track_path + # If no audio tracks were joined, use the original video path + if path_video_and_audio is None: + path_video_and_audio = self.video_track_path - # Transcode video with subtitles - path_join_subtitles = transcode_with_subtitles( - path_video_and_audio, - self.downloaded_subtitle, - os.path.join(self.base_path, "out.mkv") - ) + # Transcode video with subtitles + path_join_subtitles = transcode_with_subtitles( + path_video_and_audio, + self.downloaded_subtitle, + os.path.join(self.base_path, "out.mkv") + ) + else: + console.log("[cyan]Moving subtitle out of tmp folder.") + for obj_sub in self.downloaded_subtitle: + try: + language = obj_sub.get('language').lower() + + # Modifica la stringa se contiene "forced-", altrimenti la converte in minuscolo + language = (language.replace("forced-", "") + ".forced") if 'forced-' in language else language + sub_path = self.output_filename.replace(".mp4", f".{language}.vtt") + os.rename(obj_sub.get('path'), sub_path) + except Exception as e: + logging.error(f"Error moving subtitle: {e}. Skipping...") + continue self.path_video_and_audio = path_video_and_audio self.path_join_subtitles = path_join_subtitles diff --git a/config.json b/config.json index 5830763..a9f312a 100644 --- a/config.json +++ b/config.json @@ -34,6 +34,7 @@ "M3U8_OPTIONS": { "download_audio": true, "download_subtitles": true, + "merge_subtitles": true, "specific_list_audio": [ "ita" ],