Zulko / moviepy

Video editing with Python
https://zulko.github.io/moviepy/
MIT License
12.07k stars 1.51k forks source link

Can't save gif #2122

Closed NiKiTjAOFF closed 4 months ago

NiKiTjAOFF commented 4 months ago

After I use these lines of code:

from moviepy.editor import VideoFileClip
video_path = SOURCE_DIRECTORY_PATH + "\\" + video_name_with_extension
gif_path = SOURCE_DIRECTORY_PATH + "\\" + video_name + ".gif"
clip = VideoFileClip(video_path)
clip.write_gif(gif_path)

It proceeds the mp4 video, saves gif with 0 bytes and continueing to save something somewhere, so that after 30s my disk space is shortened for 1gb. After I restart my pc the space's restored to as it was before.

VideoFileClip actually contains video.

After I interrupt execution I get:

Exception ignored in: <function LegacyWriter.__del__ at 0x0000021F60F273A0>
Traceback (most recent call last):
  File "C:\Users\nikit\Всякое\MyProjects\converter\convert\lib\site-packages\imageio\v2.py", line 200, in __del__
    self.close()
  File "C:\Users\nikit\Всякое\MyProjects\converter\convert\lib\site-packages\imageio\v2.py", line 190, in close
    self.instance.close()
  File "C:\Users\nikit\Всякое\MyProjects\converter\convert\lib\site-packages\imageio\plugins\pillow.py", line 144, in close
    self._flush_writer()
  File "C:\Users\nikit\Всякое\MyProjects\converter\convert\lib\site-packages\imageio\plugins\pillow.py", line 485, in _flush_writer
    primary_image.save(self._request.get_file(), **self.save_args)
  File "C:\Users\nikit\Всякое\MyProjects\converter\convert\lib\site-packages\PIL\Image.py", line 2439, in save
    save_handler(self, fp, filename)
  File "C:\Users\nikit\Всякое\MyProjects\converter\convert\lib\site-packages\PIL\GifImagePlugin.py", line 704, in _save_all
    _save(im, fp, filename, save_all=True)
  File "C:\Users\nikit\Всякое\MyProjects\converter\convert\lib\site-packages\PIL\GifImagePlugin.py", line 715, in _save
    if not save_all or not _write_multiple_frames(im, fp, palette):
  File "C:\Users\nikit\Всякое\MyProjects\converter\convert\lib\site-packages\PIL\GifImagePlugin.py", line 602, in _write_multiple_frames
    im_frame = _normalize_mode(im_frame.copy())
  File "C:\Users\nikit\Всякое\MyProjects\converter\convert\lib\site-packages\PIL\GifImagePlugin.py", line 492, in _normalize_mode
    im = im.convert("P", palette=Image.Palette.ADAPTIVE)
  File "C:\Users\nikit\Всякое\MyProjects\converter\convert\lib\site-packages\PIL\Image.py", line 1029, in convert
    im = self.im.quantize(colors)
KeyboardInterrupt:
Exception ignored in: <function PluginV3.__del__ at 0x0000021F60F0F160>
Traceback (most recent call last):
  File "C:\Users\nikit\Всякое\MyProjects\converter\convert\lib\site-packages\imageio\core\v3_plugin_api.py", line 370, in __del__
    self.close()
  File "C:\Users\nikit\Всякое\MyProjects\converter\convert\lib\site-packages\imageio\plugins\pillow.py", line 144, in close
    self._flush_writer()
  File "C:\Users\nikit\Всякое\MyProjects\converter\convert\lib\site-packages\imageio\plugins\pillow.py", line 485, in _flush_writer
    primary_image.save(self._request.get_file(), **self.save_args)
  File "C:\Users\nikit\Всякое\MyProjects\converter\convert\lib\site-packages\PIL\Image.py", line 2439, in save
    save_handler(self, fp, filename)
  File "C:\Users\nikit\Всякое\MyProjects\converter\convert\lib\site-packages\PIL\GifImagePlugin.py", line 704, in _save_all
    _save(im, fp, filename, save_all=True)
  File "C:\Users\nikit\Всякое\MyProjects\converter\convert\lib\site-packages\PIL\GifImagePlugin.py", line 715, in _save
    if not save_all or not _write_multiple_frames(im, fp, palette):
  File "C:\Users\nikit\Всякое\MyProjects\converter\convert\lib\site-packages\PIL\GifImagePlugin.py", line 602, in _write_multiple_frames
    im_frame = _normalize_mode(im_frame.copy())
  File "C:\Users\nikit\Всякое\MyProjects\converter\convert\lib\site-packages\PIL\GifImagePlugin.py", line 492, in _normalize_mode
    im = im.convert("P", palette=Image.Palette.ADAPTIVE)
  File "C:\Users\nikit\Всякое\MyProjects\converter\convert\lib\site-packages\PIL\Image.py", line 1029, in convert
    im = self.im.quantize(colors)
KeyboardInterrupt:

My setup: Windows 10, python 3.9, venv, modeules of moviepy:

