diff --git a/src/chapter_parser_xml.cpp b/src/chapter_parser_xml.cpp index 642cfef42..3c5cdd17d 100644 --- a/src/chapter_parser_xml.cpp +++ b/src/chapter_parser_xml.cpp @@ -601,6 +601,38 @@ bool probe_xml_chapters(mm_text_io_c *in) { return false; } +KaxChapters *select_chapters_in_timeframe(KaxChapters *chapters, + int64_t min_tc, int64_t max_tc, + int64_t offset, bool validate) { + uint32_t i; + KaxEditionEntry *eentry; + + for (i = 0; i < chapters->ListSize(); i++) { + if (validate) + validate_chapters(0, -1, *static_cast((*chapters)[i])); + remove_entries(min_tc, max_tc, offset, + *static_cast((*chapters)[i])); + } + + i = 0; + while (i < chapters->ListSize()) { + eentry = static_cast((*chapters)[i]); + if (eentry->ListSize() == 0) { + chapters->Remove(i); + delete eentry; + + } else + i++; + } + + if (chapters->ListSize() == 0) { + delete chapters; + chapters = NULL; + } + + return chapters; +} + KaxChapters *parse_xml_chapters(mm_text_io_c *in, int64_t min_tc, int64_t max_tc, int64_t offset) { bool done; @@ -608,10 +640,8 @@ KaxChapters *parse_xml_chapters(mm_text_io_c *in, int64_t min_tc, XML_Parser parser; XML_Error xerror; char *emsg; - int i; string buffer; KaxChapters *chapters; - KaxEditionEntry *eentry; done = 0; @@ -650,29 +680,9 @@ KaxChapters *parse_xml_chapters(mm_text_io_c *in, int64_t min_tc, chapters = pdata->chapters; if (chapters != NULL) { - - for (i = 0; i < chapters->ListSize(); i++) { - validate_chapters(0, -1, *static_cast((*chapters)[i])); - remove_entries(min_tc, max_tc, offset, - *static_cast((*chapters)[i])); - } - - i = 0; - while (i < chapters->ListSize()) { - eentry = static_cast((*chapters)[i]); - if (eentry->ListSize() == 0) { - chapters->Remove(i); - delete eentry; - - } else - i++; - } - - if (chapters->ListSize() == 0) { - delete chapters; - chapters = NULL; - - } else if (!chapters->CheckMandatory()) + chapters = select_chapters_in_timeframe(chapters, min_tc, max_tc, offset, + true); + if ((chapters != NULL) && !chapters->CheckMandatory()) die("chapters->CheckMandatory() failed. Should not have happened!"); } diff --git a/src/chapters.h b/src/chapters.h index 8790a1b03..079e07838 100644 --- a/src/chapters.h +++ b/src/chapters.h @@ -43,6 +43,9 @@ void write_chapters_xml(KaxChapters *chapters, FILE *out); void write_chapters_simple(int &chapter_num, KaxChapters *chapters, FILE *out); KaxChapters *copy_chapters(KaxChapters *source); +KaxChapters *select_chapters_in_timeframe(KaxChapters *chapters, + int64_t min_tc, int64_t max_tc, + int64_t offset, bool validate); #endif // __CHAPTERS_H diff --git a/src/mkvmerge.cpp b/src/mkvmerge.cpp index 887254bab..80f13f767 100644 --- a/src/mkvmerge.cpp +++ b/src/mkvmerge.cpp @@ -1900,8 +1900,9 @@ void finish_file() { start = cluster_helper->get_first_timecode() + offset; end = cluster_helper->get_max_timecode() + offset; - chapters_here = parse_chapters(chapter_file_name, start, end, offset, - chapter_language, chapter_charset); + chapters_here = copy_chapters(kax_chapters); + chapters_here = select_chapters_in_timeframe(chapters_here, start, end, + offset, false); if (chapters_here != NULL) kax_chapters_void->ReplaceWith(*chapters_here, *out, true); delete kax_chapters_void;