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

Fix #933 - Correct Chapter Durations If They Extend Past the Episode Length #934

Open
wants to merge 3 commits into
base: trunk
Choose a base branch
from

Conversation

maarut
Copy link
Contributor

@maarut maarut commented Jun 28, 2023

Fixes #933

When reading chapter information, sanitise it so that no chapter duration is past the episodes end time.

To test

Playing ATP episode 538 shows the correct chapter duration.

Checklist

  • I have considered if this change warrants user-facing release notes and have added them to CHANGELOG.md if necessary.
  • I have considered adding unit tests for my changes.
  • I have updated (or requested that someone edit) the spreadsheet to reflect any new or changed analytics.

@maarut maarut requested a review from a team as a code owner June 28, 2023 21:38
- When reading in chapter information, sanitise the duration so that it
  doesn't go beyond the end of the episode.
@maarut maarut changed the title Update Changelog for the fixes on this branch Fix #933 - Correct Chapter Durations If They Extend Past the Episode Length Jul 1, 2023
Copy link
Member

@leandroalonso leandroalonso left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@maarut thanks for opening this issue and working on a fix. I think the logic might not work if a wrong duration is returned for a chapter that is not the last one. Keen to hear your thoughts about it.

@@ -68,4 +68,10 @@ class PodcastChapterParser {
// next see if the scheme is http or https, we don't support any others
return scheme.caseInsensitiveCompare("http") == .orderedSame || scheme.caseInsensitiveCompare("https") == .orderedSame
}

private func sanitise(chapterDuration: TimeInterval, usingChapterStart chapterStart: CMTime, episodeDuration: TimeInterval) -> TimeInterval {
let sanitisedDuration = chapterStart.seconds + chapterDuration > episodeDuration ? episodeDuration - chapterStart.seconds : chapterDuration
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Am I right in assuming that this logic works only if the last chapter has a wrong duration?

If, by any chance, the penultimate chapter has a similar issue, the operation episodeDuration - chapterStart.seconds will return the incorrect chapter duration (being the remaining time counting from the penultimate chapter, including the last one).

I think we can either add a logic to perform this only on the last chapter or adapt the logic so it grabs the duration between this chapter and the next one (if any).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for your comments.

Am I right in assuming that this logic works only if the last chapter has a wrong duration?

No. The logic applies to all chapters.

If, by any chance, the penultimate chapter has a similar issue, the operation episodeDuration - chapterStart.seconds will return the incorrect chapter duration (being the remaining time counting from the penultimate chapter, including the last one).

The logic checks the duration of the chapter. If the chapter is longer that the time remaining to the end of the episode, then the chapter duration is capped.

I think we can either add a logic to perform this only on the last chapter or adapt the logic so it grabs the duration between this chapter and the next one (if any).

I think this logic should apply to all chapters, whether hidden or not. We don't know what the episode author intended, so it's not "safe" to cap the duration to the beginning of the next chapter (IMO).

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@maarut maybe I'm overthinking it, but take this example:

episodeDuration is 60 and we have 3 chapters:

  1. duration is 20
  2. duration is 70
  3. duration is 30

For the second chapter, the expression: let sanitisedDuration = chapterStart.seconds + chapterDuration > episodeDuration ? episodeDuration - chapterStart.seconds : chapterDuration will be:

let sanitisedDuration = 20 + 70 > 60 ? 60 - 20 : 70 which will return 40 — while the correct duration of the chapter should be 10.

I believe that the episodeDuration - chapterStart.seconds is only correct when the chapter with an issue is the last one, otherwise, this won't return the correct duration.

I agree that the logic should fix all the chapters as you mentioned, but it seems that it won't if the chapter is not the last one.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@leandroalonso I think I was unclear in my previous comment and that there may be some misunderstanding as to how chapters are stored in an episode.

Each chapter contains a start time and a duration (amongst other things). Because chapters can overlap, it's not necessarily true that the end of one chapter is the beginning of the next. For example, you could have a file with chapters like so:

  1. startTime is 0, duration is 10
  2. startTime is 5, duration is 5
  3. startTime is 10, duration is 10
  4. startTime is 15, duration is 5

In this example, if episodeDuration is 17, we would want to cap chapters 3 and 4 to have a duration of 7 and 2 respectively. We can calculate the duration by taking episodeDuration and subtracting the startTime of the chapter.

I believe my code does exactly this (with the extra check to determine if we even need to make the calculation).

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@maarut got it! Thanks for explaining.

I think I was unclear in my previous comment and that there may be some misunderstanding as to how chapters are stored in an episode.

What do you think about adding some unit tests to cover this? This way another person in the future won't have to guess (and/or search) for this context as it would be available in the tests. :)

Another question, do you have any real episode examples that contain more than one chapter with the wrong duration?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Chapter Durations Sometimes Display As Ending Past the Episode End Time
2 participants