How can I efficiently cut out part of a video?

Question:

I want to remove the first few seconds from a video that’s about 25 minutes long. I found the moviepy package, and tried writing this code:

from moviepy.editor import *
clip = VideoFileClip("video1.mp4").cutout(0, 7)
clip.write_videofile("test.mp4")

However, it’s very slow even for a single video. Is there a faster way to do this in Python?

Asked By: Pranav Arora

||

Answers:

Try this and tell us if it is faster (if it can, it will extract the video directly using ffmpeg, without decoding and reencoding):

from moviepy.video.io.ffmpeg_tools import ffmpeg_extract_subclip
ffmpeg_extract_subclip("video1.mp4", start_time, end_time, targetname="test.mp4")

If that doesn’t help, have a look at the code

Answered By: Zulko
from moviepy.video.io.ffmpeg_tools import ffmpeg_extract_subclip
ffmpeg_extract_subclip("video1.mp4", t1, t2, targetname="test.mp4")

t1 and t2 in this code represent the start time and end time for trimming. Video before t1 and after t2 will be omitted.

Answered By: kibitzforu

If you are new to moviepy you should follow these steps.

Installation :

pip install --trusted-host pypi.python.org moviepy
pip install imageio-ffmpeg

Installation (in your virtualenv) version for old systems :

pip install --trusted-host pypi.python.org moviepy
python
import imageio
imageio.plugins.ffmpeg.download()

After these commands, you have the minimal software requirements.

Usage

from moviepy.video.io.ffmpeg_tools import ffmpeg_extract_subclip
# ffmpeg_extract_subclip("full.mp4", start_seconds, end_seconds, targetname="cut.mp4")
ffmpeg_extract_subclip("full.mp4", 60, 300, targetname="cut.mp4")
Answered By: Samuel Dauzon

The ffmpeg_extract_subclip did not produce correct results for me for some videos. The following code from this link worked though.

# Import everything needed to edit video clips
from moviepy.editor import *

# loading video gfg
clip = VideoFileClip("geeks.mp4")
# getting only first 5 seconds
clip = clip.subclip(0, 5)
# showing clip
clip.ipython_display(width = 360)

Then you can save the clip as follows:

clip.write_videofile("clip.mp4")
Answered By: MRM
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.