Skip to content

Commit

Permalink
Add rounding margin to the subtitle time search
Browse files Browse the repository at this point in the history
In case the subtitle times are not already set to exact frame times,
this extra rounding margin will round to the nearest frame. This
Could happen if the SRT subtitles were originally timed for a
different frame rate.
  • Loading branch information
bmatherly committed Aug 13, 2024
1 parent 884ef05 commit 017dd93
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 9 deletions.
3 changes: 2 additions & 1 deletion src/modules/plus/filter_subtitle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,8 @@ static mlt_frame filter_process(mlt_filter filter, mlt_frame frame)
int64_t frameMs = (int64_t) mlt_frame_get_position(frame) * 1000 * profile->frame_rate_den
/ profile->frame_rate_num;
int prevIndex = mlt_properties_get_int(properties, "_prevIndex");
int index = Subtitles::indexForTime(subtitles, frameMs, prevIndex);
int marginMs = 999 * profile->frame_rate_den / profile->frame_rate_num;
int index = Subtitles::indexForTime(subtitles, frameMs, prevIndex, marginMs);
if (index > -1) {
text = subtitles[index].text.c_str();
mlt_properties_set_int(properties, "_prevIndex", index);
Expand Down
3 changes: 2 additions & 1 deletion src/modules/plus/filter_subtitle_feed.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,8 @@ static mlt_frame filter_process(mlt_filter filter, mlt_frame frame)
int64_t frameMs = (int64_t) mlt_frame_get_position(frame) * 1000 * profile->frame_rate_den
/ profile->frame_rate_num;
int prevIndex = mlt_properties_get_int(properties, "_prevIndex");
int index = Subtitles::indexForTime(subtitles, frameMs, prevIndex);
int marginMs = 999 * profile->frame_rate_den / profile->frame_rate_num;
int index = Subtitles::indexForTime(subtitles, frameMs, prevIndex, marginMs);
if (index > -1) {
mlt_properties_set_int(properties, "_prevIndex", index);
}
Expand Down
17 changes: 11 additions & 6 deletions src/modules/plus/subtitles/subtitles.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -153,33 +153,38 @@ bool Subtitles::writeToSrtString(std::string &text, const Subtitles::SubtitleVec
return writeToSrtStream(textStream, items);
}

int Subtitles::indexForTime(const Subtitles::SubtitleVector &items, int64_t msTime, int searchStart)
int Subtitles::indexForTime(const Subtitles::SubtitleVector &items,
int64_t msTime,
int searchStart,
int msMargin)
{
// Return -1 if there is no subtitle for the time.
int index = -1;
int count = (int) items.size();
if (count == 0) {
// Nothing to search
} else if (count > 0 && items[0].start > msTime) {
} else if (count > 0 && (items[0].start - msMargin) > msTime) {
// No text if before the first item;
} else if (count > 1 && items[count - 1].end < msTime) {
// No text if after the last item;
} else if (searchStart > -1 && searchStart < count && items[searchStart].start <= msTime
} else if (searchStart > -1 && searchStart < count
&& (items[searchStart].start - msMargin) <= msTime
&& items[searchStart].end >= msTime) {
// First see if this is the same as the last subtitle
index = searchStart;
} else if (searchStart > -1 && (searchStart + 1) < count && items[searchStart].end < msTime
&& items[searchStart + 1].start > msTime) {
&& (items[searchStart + 1].start - msMargin) > msTime) {
// No text if between the previous and next subtitle
} else if (searchStart > -1 && (searchStart + 1) < count
&& items[searchStart + 1].start <= msTime && items[searchStart + 1].end >= msTime) {
&& (items[searchStart + 1].start - msMargin) <= msTime
&& items[searchStart + 1].end >= msTime) {
// See if this is the next subtitle
index = searchStart + 1;
} else {
// Perform a full search from the beginning
int i = 0;
for (i = 0; i < count; i++) {
if (items[i].start <= msTime && items[i].end >= msTime) {
if ((items[i].start - msMargin) <= msTime && items[i].end >= msTime) {
index = i;
break;
} else if (items[i].end > msTime) {
Expand Down
2 changes: 1 addition & 1 deletion src/modules/plus/subtitles/subtitles.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ SubtitleVector readFromSrtFile(const std::string &path);
bool writeToSrtFile(const std::string &path, const SubtitleVector &items);
SubtitleVector readFromSrtString(const std::string &text);
bool writeToSrtString(std::string &text, const SubtitleVector &items);
int indexForTime(const SubtitleVector &items, int64_t msTime, int searchStart);
int indexForTime(const SubtitleVector &items, int64_t msTime, int searchStart, int msMargin);
} // namespace Subtitles

#endif // SUBTITLES_H

0 comments on commit 017dd93

Please sign in to comment.