使用moviepy软件包(Python)发布
我正在使用ffmpeg_extract_subclip函数从moviepy处理视频文件。但是,我得到的视频剪辑在开始时间和结束时间之间的长度不同。例如,写作:使用moviepy软件包(Python)发布,python,python-3.x,ffmpeg,video-processing,video-editing,Python,Python 3.x,Ffmpeg,Video Processing,Video Editing,我正在使用ffmpeg_extract_subclip函数从moviepy处理视频文件。但是,我得到的视频剪辑在开始时间和结束时间之间的长度不同。例如,写作: from moviepy.video.io.ffmpeg_tools import ffmpeg_extract_subclip clip=clip_filename cutclip="cutvideo.avi" ffmpeg_extract_subclip(clip_filename, 0, 10, targetn
from moviepy.video.io.ffmpeg_tools import ffmpeg_extract_subclip
clip=clip_filename
cutclip="cutvideo.avi"
ffmpeg_extract_subclip(clip_filename, 0, 10, targetname=cutclip)
我得到一个长度为10,03或类似的视频(就帧数而言,我得到的是602帧,而不是600帧)。
有没有办法获得更精确的输出?嗯……我将把答案和ffmpeg\u extract\u subclip的实际实现合并起来
():
因此,正如您所看到的,该库是非常无状态的,因此可以很容易地进行扩展:
def ffmpeg_extract_subclip_precisely(filename, t1, t2, targetname=None):
""" Makes a new video file playing video file ``filename`` between
the times ``t1`` and ``t2``. """
name, ext = os.path.splitext(filename)
if not targetname:
T1, T2 = [int(1000*t) for t in [t1, t2]]
targetname = "%sSUB%d_%d.%s" % (name, T1, T2, ext)
cmd = [get_setting("FFMPEG_BINARY"), "-i", filename,
"-force_key_frames", "{}:{}".format(t1,t2), "temp_out.mp4"]
subprocess_call(cmd)
cmd = [get_setting("FFMPEG_BINARY"),"-y",
"-ss", "%0.2f"%t1,
"-i", "temp_out.mp4",
"-t", "%0.2f"%(t2-t1),
"-map", "0", "-vcodec", "copy", "-acodec", "copy", targetname]
subprocess_call(cmd)
顺便说一句,我相信你应该尽可能精确地指定时间
请注意以上代码未经测试
def ffmpeg_extract_subclip_precisely(filename, t1, t2, targetname=None):
""" Makes a new video file playing video file ``filename`` between
the times ``t1`` and ``t2``. """
name, ext = os.path.splitext(filename)
if not targetname:
T1, T2 = [int(1000*t) for t in [t1, t2]]
targetname = "%sSUB%d_%d.%s" % (name, T1, T2, ext)
cmd = [get_setting("FFMPEG_BINARY"), "-i", filename,
"-force_key_frames", "{}:{}".format(t1,t2), "temp_out.mp4"]
subprocess_call(cmd)
cmd = [get_setting("FFMPEG_BINARY"),"-y",
"-ss", "%0.2f"%t1,
"-i", "temp_out.mp4",
"-t", "%0.2f"%(t2-t1),
"-map", "0", "-vcodec", "copy", "-acodec", "copy", targetname]
subprocess_call(cmd)