Zulko / moviepy

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

Feature stop processing #2077

Open hossien014 opened 7 months ago

hossien014 commented 7 months ago

2074

Why did I make these changes?

I was building an application that allows a user to edit a video and get a video as output and end the video creation process whenever needed with a button. But Moviepy couldn't stop the video creation process, so I decided to apply these changes.

I added a function called "stop_processing_video" in utils. This function takes a "filename" argument, and this is the name you can pass to the "write_videofile" function. This function stops the video creation process and deletes temporary files.

how it works

I added a list in the module "ffmpeg_writer.py" named "VIDEOS_TO_STOP". If the name of the video being processed is included in this list, the processing of that video will be stopped by the function "ffmpeg_write_video". In the "ffmpeg_write_video" function, the following condition is placed, which stops the video processing.

 if VIDEOS_TO_STOP[0] is not None:
                if filename in VIDEOS_TO_STOP:
                    logger(
                        message="""MoviePy - process stoped in ffmpeg_write_video with
                    -> utls.stop_processing_video()."""
                    )
                    VIDEOS_TO_STOP.pop(VIDEOS_TO_STOP.index(filename))
                    return "canceled"
                if len(VIDEOS_TO_STOP) == 1:
                    VIDEOS_TO_STOP[0] = None

I did the same in the "ffmpeg_audiowriter.py" modules.

Example

In this example, I start processing 3 videos simultaneously and then stop processing video 1 and video 3 using the stop_processing_video function.

import threading
import time

from moviepy import VideoFileClip
from moviepy.utils import stop_processing_video

def edit_video(output):
    """Just to start some process"""
    vid = VideoFileClip("media/sintel_with_14_chapters.mp4")
    vid.write_videofile(output)

vid1_filename = "examples/first_vid.mp4"
vid2_filename = "second_vid.mp4"
vid3_filename = "third_vid.mp4"

t1 = threading.Thread(target=edit_video, args=[vid1_filename])
t2 = threading.Thread(target=edit_video, args=[vid2_filename])
t3 = threading.Thread(target=edit_video, args=[vid3_filename])

t1.start()
t2.start()
t3.start()

time.sleep(1)
stop_processing_video(vid1_filename)
# stop_processing_video(vid2_filename)
stop_processing_video(vid3_filename)