Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add flag to use chapters offset for non audible files tagged using audible api #171

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ optional arguments:
Output directory
-p PATH_FORMAT, --path_format PATH_FORMAT
Structure of output path/naming.Supported terms: author, narrator, series_name, series_position, subtitle, title, year
--chapters-offset For files not from Audible, adjust chapters to offset according to brandIntroDurationMs

```

Expand Down
14 changes: 13 additions & 1 deletion src/m4b_merge/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def run_all(input_path):
# Create BookData object from asin response
aud = audible_helper.BookData(asin)
metadata = aud.fetch_api_data(config.api_url)
chapters = aud.get_chapters()
chapters = aud.get_chapters(config.use_chapters_offset)

# Process metadata and run components to merge files
m4b = m4b_helper.M4bMerge(input_data, metadata, input_path, chapters)
Expand All @@ -39,6 +39,11 @@ def validate_args(args):
config.api_url = args.api_url
else:
config.api_url = "https://api.audnex.us"
# Chapter offset for non audible files
if args.chapters_offset:
config.use_chapters_offset = True
else:
config.use_chapters_offset = False
# Completed Directory
if args.completed_directory:
config.junk_dir = args.completed_directory
Expand Down Expand Up @@ -84,6 +89,7 @@ def validate_args(args):
logging.debug(f'Using CPU cores: {config.num_cpus}')
logging.debug(f'Using output path: {config.output}')
logging.debug(f'Using output format: {config.path_format}')
logging.debug(f'Using chapters offset: {config.use_chapters_offset}')
# Inputs
# Last to be checked
if args.inputs:
Expand Down Expand Up @@ -143,6 +149,12 @@ def main():
),
type=str
)
parser.add_argument(
"--chapters-offset",
help="For files not from Audible, adjust chapters to offset according to brandIntroDurationMs",
action="store_true",
required=False
)

validate_args(parser.parse_args())

Expand Down
14 changes: 10 additions & 4 deletions src/m4b_merge/audible_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,13 @@ def ms_to_timestamp(self, input_duration):

return timestamp + '.' + '000'

def get_chapters(self):
def get_chapters(self, use_chapters_offset):
# Select chapter data from json response
chapter_info = self.metadata_dict['chapter_info']

if use_chapters_offset:
offset = chapter_info.get('brandIntroDurationMs', 0)
else:
offset = 0
# Only use Audible chapters if tagged as accurate
if 'isAccurate' in chapter_info and chapter_info['isAccurate'] is True:
chapter_output = []
Expand All @@ -54,8 +57,11 @@ def get_chapters(self):
)

# Append each chapter to array
for chapter in chapter_info['chapters']:
chap_start = self.ms_to_timestamp(chapter['startOffsetMs'])
for index, chapter in enumerate(chapter_info['chapters']):
if index == 0:
chap_start = self.ms_to_timestamp(chapter['startOffsetMs'])
else:
chap_start = self.ms_to_timestamp(chapter['startOffsetMs'] - offset)
# Starting chapter title data
chapter_title = chapter['title']
chapter_output.append(
Expand Down