0
import moviepy.editor as mp
import tkinter as tk
from tkinter import filedialog
import math
from PIL import Image
import numpy


def zoom_in_effect(clip, zoom_ratio=0.02):
    def effect(get_frame, t):
        img = Image.fromarray(get_frame(t))
        base_size = img.size

        new_size = [
            math.ceil(img.size[0] * (1 + (zoom_ratio * t))),
            math.ceil(img.size[1] * (1 + (zoom_ratio * t)))
        ]

        # The new dimensions must be even.
        new_size[0] = new_size[0] + (new_size[0] % 2)
        new_size[1] = new_size[1] + (new_size[1] % 2)

        img = img.resize(new_size, Image.LANCZOS)

        x = math.ceil((new_size[0] - base_size[0]) / 2)
        y = math.ceil((new_size[1] - base_size[1]) / 2)

        img = img.crop([
            x, y, new_size[0] - x, new_size[1] - y
        ]).resize(base_size, Image.LANCZOS)

        #result = numpy.array(img)
        result = numpy.array(img, dtype=numpy.uint8)

        img.close()

        return result

    return clip.fl(effect)


    

def make_center_video():
    
    size = (1080, 1080)

    audio_file = '/home/faisal/pythonfiles/audio/tts_voice.wav'
    audio = mp.AudioFileClip(audio_file)
   
    root = tk.Tk()
    root.withdraw()
     
    print("waiting for Image Selection....")

    img = filedialog.askopenfilename()


    slide = mp.ImageClip(img).set_fps(29).set_duration(audio.duration).resize(size)
    slide = zoom_in_effect(slide, 0.02)
    slide.write_videofile('/home/faisal/pythonfiles/videos/zoom-short.mp4',codec='libx264',  fps=29)
    
    
    size = (600, 600)

 


    slide = mp.ImageClip(img).set_fps(29).set_duration(audio.duration).resize(size)
    slide = zoom_in_effect(slide, 0.02)
    slide.write_videofile('/home/faisal/pythonfiles/videos/zoom-wide.mp4',codec='libx264', fps=29)
    
import traceback

try:
    make_center_video()
except Exception as e:
    traceback.print_exc()
    print(f"An error occurred: {e}")

I'm trying to make the zoom video using image but facing

TypeError: must be real number, not NoneType.

It used was to run but I don't remember if I might have updated numpy, ffmpeg, or any thing else that is now causing the error. I have tried the code on python 3.10 and 3.11 and get the same error in both. I was previously running it on python 3.10.

An error occurred: must be real number, not NoneType
Traceback (most recent call last):
  File "/home/faisal/pythonfiles/code/zoom_video.py", line 76, in <module>
    make_center_video()
  File "/home/faisal/pythonfiles/code/zoom_video.py", line 61, in make_center_video
    slide.write_videofile('/home/faisal/pythonfiles/videos/zoom-short.mp4',codec='libx264',  fps=29)
  File "/home/faisal/anaconda3/envs/py-310/lib/python3.10/site-packages/decorator.py", line 232, in fun
    return caller(func, *(extras + args), **kw)
  File "/home/faisal/anaconda3/envs/py-310/lib/python3.10/site-packages/moviepy/decorators.py", line 54, in requires_duration
    return f(clip, *a, **k)
  File "/home/faisal/anaconda3/envs/py-310/lib/python3.10/site-packages/decorator.py", line 232, in fun
    return caller(func, *(extras + args), **kw)
  File "/home/faisal/anaconda3/envs/py-310/lib/python3.10/site-packages/moviepy/decorators.py", line 135, in use_clip_fps_by_default
    return f(clip, *new_a, **new_kw)
  File "/home/faisal/anaconda3/envs/py-310/lib/python3.10/site-packages/decorator.py", line 232, in fun
    return caller(func, *(extras + args), **kw)
  File "/home/faisal/anaconda3/envs/py-310/lib/python3.10/site-packages/moviepy/decorators.py", line 22, in convert_masks_to_RGB
    return f(clip, *a, **k)
  File "/home/faisal/anaconda3/envs/py-310/lib/python3.10/site-packages/moviepy/video/VideoClip.py", line 300, in write_videofile
    ffmpeg_write_video(self, filename, fps, codec,
  File "/home/faisal/anaconda3/envs/py-310/lib/python3.10/site-packages/moviepy/video/io/ffmpeg_writer.py", line 213, in ffmpeg_write_video
    with FFMPEG_VideoWriter(filename, clip.size, fps, codec = codec,
  File "/home/faisal/anaconda3/envs/py-310/lib/python3.10/site-packages/moviepy/video/io/ffmpeg_writer.py", line 88, in __init__
    '-r', '%.02f' % fps,
TypeError: must be real number, not NoneType
jared
  • 4,165
  • 1
  • 8
  • 31
faisal2k
  • 3
  • 3
  • Can you post traceback error? – toyota Supra Aug 12 '23 at 14:08
  • with FFMPEG_VideoWriter(filename, clip.size, fps, codec = codec, File "/home/faisal/anaconda3/envs/py-310/lib/python3.10/site-packages/moviepy/video/io/ffmpeg_writer.py", line 88, in __init__ '-r', '%.02f' % fps, TypeError: must be real number, not NoneType – faisal2k Aug 12 '23 at 14:18
  • There is no line 88. – toyota Supra Aug 12 '23 at 14:25
  • line 88 in ffmpeg_writer.py which is used for compiling – faisal2k Aug 12 '23 at 14:54
  • i have update the code using traceback,you can see and also details of errors – faisal2k Aug 12 '23 at 14:59
  • Some libraries return None when they can't load a file. Given the size of code, and the deep traceback, you probably have the best chance of tracing the problem None back to its source. – hpaulj Aug 12 '23 at 20:18
  • Note that it is specifically complaining about `fps`, which is certainly provided as a parameter. I don't see the issue here yet. – Tim Roberts Aug 13 '23 at 02:03
  • fps is not issue, the issue is with version of numpy and related lib, i cannot pin point the error – faisal2k Aug 13 '23 at 04:31
  • Why do u have duplicated slide = mp.ImageClip(img).set_fps(29).set_duration(audio.duration).resize(size) slide = zoom_in_effect(slide, 0.02) slide.write_videofile('/home/faisal/pythonfiles/videos/zoom-short.mp4',codec='libx264', fps=29)? You should remove this. – toyota Supra Aug 14 '23 at 00:59

0 Answers0