剪下一部分视频——python
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/37317140/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Cutting out a portion of video - python
提问by Pranav Arora
I have videos of length approximately 25 min each and I wish to cut a few seconds from the start using python.
我有每个大约 25 分钟的视频,我希望从一开始就使用 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?
搜索它,我偶然发现了 python 的 moviepy 包。问题是,即使是单个视频也需要花费大量时间。以下是我用来从单个视频的开头剪切 7 秒的代码片段。写入过程消耗大量时间。有没有更好的方法来使用 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!
任何帮助表示赞赏。谢谢!
回答by Zulko
Try this and tell us if it is faster (if it can, it will extract the video directly using ffmpeg, without decoding and reencoding):
试试这个并告诉我们它是否更快(如果可以,它将直接使用 ffmpeg 提取视频,无需解码和重新编码):
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
如果这没有帮助,请查看代码
回答by kibitzforu
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.
此代码中的 t1 和 t2 表示修剪的开始时间和结束时间。t1 之前和 t2 之后的视频将被省略。
回答by Samuel Dauzon
If you are new to moviepyyou should follow these steps.
如果您不熟悉 moviepy,则应遵循以下步骤。
Installation (in your virtualenv) :
安装(在您的 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")