Cutting out a portion of video - python

余生长醉 提交于 2019-12-03 06:23:05

问题


I have videos of length approximately 25 min each and I wish to cut a few seconds from the start using python.

Searching about it, I stumbled upon the moviepy package for python. The problem is, it takes up a lot of time even for a single video. Following is the code snippet I use to cut 7 seconds from the start of a single video. The write process consumes a lot of time. Is there a better way to cut the videos using python?

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

Please let me know if I have missed out any details.

Any help is appreciated. Thanks!


回答1:


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




回答2:


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.




回答3:


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

Installation (in your virtualenv) :

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")



回答4:


Loved this answer: `enter code herefrom 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")`

However, I am getting the error:

[mp4 @ 06624300] Could not find tag for codec pcm_s16be in stream #1, codec not 
currently supported in container
Could not write header for output file #0 (incorrect codec parameters ?): Invalid 
argument

Is there a way to take insert a different codec, or remove it completely?



来源:https://stackoverflow.com/questions/37317140/cutting-out-a-portion-of-video-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!