Skip to content

Commit

Permalink
embed chapter data for crunchyroll
Browse files Browse the repository at this point in the history
  • Loading branch information
justin025 committed Feb 13, 2025
1 parent 5f0bba5 commit 5f09448
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 12 deletions.
6 changes: 3 additions & 3 deletions src/onthespot/api/crunchyroll.py
Original file line number Diff line number Diff line change
Expand Up @@ -185,11 +185,11 @@ def crunchyroll_get_episode_metadata(token, item_id):

url = f'{BASE_URL}/content/v2/cms/objects/{item_id.split("/")[0]}?ratings=true&images=true&locale=en-US'
episode_data = make_call(url, headers=headers)
# intro and credit timestamps
# intro and credit timestamps (done in downloader step)
#https://static.crunchyroll.com/skip-events/production/G4VUQ588P.json
# genre
# genre (dont think mkv supports this)
#https://www.crunchyroll.com/content/v2/discover/categories?guid=G1XHJV0XM&locale=en-US
# copyright
# copyright (dont think mkv supports this)
#https://static.crunchyroll.com/copyright/G1XHJV0XM.json

info_dict = episode_data['data'][0]
Expand Down
25 changes: 22 additions & 3 deletions src/onthespot/downloader.py
Original file line number Diff line number Diff line change
Expand Up @@ -501,6 +501,7 @@ def run(self):

# Extract preferred language
encrypted_files = []
video_files = []
subtitle_formats = []
for version in versions:
if version['audio_locale'] in config.get('preferred_audio_language').split(',') or config.get('download_all_available_audio'):
Expand Down Expand Up @@ -548,12 +549,30 @@ def run(self):
'language': version['audio_locale']
})
audio.download(mpd_url)

crunchyroll_close_stream(token, item_id, stream_token)

# Download Chapters
if not config.get('raw_media_download'):
if self.gui:
self.progress.emit(item, self.tr("Downloading Chapters"), 1)
chapter_data = requests.get(f'https://static.crunchyroll.com/skip-events/production/{version['guid']}.json').json()
chapter_file = temp_file_path + f' - {version['audio_locale']}.txt'
with open(chapter_file, 'w', encoding='utf-8') as file:
file.write(';FFMETADATA1\n')
for entry in ['intro', 'credits']:
if chapter_data.get(entry):
file.write(f'[CHAPTER]\nTIMEBASE=1/1\nSTART={chapter_data[entry].get('start')}\nEND={chapter_data[entry].get('end')}\ntitle={entry.title()}\nlanguage={version['audio_locale']}\n')
video_files.append({
'path': chapter_file,
'type': 'chapter',
'format': 'txt',
'language': version['audio_locale']
})

if self.gui:
self.progress.emit(item, self.tr("Decrypting"), 99)

video_files = []
for encrypted_file in encrypted_files:
decrypted_temp_file_path = os.path.splitext(encrypted_file['path'])[0]
video_files.append({
Expand Down Expand Up @@ -598,7 +617,7 @@ def run(self):
subtitle_file = temp_file_path + f' - {lang}.{subtitle_format['extension']}'
if not os.path.exists(subtitle_file):
subtitle_data = requests.get(subtitle_format['url']).text
with open(subtitle_file, "w") as file:
with open(subtitle_file, 'w', encoding='utf-8') as file:
file.write(subtitle_data)
video_files.append({
'path': subtitle_file,
Expand Down Expand Up @@ -711,7 +730,7 @@ def run(self):
output_format = config.get("show_file_format")
elif item_type == "movie":
output_format = config.get("movie_file_format")
convert_video_format(file_path, output_format, video_files)
convert_video_format(file_path, output_format, video_files, item_metadata)
item['file_path'] = file_path + '.' + output_format
else:
item['file_path'] = file_path + '.mp4'
Expand Down
14 changes: 8 additions & 6 deletions src/onthespot/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ def convert_audio_format(filename, bitrate, default_format):
os.remove(temp_name)


def convert_video_format(output_path, output_format, video_files):
def convert_video_format(output_path, output_format, video_files, item_metadata):
target_path = os.path.abspath(output_path)
file_name = os.path.basename(target_path)
filetype = os.path.splitext(file_name)[1]
Expand All @@ -258,15 +258,17 @@ def convert_video_format(output_path, output_format, video_files):
i = 0
current_type = file["type"]
command += ['-i', file['path']]
format_map += ['-map', f'{map_index}:{current_type[:1]}']

if file.get('language'):
language = file.get('language')
format_map += [f'-metadata:s:{current_type[:1]}:{i}', f'title={file.get('language')}']
format_map += [f'-metadata:s:{current_type[:1]}:{i}', f'language={file.get('language')[:2]}']
if current_type != 'chapter':
format_map += ['-map', f'{map_index}:{current_type[:1]}']
if file.get('language'):
language = file.get('language')
format_map += [f'-metadata:s:{current_type[:1]}:{i}', f'title={file.get('language')}']
format_map += [f'-metadata:s:{current_type[:1]}:{i}', f'language={file.get('language')[:2]}']

i += 1

format_map += [f'-metadata', f'title={item_metadata['title']}']
command += format_map

# Set log level based on environment variable
Expand Down

0 comments on commit 5f09448

Please sign in to comment.