0

I have seen a few questions about this here, but none of them solved the issue for me, so maybe my case is different in some way.

I am trying to achieve a simple result: read a file and write it. Here is the code:

import os
os.environ['FFMPEG_BINARY'] = '/usr/bin/ffmpeg'

from moviepy.editor import VideoFileClip
name = 'test.mp4'

clip = VideoFileClip('./vids/'+name)

clip.write_videofile('./vids/'+name, codec='libx264', fps=30)

This code comes up with an error:

---> 88     '-r', '%.02f' % fps,
     89     '-an', '-i', '-'
     90 ]
     91 if audiofile is not None:
     92     cmd.extend([
     93         '-i', audiofile,
     94         '-acodec', 'copy'
     95     ])

TypeError: must be real number, not NoneType

You may notice that I have set the environment variable for ffmpeg (I have also changed that in configure_defaults.py). This is because it was suggested in other questions. Also based on them I have run the following commands before running the code:

sudo apt -y update
sudo apt -y install ffmpeg
pip install decorator
pip install moviepy --upgrade
pip install ffmpeg --upgrade

I am using a Debian GNU/Linux 10 (buster) machine, and the versions of moviepy and ffmpeg are 1.0.3 and 4.1.10-0+deb10u1 respectively.

Nothing seems to be helping to solve this. What am I missing here?

user9102437
  • 600
  • 1
  • 10
  • 24
  • What happens if you don't set fps? Do you have a debugger, would be interesting to see what's inside those variables right before the error? – DeadSec Jan 26 '23 at 11:58
  • @DeadSec setting or unsetting fps basically does nothing, the error is absolutely the same, it was just one of the suggestions I found on the internet. I run my code in Jupyter Notebook, so I will be able to give you any variables if you tell me which ones you need. I can also make a pastebin of the full error if that will help. – user9102437 Jan 26 '23 at 13:30
  • 1
    You are reading and writing to the same file: `./vids/test.mp4`. Try setting different names for input an output files. Try using full path for video file name instead of relative path. Make sure that `test.mp4` is not corrupted - try to play it in a video player. Try other MP4 file. Place a breakpoint in line 77 of `ffmpeg_writer.py`, use the debugger, and check which value is `None`. Is `fps` = `None`??? For debugging, you may try using other IDE - use PyCharm for example. Setup a clean Virtual Environment in and a new project in a clean folder. – Rotem Jan 27 '23 at 10:10
  • @Rotem Thanks for the suggestions, I have tried all of them and here is what I found: Setting different names to the input and output files and using absolute paths solves absolutely nothing. I have tried a few other videos that I had on my pc (of course, I have tested that they work in a player) and found that some of them get another error, but the issue seems to be the same: `self.fps = infos['video_fps']` gives a `KeyError` inside the library. This particular file has no mentions of fps in `ffmpeg -i filename.mp4`, but others do, so I don't know what to make of this yet. – user9102437 Jan 27 '23 at 19:11
  • @Rotem As for the `None`, I can't install PyCharm where I do this, but it is clear from the error that it refers to the fps variable. I have also double-checked that by modifying the library's files and manually changing fps to 60, but the code still fails when extracting other info from ffmpeg like `video_size`. – user9102437 Jan 27 '23 at 19:14

1 Answers1

0

I got same error

    Traceback (most recent call last):
  File "SampleVidCode.py", line 28, in <module>
    final_video.write_videofile(fps=60, codec="libx264", filename="video.mp4")
  File "C:\ProgramData\Miniconda3\lib\site-packages\decorator.py", line 232, in fun
    return caller(func, *(extras + args), **kw)
  File "C:\ProgramData\Miniconda3\lib\site-packages\moviepy\decorators.py", line 54, in requires_duration
    return f(clip, *a, **k)
  File "C:\ProgramData\Miniconda3\lib\site-packages\decorator.py", line 232, in fun
    return caller(func, *(extras + args), **kw)
  File "C:\ProgramData\Miniconda3\lib\site-packages\moviepy\decorators.py", line 135, in use_clip_fps_by_default
    return f(clip, *new_a, **new_kw)
  File "C:\ProgramData\Miniconda3\lib\site-packages\decorator.py", line 232, in fun
    return caller(func, *(extras + args), **kw)
  File "C:\ProgramData\Miniconda3\lib\site-packages\moviepy\decorators.py", line 22, in convert_masks_to_RGB
    return f(clip, *a, **k)
  File "C:\ProgramData\Miniconda3\lib\site-packages\moviepy\video\VideoClip.py", line 300, in write_videofile
    ffmpeg_write_video(self, filename, fps, codec,
  File "C:\ProgramData\Miniconda3\lib\site-packages\moviepy\video\io\ffmpeg_writer.py", line 213, in ffmpeg_write_video
    with FFMPEG_VideoWriter(filename, clip.size, fps, codec = codec,
  File "C:\ProgramData\Miniconda3\lib\site-packages\moviepy\video\io\ffmpeg_writer.py", line 88, in __init__
    '-r', '%.02f' % fps,
TypeError: must be real number, not NoneType

Then I noticed that "decorators" package was causing the issue. So, first I uninstalled it

pip uninstall decorator

Then, I installed 'moviepy' again:

pip install moviepy

It gave this message:

    Installing collected packages: decorator
      Attempting uninstall: decorator
        Found existing installation: decorator 5.1.1
        Uninstalling decorator-5.1.1:
          Successfully uninstalled decorator-5.1.1
....
Successfully installed decorator-4.4.2

I don't see a good logic there but that solved problem for me:

Moviepy - Building video video.mp4.
MoviePy - Writing audio in videoTEMP_MPY_wvf_snd.mp3
MoviePy - Done.
Moviepy - Writing video video.mp4

Moviepy - Done !
Moviepy - video ready video.mp4