Add chapters to video info

This commit is contained in:
Ivan Habunek 2022-11-20 09:42:09 +01:00
parent d505056fee
commit 35e53ba4fd
No known key found for this signature in database
GPG Key ID: CDBD63C43A30BB95
2 changed files with 22 additions and 10 deletions

View File

@ -20,12 +20,14 @@ def info(args):
print_log("Fetching playlists...")
playlists = twitch.get_playlists(video_id, access_token)
if video:
if args.json:
video_json(video, playlists)
else:
video_info(video, playlists)
return
print_log("Fetching chapters...")
chapters = twitch.get_video_chapters(video_id)
if args.json:
video_json(video, playlists, chapters)
else:
video_info(video, playlists, chapters)
return
clip_slug = utils.parse_clip_identifier(args.video)
if clip_slug:
@ -43,7 +45,7 @@ def info(args):
raise ConsoleError("Invalid input: {}".format(args.video))
def video_info(video, playlists):
def video_info(video, playlists, chapters):
print_out()
print_video(video)
@ -52,8 +54,16 @@ def video_info(video, playlists):
for p in m3u8.loads(playlists).playlists:
print_out("<b>{}</b> {}".format(p.stream_info.video, p.uri))
if chapters:
print_out()
print_out("Chapters:")
for chapter in chapters:
start = utils.format_time(chapter["positionMilliseconds"] // 1000, force_hours=True)
duration = utils.format_time(chapter["durationMilliseconds"] // 1000)
print_out(f'{start} <b>{chapter["description"]}</b> ({duration})')
def video_json(video, playlists):
def video_json(video, playlists, chapters):
playlists = m3u8.loads(playlists).playlists
video["playlists"] = [
@ -66,6 +76,8 @@ def video_json(video, playlists):
} for p in playlists
]
video["chapters"] = chapters
print_json(video)

View File

@ -40,14 +40,14 @@ def format_duration(total_seconds):
return "{} sec".format(seconds)
def format_time(total_seconds):
def format_time(total_seconds, force_hours=False):
total_seconds = int(total_seconds)
hours = total_seconds // 3600
remainder = total_seconds % 3600
minutes = remainder // 60
seconds = total_seconds % 60
if hours:
if hours or force_hours:
return f"{hours:02}:{minutes:02}:{seconds:02}"
return f"{minutes:02}:{seconds:02}"