diff --git a/libavformat/utils.c b/libavformat/utils.c
index 23865c88c43d9cf39395caf4675aba1c09caec85..7abca632b56ff4f7a559f0e1a768f3c614c453eb 100644
--- a/libavformat/utils.c
+++ b/libavformat/utils.c
@@ -3167,7 +3167,7 @@ static void compute_chapters_end(AVFormatContext *s)
                 if (j != i && next_start > ch->start && next_start < end)
                     end = next_start;
             }
-            ch->end = (end == INT64_MAX) ? ch->start : end;
+            ch->end = (end == INT64_MAX || end < ch->start) ? ch->start : end;
         }
 }