mirror of
https://github.com/neosubhamoy/pytubepp.git
synced 2025-12-20 03:29:36 +05:30
(refactor): improved formatted and raw captions info
This commit is contained in:
@@ -3,7 +3,7 @@ from tabulate import tabulate
|
|||||||
from .config import get_temporary_directory, load_config, update_config, reset_config
|
from .config import get_temporary_directory, load_config, update_config, reset_config
|
||||||
from .download import download_progressive, download_nonprogressive, download_audio, progress
|
from .download import download_progressive, download_nonprogressive, download_audio, progress
|
||||||
from .postprocess import merge_audio_video, convert_to_mp3
|
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
|
import appdirs, os, re, sys, argparse, json
|
||||||
|
|
||||||
class YouTubeDownloader:
|
class YouTubeDownloader:
|
||||||
@@ -151,9 +151,19 @@ class YouTubeDownloader:
|
|||||||
print('Sorry, No video streams found....!!!')
|
print('Sorry, No video streams found....!!!')
|
||||||
sys.exit()
|
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(tabulate(table, headers=['Stream', 'Alias (for -s flag)', 'Format', 'Size', 'FrameRate', 'V-Codec', 'A-Codec', 'V-BitRate', 'A-BitRate']))
|
||||||
print('\n')
|
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:
|
else:
|
||||||
print('\nInvalid video link! Please enter a valid video url...!!')
|
print('\nInvalid video link! Please enter a valid video url...!!')
|
||||||
|
|
||||||
@@ -204,6 +214,15 @@ class YouTubeDownloader:
|
|||||||
print('Sorry, No video streams found....!!!')
|
print('Sorry, No video streams found....!!!')
|
||||||
sys.exit()
|
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 = {
|
output = {
|
||||||
'id': self.video.video_id,
|
'id': self.video.video_id,
|
||||||
'title': self.video.title,
|
'title': self.video.title,
|
||||||
@@ -213,7 +232,7 @@ class YouTubeDownloader:
|
|||||||
'published_on': self.video.publish_date.strftime('%d/%m/%Y'),
|
'published_on': self.video.publish_date.strftime('%d/%m/%Y'),
|
||||||
'duration': self.video.length,
|
'duration': self.video.length,
|
||||||
'streams': streams_list,
|
'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))
|
print(json.dumps(output, indent=4 if prettify else None))
|
||||||
|
|||||||
@@ -47,6 +47,17 @@ def get_unique_filename(filename, directory=downloadDIR):
|
|||||||
counter += 1
|
counter += 1
|
||||||
return filename
|
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):
|
def postprocess_cleanup(dir, files, random_filename):
|
||||||
for file in files:
|
for file in files:
|
||||||
file_path = os.path.join(dir, random_filename + file)
|
file_path = os.path.join(dir, random_filename + file)
|
||||||
|
|||||||
Reference in New Issue
Block a user