简体   繁体   English

Python moviepy ImageClip() 生成损坏的文件

[英]Python moviepy ImageClip() generates corrupt files

I have a script that combines a background and a sentence into 1 image and then creates a 10second clip from just that image.我有一个脚本,将背景和句子组合成 1 个图像,然后仅从该图像创建一个 10 秒的剪辑。 However, even though the images get created correctly, about 20% of the created clips are corrupted.然而,即使图像创建正确,创建的剪辑中约有 20% 已损坏。 Here is the code I used:这是我使用的代码:

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")

For the font I use.ttf files.对于我使用的字体.ttf 文件。 I have tried just using 1 single ttf file and that still gave some corrupt files, so I have concluded that that could not be the issue.我试过只使用 1 个单独的 ttf 文件,但仍然给出了一些损坏的文件,所以我得出结论,这不是问题所在。 All the backgrounds used in this are.jpg files and all the quotes are just strings stored in the JSON file.这里使用的所有背景都是 .jpg 文件,所有引号都只是存储在 JSON 文件中的字符串。 I have before tried saving every image and still get the same behaviour from the ImageClip() function.我之前曾尝试保存每张图像,但仍然从 ImageClip() function 获得相同的行为。

Is there anything in my function that could cause this?我的 function 中是否有任何可能导致此问题的内容? Or is there a workaround for it?或者有解决方法吗? I would even use a different library to create the clips and preferrably also concatenate them into one video.我什至会使用不同的库来创建剪辑,最好也将它们连接成一个视频。

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")

The code above works and is very fast.上面的代码有效并且非常快。 I used the resize() method, which makes me able to compose everything together very fast.我使用了 resize() 方法,这使我能够非常快速地将所有内容组合在一起。 I also found that some of the backgrounds I used consistently generated corrupted clips.我还发现我使用的某些背景始终会生成损坏的剪辑。 Why that is I do not know, but after deleting them it works.我不知道为什么会这样,但是删除它们后它就起作用了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM