1
1
mirror of https://github.com/neosubhamoy/pytubepp.git synced 2025-12-19 08:53:01 +05:30

(refactor): improved formatted and raw captions info

This commit is contained in:
2025-01-21 22:30:28 +05:30
parent 6dba3d2b63
commit ea2ad9b258
2 changed files with 33 additions and 3 deletions

View File

@@ -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))

View File

@@ -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)