pip install moviepy
Requirement already satisfied: moviepy in c:\users\nikit\всякое\myprojects\converter\convert\lib\site-packages (1.0.3)
Requirement already satisfied: decorator<5.0,>=4.0.2 in c:\users\nikit\всякое\myprojects\converter\convert\lib\site-packages (from moviepy) (4.4.2)
Requirement already satisfied: tqdm<5.0,>=4.11.2 in c:\users\nikit\всякое\myprojects\converter\convert\lib\site-packages (from moviepy) (4.66.2)
Requirement already satisfied: requests<3.0,>=2.8.1 in c:\users\nikit\всякое\myprojects\converter\convert\lib\site-packages (from moviepy) (2.31.0)
Requirement already satisfied: proglog<=1.0.0 in c:\users\nikit\всякое\myprojects\converter\convert\lib\site-packages (from moviepy) (0.1.10)
Requirement already satisfied: numpy>=1.17.3 in c:\users\nikit\всякое\myprojects\converter\convert\lib\site-packages (from moviepy) (1.26.4)
Requirement already satisfied: imageio<3.0,>=2.5 in c:\users\nikit\всякое\myprojects\converter\convert\lib\site-packages (from moviepy) (2.34.0)
Requirement already satisfied: imageio-ffmpeg>=0.2.0 in c:\users\nikit\всякое\myprojects\converter\convert\lib\site-packages (from moviepy) (0.4.9)
Requirement already satisfied: pillow>=8.3.2 in c:\users\nikit\всякое\myprojects\converter\convert\lib\site-packages (from imageio<3.0,>=2.5->moviepy) (10.2.0)
Requirement already satisfied: setuptools in c:\users\nikit\всякое\myprojects\converter\convert\lib\site-packages (from imageio-ffmpeg>=0.2.0->moviepy) (49.2.1)
Requirement already satisfied: charset-normalizer<4,>=2 in c:\users\nikit\всякое\myprojects\converter\convert\lib\site-packages (from requests<3.0,>=2.8.1->moviepy) (3.3.2)
Requirement already satisfied: idna<4,>=2.5 in c:\users\nikit\всякое\myprojects\converter\convert\lib\site-packages (from requests<3.0,>=2.8.1->moviepy) (3.6)
Requirement already satisfied: urllib3<3,>=1.21.1 in c:\users\nikit\всякое\myprojects\converter\convert\lib\site-packages (from requests<3.0,>=2.8.1->moviepy) (2.2.1)
Requirement already satisfied: certifi>=2017.4.17 in c:\users\nikit\всякое\myprojects\converter\convert\lib\site-packages (from requests<3.0,>=2.8.1->moviepy) (2024.2.2)
Requirement already satisfied: colorama in c:\users\nikit\всякое\myprojects\converter\convert\lib\site-packages (from tqdm<5.0,>=4.11.2->moviepy) (0.4.6)

pls help, I followed tutorial how to convert mp4 to gif: https://www.youtube.com/watch?v=mheEk_XyAv8 and this happened

keikoro commented 4 months ago

Please always include your specs like we ask for in our issue templates – MoviePy version, platform used etc. – to help pinpoint what causes your problem, thanks.

NiKiTjAOFF commented 4 months ago

Please always include your specs like we ask for in our issue templates – MoviePy version, platform used etc. – to help pinpoint what causes your problem, thanks.

They're already in there

NiKiTjAOFF commented 4 months ago

After I ran the following code on collab:

from moviepy.editor import *
clip =VideoFileClip("2024-02-25 23-53-31.mp4")
clip.write_gif("2024-02-25 23-53-31.gif")

It gave me this warn:

MoviePy - Building file 2024-02-25 23-53-31.gif with imageio.
t: 100%|█████████▉| 2322/2324 [00:09<00:00, 277.26it/s, now=None]WARNING:py.warnings:/usr/local/lib/python3.10/dist-packages/moviepy/video/io/ffmpeg_reader.py:123: UserWarning: Warning: in file 2024-02-25 23-53-31.mp4, 691200 bytes wanted but 0 bytes read,at frame 2323/2324, at time 38.72/38.72 sec. Using the last valid frame instead.
  warnings.warn("Warning: in file %s, "%(self.filename)+
NiKiTjAOFF commented 4 months ago

Problem solved. Make sure ffmpeg.exe is in PATH, so that the following code could work. This code not only converts all the mp4 videos inside of a folder, but also changes the speed of a video, it's fps and res without loosing any quality. Also you can turn on lantzoc filtering.

from moviepy.editor import *
from typing import List
import os

def list_image_files(directory: str) -> List[str]:
    image_extensions = [".mp4"]
    image_files = [f for f in os.listdir(directory) if os.path.splitext(f)[1].lower() in image_extensions]
    return image_files

SOURCE_DIRECTORY_PATH = r"videos"
list = list_image_files(SOURCE_DIRECTORY_PATH)
print(list)
height = "640"
speedX = 0.5
duration = 2
lanczos_is_off = False
for video_name_with_extension in list:
    video_name = video_name_with_extension.split('.')[0]
    video_path = SOURCE_DIRECTORY_PATH + "\\" + video_name_with_extension
    gif_path = SOURCE_DIRECTORY_PATH + "\\" + video_name + ".gif"
    print(video_path)
    print(gif_path)
    video = VideoFileClip(video_path)
    fps = video.duration / duration
    if lanczos_is_off:
        command = f'ffmpeg -i "{video_path}" -vf "fps={fps},setpts={speedX}*PTS,scale={height}:-1" -c:v gif "{gif_path}" -y'
    else :
        command = f'ffmpeg -i "{video_path}" -vf "fps={fps},setpts={speedX}*PTS,scale={height}:-1:flags=lanczos,split[s0][s1];[s0]palettegen[p];[s1][p]paletteuse=dither=none" -c:v gif "{gif_path}" -y'
    os.system(command)
keikoro commented 4 months ago

They're already in there

Thanks, I didn't see that because I scan for them at the bottom of an issue when I tag it.

Problem solved.

If this means this issue is solved, please don't forget to close it. Thank you.