Video editing with Python
Fix text without spaces overflowing TextClip boundaries
osaajani9d7d668a3c7a
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex 2f504df7..2167dd8b 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -33,6 +33,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0- Fix GPU h264_nvenc encoding not working.- Improve perfs of decorator by pre-computing arguments- Fix textclip being cut or of impredictable height (see issues #2325, #2260 and #2268)+- TextClip now properly breaklines on text without spaces, such as chinese (see #2260)- Fix TimeMirror and TimeSymmetrize cutting last second of clip- ImageSequenceClip was wrong when calculating fps with duration and no fps (see issue #2351)- More consistent frame seek (see issue #2115 and PR #2117)diff --git a/moviepy/video/VideoClip.py b/moviepy/video/VideoClip.pyindex d5378f9f..0b22a6d5 100644--- a/moviepy/video/VideoClip.py+++ b/moviepy/video/VideoClip.py@@ -1577,7 +1577,9 @@ class TextClip(ImageClip):_ = ImageFont.truetype(font)except Exception as e:raise ValueError(- "Invalid font {}, pillow failed to use it with error {}".format(font, e)+ "Invalid font {}, pillow failed to use it with error {}".format(+ font, e+ ))if filename:@@ -1777,9 +1779,15 @@ class TextClip(ImageClip):lines = []current_line = ""- words = text.split(" ")- for word in words:- temp_line = current_line + " " + word if current_line else word++ # We try to break on spaces as much as possible+ # if a text dont contain spaces (ex chinese), we will break when possible+ last_space = 0+ for index, char in enumerate(text):+ if char == " ":+ last_space = index++ temp_line = current_line + chartemp_left, temp_top, temp_right, temp_bottom = draw.multiline_textbbox((0, 0),temp_line,@@ -1790,11 +1798,20 @@ class TextClip(ImageClip):)temp_width = temp_right - temp_left- if temp_width <= width:- current_line = temp_line+ if temp_width >= width:+ # If we had a space previously, add everything up to the space+ # and reset last_space and current_line else add everything up+ # to previous char+ if last_space:+ lines.append(temp_line[0:last_space])+ current_line = temp_line[last_space + 1 : index + 1]+ last_space = 0+ else:+ lines.append(current_line[0:index])+ current_line = char+ last_space = 0else:- lines.append(current_line)- current_line = word+ current_line = temp_lineif current_line:lines.append(current_line)diff --git a/moviepy/video/io/ffmpeg_reader.py b/moviepy/video/io/ffmpeg_reader.pyindex 1469b7c2..31645efc 100644--- a/moviepy/video/io/ffmpeg_reader.py+++ b/moviepy/video/io/ffmpeg_reader.py@@ -492,12 +492,12 @@ class FFmpegInfosParser:# for default streams, set their numbers globally, so it's# easy to get without iterating allif self._current_stream["default"]:- self.result[f"default_{stream_type_lower}_input_number"] = (- input_number- )- self.result[f"default_{stream_type_lower}_stream_number"] = (- stream_number- )+ self.result[+ f"default_{stream_type_lower}_input_number"+ ] = input_number+ self.result[+ f"default_{stream_type_lower}_stream_number"+ ] = stream_number# exit chapterif self._current_chapter: