[英]Python moviepy ImageClip() generates corrupt files
我有一個腳本,將背景和句子組合成 1 個圖像,然后僅從該圖像創建一個 10 秒的剪輯。 然而,即使圖像創建正確,創建的剪輯中約有 20% 已損壞。 這是我使用的代碼:
file = open(f"quotes/{genre}.json")
quotes = json.load(file)
# Create a TEMP folder to save the images
os.mkdir("clips")
clips = list()
for i in range(imagesNeeded):
# Get quote, background and font
quote = random.choice(quotes)
background = random.choice(os.listdir(f"./backgrounds/{genre}"))
font = random.choice(os.listdir(f"./fonts"))
#Video size 1920x1080 16:9
# Open image and resize to ratio
image = Image.open(f'./backgrounds/{genre}/{background}')
image_editable = ImageDraw.Draw(image)
#resize quote so it will fit the image
font_size = 200
new_quote, width, height = resizequote(quote, image, font_size)
title_font = ImageFont.truetype(f"./fonts/{font}", font_size)
w, h = image_editable.textsize(new_quote,font=title_font)
image_editable.text((0.5*(width-w),0.5*(height-h)), new_quote, (255, 255, 255), font=title_font)
#Create the clip
numpydata = asarray(image)
clip = ImageClip(numpydata).set_duration(10)
filename = ''.join(random.choice([chr(i) for i in range(ord('a'),ord('z'))]) for _ in range(20))
clip.write_videofile(f"clips/{filename}.mp4", fps=24, threads=8)
print(f"Background: {background}")
print(f"Quote: {quote}")
print(f"Font: {font}")
clips.append(clip)
print(f"Created {i} clips")
對於我使用的字體.ttf 文件。 我試過只使用 1 個單獨的 ttf 文件,但仍然給出了一些損壞的文件,所以我得出結論,這不是問題所在。 這里使用的所有背景都是 .jpg 文件,所有引號都只是存儲在 JSON 文件中的字符串。 我之前曾嘗試保存每張圖像,但仍然從 ImageClip() function 獲得相同的行為。
我的 function 中是否有任何可能導致此問題的內容? 或者有解決方法嗎? 我什至會使用不同的庫來創建剪輯,最好也將它們連接成一個視頻。
def create_video(genre, audioLength, audio):
imagesNeeded = math.ceil(audioLength/10)
print(f"Creating {imagesNeeded} clips")
file = open(f"quotes/{genre}.json")
quotes = json.load(file)
# Create a TEMP folder to save the images
os.mkdir("clips")
clips = list()
for i in range(imagesNeeded):
# Get quote, background and font
quote = random.choice(quotes)
background = random.choice(os.listdir(f"./backgrounds/{genre}"))
font = random.choice(os.listdir(f"./fonts"))
#Video size 1920x1080 16:9
# Open image and resize to ratio
image = Image.open(f'./backgrounds/{genre}/{background}')
image_editable = ImageDraw.Draw(image)
#resize quote so it will fit the image
font_size = 200
new_quote, width, height = resizequote(quote, image, font_size)
title_font = ImageFont.truetype(f"./fonts/{font}", font_size)
w, h = image_editable.textsize(new_quote,font=title_font)
image_editable.text((0.5*(width-w),0.5*(height-h)), new_quote, (255, 255, 255), font=title_font)
#Create the clip
numpydata = asarray(image)
clip = ImageClip(numpydata).set_duration(10).resize( (1920,1080) )
filename = ''.join(random.choice([chr(i) for i in range(ord('a'),ord('z'))]) for _ in range(15))
clip.write_videofile(f"clips/{filename}.mp4", fps=24, threads=8)
clips.append(clip)
print(f"Created {i} clips")
上面的代碼有效並且非常快。 我使用了 resize() 方法,這使我能夠非常快速地將所有內容組合在一起。 我還發現我使用的某些背景始終會生成損壞的剪輯。 我不知道為什么會這樣,但是刪除它們后它就起作用了。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.