0

I am working on a reddit youtube video creation script using MoviePy in Python. However, I encountered an error related to ImageMagick during the execution of the code.

The error message I received is:

Exception has occurred: OSError MoviePy Error: creation of None failed because of the following error:

magick.exe: label expected `@C:\Users\HHHHHH~1\AppData\Local\Temp\tmp2zy8buxv.txt' @ error/annotate.c/GetMultilineTypeMetrics/797. .

.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 OSError: magick.exe: label expected `@C:\Users\HHHHHH~1\AppData\Local\Temp\tmp2zy8buxv.txt' @ error/annotate.c/GetMultilineTypeMetrics/797.

During handling of the above exception, another exception occurred:

File "C:\Users\hhhhhhhhhhhhhhhhhhhh\Downloads\VSCODE\movie.py", line 84, in <module> txt_clip = (TextClip('\n'.join(lines), fontsize=25, font='Arial', color='white', align='West', size=(830,305)) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ OSError: MoviePy Error: creation of None failed because of the following error:

magick.exe: label expected `@C:\Users\HHHHHH~1\AppData\Local\Temp\tmp2zy8buxv.txt' @ error/annotate.c/GetMultilineTypeMetrics/797. .

.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

The full code reads:

from moviepy.editor import * import os from moviepy.config import change_settings import conf

Use the specified ImageMagick path

change_settings({"IMAGEMAGICK_BINARY": conf.IMAGEMAGICK_BINARY})

reddit_text_path = "C:\Users\hhhhhhhhhhhhhhhhhhhh\Downloads\VSCODE\reddit_text" reddit_text_sound_path = "C:\Users\hhhhhhhhhhhhhhhhhhhh\Downloads\VSCODE\reddit_text_sound" reddit_text_image_path = "C:\Users\hhhhhhhhhhhhhhhhhhhh\Downloads\VSCODE\reddit_text_image" static_trans_path = "C:\Users\hhhhhhhhhhhhhhhhhhhh\Downloads\VSCODE\reddit_transition\statictrans.mp4" reddit_users_path = "C:\Users\hhhhhhhhhhhhhhhhhhhh\Downloads\VSCODE\output\reddit_users.txt"

clips = [] audio_clips = [] image_index = 1

text_files = sorted(os.listdir(reddit_text_path)) sound_files = sorted(os.listdir(reddit_text_sound_path))

Read the file and split the lines

users = open(reddit_users_path, 'r', encoding='latin-1').read().splitlines()

Extract the first name from the first line

first_name = users[0].split()[0]

Trim the first name if it's longer than 7 characters and add "..."

if len(first_name) > 7: first_name = first_name[:7] + "..."

Create a new list with only the first name

trimmedusers = str(first_name)

for text_file, sound_file, user in zip(text_files, sound_files, users): text_path = os.path.join(reddit_text_path, text_file) sound_path = os.path.join(reddit_text_sound_path, sound_file)

text = open(text_path, 'r', encoding='latin-1').read() audio = AudioFileClip(sound_path)

Set the image path based on the current iteration

if text_file == text_files[0]: image_path = os.path.join(reddit_text_image_path, 'reddit_text_01.jpg') author_pos = ('10%', '10%') text_pos = ('center', 'center+100') else: # Determine image based on text length if len(text) <= 100: image_path = os.path.join(reddit_text_image_path, 'reddit_text_02.jpg') else: image_path = os.path.join(reddit_text_image_path, 'reddit_text_03.jpg')

if image_path == os.path.join(reddit_text_image_path, 'reddit_text_01.jpg'): # Add author to the image clip author_clip = (TextClip(trimmedusers, fontsize=15, font='Arial', color='#696B6B', align='West', size=(830,305)) .set_pos((338, 138)) .set_duration(audio.duration)) elif image_path == os.path.join(reddit_text_image_path, 'reddit_text_02.jpg'): author_clip = (TextClip(user, fontsize=20, font='Arial', color='white', align='West', size=(830,305)) .set_pos((240, 148)) .set_duration(audio.duration)) elif image_path == os.path.join(reddit_text_image_path, 'reddit_text_03.jpg'): author_clip = (TextClip(user, fontsize=20, font='Arial', color='white', align='West', size=(830,305)) .set_pos((200, 35)) .set_duration(audio.duration))

image_clip = ImageClip(image_path).set_duration(audio.duration)

lines = [] words = text.split() while len(words) > 0: line_words = [] while len(words) > 0 and sum([len(w) for w in line_words]) + len(line_words) + len(words[0]) <= 50: line_words.append(words.pop(0))

Add text to the image clip

if image_path == os.path.join(reddit_text_image_path, 'reddit_text_01.jpg'):

Add text to the image clip

txt_clip = (TextClip('\n'.join(lines), fontsize=25, font='Arial', color='white', align='West', size=(830,305)) .set_pos((240, 190)) # adjust the position based on the image .set_duration(audio.duration))

elif image_path == os.path.join(reddit_text_image_path, 'reddit_text_02.jpg'): txt_clip = (TextClip('\n'.join(lines), fontsize=25, font='Arial', color='white', align='West', size=(830,305)) .set_pos((240, 190)) .set_duration(audio.duration)) elif image_path == os.path.join(reddit_text_image_path, 'reddit_text_03.jpg'): txt_clip = (TextClip('\n'.join(lines), fontsize=25, font='Arial', color='white', align='West', size=(830,305)) .set_pos((200, 190)) .set_duration(audio.duration))

composite_clip = CompositeVideoClip([image_clip, txt_clip, author_clip])

Add the composite clip and static transition clip to the list of clips

clips.append(composite_clip) audio_clips.append(audio)

if text_file != text_files[-1]: # Add the audio and video from the transition clip trans_video = VideoFileClip(static_trans_path) trans_audio = trans_video.audio clips.append(trans_video) audio_clips.append(trans_audio)

Concatenate all the clips in the list

video = concatenate_videoclips(clips) audio = concatenate_audioclips(audio_clips)

video.audio = audio

video.write_videofile("C:\Users\hhhhhhhhhhhhhhhhhhhh\Downloads\VSCODE\output\reddit_video.mp4", fps=24)

Thank you guys so much. Thanks for reading.

alanelrod
  • 1
  • 1
  • Please trim your code to make it easier to find your problem. Follow these guidelines to create a [minimal reproducible example](https://stackoverflow.com/help/minimal-reproducible-example). – Community Jun 18 '23 at 01:08

1 Answers1

0

have you installed ImageMagick on your computer? moviepy needs it for rendering movies.

it might be that easy.

  • I have. I have installed version 7.1.1-11 and installed legacy dependencies in the downloader. – alanelrod Jun 18 '23 at 21:48
  • Does any kind of rendering video work with moviepy work for you? Try simple code :clip.subclip(start time ,finish time ) . If u don't know this google it. If it didn't maybe u should reinstall dependencies. Check one more thing: does the address that u have written to save file exist? And please trim your code to make it easier to read. – Kian Farooghi Jun 20 '23 at 04:36
  • I used a generic clip.subclip script and it worked, not sure why this file worked. – alanelrod Jun 30 '23 at 14:31
  • did u render a clip.subcip ? u saved a video file ? it worked? – Kian Farooghi Jul 03 '23 at 13:22