Skip to content

Video editing with Python

Add changelog and fix formatting

osaajani8433d3474c5f
diff --git a/CHANGELOG.md b/CHANGELOG.md
index b3575a66..1fd0bbf1 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -27,6 +27,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Fix ffmpeg reading crash when invalid metadata (see pr #2311)
- 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)
## [v2.1.2](https://github.com/zulko/moviepy/tree/master)
diff --git a/moviepy/video/VideoClip.py b/moviepy/video/VideoClip.py
index 04587c16..e64d8a37 100644
--- a/moviepy/video/VideoClip.py
+++ b/moviepy/video/VideoClip.py
@@ -1537,12 +1537,12 @@ class TextClip(ImageClip):
bottom pixel of text - absolute top pixel of text.
This is because in a font, some letter go above standard top line (e.g
letters with accents), and bellow standard baseline (e.g letters such as
- p, y, g).
-
+ p, y, g).
+
This notion is knowned under the name ascent and descent meaning the
highest and lowest pixel above and below baseline
- If your first line dont have an "accent character" and your last line
+ If your first line dont have an "accent character" and your last line
dont have a "descent character", you'll have some "fat" arround
"""
@@ -1801,7 +1801,7 @@ class TextClip(ImageClip):
allow_break=False,
) -> tuple[int, int]:
"""Find *real* dimensions a text will occupy, return a tuple (width, height)
-
+
.. note::
Text height calculation is quite complex due to how `Pillow` works.
When calculating line height, `Pillow` actually uses the letter ``A``
@@ -1863,7 +1863,7 @@ class TextClip(ImageClip):
)
text = "\n".join(lines)
-
+
# Use multiline textbbox to get width
left, top, right, bottom = draw.multiline_textbbox(
(0, 0),
@@ -1876,13 +1876,12 @@ class TextClip(ImageClip):
)
# For height calculate manually as textbbox is not realiable
- line_breaks = text.count('\n')
+ line_breaks = text.count("\n")
lines_height = line_breaks * line_height
paddings = real_font_size + stroke_width * 2
return (int(right - left), int(lines_height + paddings))
-
def __find_optimum_font_size(
self,
text,
@@ -1895,7 +1894,8 @@ class TextClip(ImageClip):
allow_break=False,
):
"""Find the best font size to fit as optimally as possible
- in a box of some width and optionally height"""
+ in a box of some width and optionally height
+ """
max_font_size = width
min_font_size = 1
diff --git a/moviepy/video/io/ffmpeg_reader.py b/moviepy/video/io/ffmpeg_reader.py
index 8f6835aa..41de8414 100644
--- a/moviepy/video/io/ffmpeg_reader.py
+++ b/moviepy/video/io/ffmpeg_reader.py
@@ -476,12 +476,12 @@ class FFmpegInfosParser:
# for default streams, set their numbers globally, so it's
# easy to get without iterating all
if 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 chapter
if self._current_chapter:
diff --git a/tests/test_TextClip.py b/tests/test_TextClip.py
index f19a4184..1d75c1b8 100644
--- a/tests/test_TextClip.py
+++ b/tests/test_TextClip.py
@@ -6,8 +6,6 @@ import numpy as np
import pytest
-import string
-
from moviepy import *
@@ -77,17 +75,38 @@ def test_no_text_nor_filename_arguments(method, util):
def test_label_autosizing(util):
# We test with about all possible letters
- text = "abcdefghijklmnopqrstuvwxyzáàâäãåāæąēéèêëīíìîïñōóòôöõøœęýABCDEFGHIJKLMNOPQRSTUVWXYZÁÀÂÄÃÅĀÆĄĒÉÈÊËĪÍÌÎÏÑŌÓÒÔÖÕØŒĘÝ"
- text += "\nabcdefghijklmnopqrstuvwxyzáàâäãåāæąēéèêëīíìîïñōóòôöõøœęýABCDEFGHIJKLMNOPQRSTUVWXYZÁÀÂÄÃÅĀÆĄĒÉÈÊËĪÍÌÎÏÑŌÓÒÔÖÕØŒĘÝ"
- text += "\nabcdefghijklmnopqrstuvwxyzáàâäãåāæąēéèêëīíìîïñōóòôöõøœęýABCDEFGHIJKLMNOPQRSTUVWXYZÁÀÂÄÃÅĀÆĄĒÉÈÊËĪÍÌÎÏÑŌÓÒÔÖÕØŒĘÝ"
-
+ text = "abcdefghijklmnopqrstuvwxyzáàâäãåāæąēéèêëīíìîïñōóòôöõøœęý\
+ ABCDEFGHIJKLMNOPQRSTUVWXYZÁÀÂÄÃÅĀÆĄĒÉÈÊËĪÍÌÎÏÑŌÓÒÔÖÕØŒĘÝ"
+ text += "\nabcdefghijklmnopqrstuvwxyzáàâäãåāæąēéèêëīíìîïñōóòôöõøœęý\
+ ABCDEFGHIJKLMNOPQRSTUVWXYZÁÀÂÄÃÅĀÆĄĒÉÈÊËĪÍÌÎÏÑŌÓÒÔÖÕØŒĘÝ"
+ text += "\nabcdefghijklmnopqrstuvwxyzáàâäãåāæąēéèêëīíìîïñōóòôöõøœęý\
+ ABCDEFGHIJKLMNOPQRSTUVWXYZÁÀÂÄÃÅĀÆĄĒÉÈÊËĪÍÌÎÏÑŌÓÒÔÖÕØŒĘÝ"
+
+ text_clip_margin = TextClip(
+ util.FONT,
+ method="label",
+ font_size=40,
+ text=text,
+ color="red",
+ bg_color="black",
+ stroke_width=3,
+ stroke_color="white",
+ margin=(1, 1),
+ ).with_duration(1)
+ text_clip_no_margin = TextClip(
+ util.FONT,
+ method="label",
+ font_size=40,
+ text=text,
+ color="red",
+ bg_color="black",
+ stroke_width=3,
+ stroke_color="white",
+ ).with_duration(1)
- text_clip_margin = TextClip(util.FONT, method="label", font_size=40, text=text, color="red", bg_color="black", stroke_width=3, stroke_color="white", margin=(1, 1)).with_duration(1)
- text_clip_no_margin = TextClip(util.FONT, method="label", font_size=40, text=text, color="red", bg_color="black", stroke_width=3, stroke_color="white").with_duration(1)
-
margin_frame = text_clip_margin.get_frame(1)
no_margin_frame = text_clip_no_margin.get_frame(1)
-
+
# The idea is, if autosizing work as expected, frame with 1px margin will
# have black color all around, where frame without margin will have white somewhere
first_row, last_row = (margin_frame[0], margin_frame[-1])
@@ -95,14 +114,17 @@ def test_label_autosizing(util):
# We add a bit of tolerance (about 1%) to account for possible rounding errors
assert np.allclose(first_row, [0, 0, 0], rtol=0.01)
- assert np.allclose(last_row, [0, 0, 0], rtol=0.01)
+ assert np.allclose(last_row, [0, 0, 0], rtol=0.01)
assert np.allclose(first_column, [0, 0, 0], rtol=0.01)
assert np.allclose(last_column, [0, 0, 0], rtol=0.01)
# We actually check on three pixels border, because some fonts
# always add a 1px padding all arround and some rounding error can make it two
first_three_rows, last_three_rows = (no_margin_frame[:3], no_margin_frame[-3:])
- first_three_columns, last_three_columns = (no_margin_frame[:, :3], no_margin_frame[:, -3:])
+ first_three_columns, last_three_columns = (
+ no_margin_frame[:, :3],
+ no_margin_frame[:, -3:],
+ )
# We add a bit of tolerance (about 1%) to account for possible rounding errors
assert not np.allclose(first_three_rows, [0, 0, 0], rtol=0.01)
@@ -111,6 +133,5 @@ def test_label_autosizing(util):
assert not np.allclose(last_three_columns, [0, 0, 0], rtol=0.01)
-
if __name__ == "__main__":
pytest.main()