biflé
biflé

Reputation: 173

How to make normal subtitles in MoviePy

Writing this makes me feel stupid... So, currently I am trying to make a simple subtitles for a video - I have ready phrases and their durations. But I am really struggling with making a caption on the video. Is there any method to make subtitles without saving a bunch of files for moviepy?

I tried TextClip - it returned some strange error messages (tride to fix them, haven't succeed). Then I tried to make image via PIL and convert in to numpy.array - this didn't work either.

UPD

The code I tried write for ImageClip + PIL

def get_image(text: str) -> numpy.array:

    image = Image.new('RGB', (600, 400), color = (255, 255, 255))
    draw = ImageDraw.Draw(image)
    draw.text((10, 10), text.encode('utf-8'), fill=(255, 255, 0))
    image.show()

    return numpy.array(image)


audio_with_subtitles = [(str, int), ...]
textclips = [
    ImageClip(
        get_image(phrase),
    ).set_duration(duration)
    for phrase, duration in audio_with_subtitles
]
videoclip = CompositeVideoClip(
    [
        videoclip,
        *textclips,
    ]
)

The tries with TextClip

audio_with_subtitles = [(str, int), ...]
textclips = [
    TextClip(
        phrase,
        fontsize=30,
        color='black',
    ).set_duration(duration)
    for phrase, duration in audio_with_subtitles
]
videoclip = CompositeVideoClip(
    [
        videoclip,
        *textclips,
    ]
)

The error I got while using TextClip

OSError: convert-im6.q16: attempt to perform an operation not allowed by the security policy `@/tmp/tmp7wugzgq3.txt' @ error/property.c/InterpretImageProperties/3706.
convert-im6.q16: no images defined `PNG32:/tmp/tmpk3q2l74h.png' @ error/convert.c/ConvertImageCommand/3229.


During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/bifle/Desktop/tiktok_test.py", line 67, in <module>
    TextClip(
  File "/home/bifle/.local/lib/python3.10/site-packages/moviepy/video/VideoClip.py", line 1146, in __init__
    raise IOError(error)
OSError: MoviePy Error: creation of None failed because of the following error:

convert-im6.q16: attempt to perform an operation not allowed by the security policy `@/tmp/tmp7wugzgq3.txt' @ error/property.c/InterpretImageProperties/3706.
convert-im6.q16: no images defined `PNG32:/tmp/tmpk3q2l74h.png' @ error/convert.c/ConvertImageCommand/3229.
.

.This error can be due to the fact that ImageMagick is not installed on your computer, or (for Windows users) that you didn't specify the path to the ImageMagick binary in file conf.py, or that the path you specified is incorrect

I tried installing ImageMagick and all *-dev things, nothing helped(

Upvotes: 1

Views: 362

Answers (1)

Atul Kumar
Atul Kumar

Reputation: 129

Locate the "policy.xml" file of your ImageMagick.

locate policy.xml

Search for :

<policy domain="path" rights="none" pattern="@*" />

and comment out it like

<!-- <policy domain="path" rights="none" pattern="@*" /> -->

Try restart your service.

Upvotes: 1

Related Questions