Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature stop processing #2077

Closed
wants to merge 11 commits into from

Conversation

hossien014
Copy link

#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)
  • I have added suitable tests demonstrating a fixed bug or new/changed feature to the test suite in tests/
  • I have properly documented new or changed features in the documentation or in the docstrings
  • I have properly explained unusual or unexpected code in the comments around it

@hossien014 hossien014 changed the title Feature stop processing #2074 Feature stop processing Dec 6, 2023
@OsaAjani OsaAjani added feature New addition to the API i.e. a new class, method or parameter. multithreading a label for OsaAjani to easily find old PR he might use in multithreading migration labels Dec 6, 2024
@OsaAjani
Copy link
Collaborator

OsaAjani commented Dec 6, 2024

Closing for now as part of the ongoing housekeeping effort, I will make sure to take a look on the ideas for the intended future multithreading migration.

@OsaAjani OsaAjani closed this Dec 6, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature New addition to the API i.e. a new class, method or parameter. multithreading a label for OsaAjani to easily find old PR he might use in multithreading migration
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants