69

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?

Karl Knechtel
  • 62,466
  • 11
  • 102
  • 153
Pranav Arora
  • 993
  • 2
  • 10
  • 16
  • Writing the video probably means that the library has to re-encode it, resulting in a loss of quality and poor performance. It *is* possible to cut away from a MP4 video stream without re-encoding, but it's not as simple as you seem to think it is, and you can't cut at arbitrary points (only right before keyframes). I also don't know if moviepy can do this. – Colin Emonds May 19 '16 at 08:51
  • I agree with @cemper93. MoviePy always decode your frames (to Numpy arrays) and reencode them at write time, which takes time. The simplest way to cut without reencoding is to use FFMPEG to extract the right segment from the mp4. You can do it directly at the command line, or from Python, using the subprocess library to call ffmpeg. – Zulko May 19 '16 at 11:17
  • @Zulko: Can you write that as an answer and add a bit more explanation? I have no idea about FFMPEG and the subprocess library you are talking about. :/ – Pranav Arora May 19 '16 at 11:45

4 Answers4

128

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

Zain Rizvi
  • 23,586
  • 22
  • 91
  • 133
Zulko
  • 3,540
  • 1
  • 22
  • 18
16

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")
Samuel Dauzon
  • 10,744
  • 13
  • 61
  • 94
  • Btw, if you are looking to disable the output of `ffmpeg_extract_subclip` that not possible with the current version. You should install moviepy from the [git](https://github.com/Zulko/moviepy) repo because they actually recently added that option. – Bendemann Apr 19 '21 at 05:18
5
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.

kibitzforu
  • 383
  • 4
  • 10
4

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")
MRM
  • 1,099
  • 2
  • 12
  • 29