From ea2ad9b2585914f76cb907436242fc35a0458849 Mon Sep 17 00:00:00 2001 From: Subhamoy Biswas Date: Tue, 21 Jan 2025 22:30:28 +0530 Subject: [PATCH] (refactor): improved formatted and raw captions info --- pytubepp/main.py | 25 ++++++++++++++++++++++--- pytubepp/utils.py | 11 +++++++++++ 2 files changed, 33 insertions(+), 3 deletions(-) diff --git a/pytubepp/main.py b/pytubepp/main.py index 7995d6e..8df47bb 100644 --- a/pytubepp/main.py +++ b/pytubepp/main.py @@ -3,7 +3,7 @@ from tabulate import tabulate from .config import get_temporary_directory, load_config, update_config, reset_config from .download import download_progressive, download_nonprogressive, download_audio, progress from .postprocess import merge_audio_video, convert_to_mp3 -from .utils import get_version, clear_temp_files, is_valid_url, network_available, ffmpeg_installed, nodejs_installed +from .utils import get_version, clear_temp_files, is_valid_url, network_available, ffmpeg_installed, nodejs_installed, unpack_caption import appdirs, os, re, sys, argparse, json class YouTubeDownloader: @@ -151,9 +151,19 @@ class YouTubeDownloader: print('Sorry, No video streams found....!!!') sys.exit() - print(f'\nTitle: {self.video.title}\nAuthor: {self.author}\nPublished On: {self.video.publish_date.strftime("%d/%m/%Y")}\nDuration: {f"{self.video.length//3600:02}:{(self.video.length%3600)//60:02}:{self.video.length%60:02}" if self.video.length >= 3600 else f"{(self.video.length%3600)//60:02}:{self.video.length%60:02}"}\nViews: {self.views}\nCaptions: {[caption.code for caption in self.captions.keys()] or "Unavailable"}\n') + print(f'\nTitle: {self.video.title}\nAuthor: {self.author}\nPublished On: {self.video.publish_date.strftime("%d/%m/%Y")}\nDuration: {f"{self.video.length//3600:02}:{(self.video.length%3600)//60:02}:{self.video.length%60:02}" if self.video.length >= 3600 else f"{(self.video.length%3600)//60:02}:{self.video.length%60:02}"}\nViews: {self.views}\nCaptions: {"Available" if self.captions else "Unavailable"}') + + print('\n') print(tabulate(table, headers=['Stream', 'Alias (for -s flag)', 'Format', 'Size', 'FrameRate', 'V-Codec', 'A-Codec', 'V-BitRate', 'A-BitRate'])) print('\n') + + if self.captions: + caption_table = [] + for caption in self.captions: + cap_code, cap_lang = unpack_caption(caption) + caption_table.append([cap_lang, cap_code]) + print(tabulate(caption_table, headers=['Caption', 'CaptionCode (for -c flag)'])) + print('\n') else: print('\nInvalid video link! Please enter a valid video url...!!') @@ -204,6 +214,15 @@ class YouTubeDownloader: print('Sorry, No video streams found....!!!') sys.exit() + captions_list = [] + if self.captions: + for caption in self.captions: + cap_code, cap_lang = unpack_caption(caption) + captions_list.append({ + 'code': cap_code, + 'lang': cap_lang + }) + output = { 'id': self.video.video_id, 'title': self.video.title, @@ -213,7 +232,7 @@ class YouTubeDownloader: 'published_on': self.video.publish_date.strftime('%d/%m/%Y'), 'duration': self.video.length, 'streams': streams_list, - 'captions': [caption.code for caption in self.captions.keys()] or None + 'captions': captions_list or None } print(json.dumps(output, indent=4 if prettify else None)) diff --git a/pytubepp/utils.py b/pytubepp/utils.py index 7ee8131..614d125 100644 --- a/pytubepp/utils.py +++ b/pytubepp/utils.py @@ -47,6 +47,17 @@ def get_unique_filename(filename, directory=downloadDIR): counter += 1 return filename +def unpack_caption(caption): + caption_str = str(caption) + code_start = caption_str.find('code="') + 6 + code_end = caption_str.find('"', code_start) + lang_start = caption_str.find('lang="') + 6 + lang_end = caption_str.find('"', lang_start) + + code = caption_str[code_start:code_end] + lang = caption_str[lang_start:lang_end] + return code, lang + def postprocess_cleanup(dir, files, random_filename): for file in files: file_path = os.path.join(dir, random_filename + file)