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
base: trunk
Are you sure you want to change the base?
Conversation
- When reading in chapter information, sanitise the duration so that it doesn't go beyond the end of the episode.
a8c823f
to
5f7acb3
Compare
There was a problem hiding this 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 |
There was a problem hiding this comment.
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).
There was a problem hiding this comment.
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).
There was a problem hiding this comment.
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:
duration
is20
duration
is70
duration
is30
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.
There was a problem hiding this comment.
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:
startTime
is0
,duration
is10
startTime
is5
,duration
is5
startTime
is10
,duration
is10
startTime
is15
,duration
is5
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).
There was a problem hiding this comment.
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?
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
CHANGELOG.md
if necessary.