Extract part of a video using ffmpeg_extract_subclip - black frames

try to use moviepy.video.io.VideoFileClip:

from moviepy.video.io.VideoFileClip import VideoFileClip

input_video_path = 'myPath/vid1.mp4'
output_video_path = 'myPath/output/vid1.mp4'

with VideoFileClip(input_video_path) as video:
    new = video.subclip(t1, t2)
    new.write_videofile(output_video_path, audio_codec='aac')

It works fine for me. aah audio codec is important for Safari and some Mac OS video players.


There's a fix in the master for moviepy here however it's yet to update to the pip index. So what I used the code below to bring it into my program

from moviepy.tools import subprocess_call
from moviepy.config import get_setting

def ffmpeg_extract_subclip(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"),"-y",
           "-ss", "%0.2f"%t1,
           "-i", filename,
           "-t", "%0.2f"%(t2-t1),
           "-vcodec", "copy", "-acodec", "copy", targetname]

    subprocess_call(cmd)

you can then call is as a normal function. This requires that you have moviepy and it's dependencies already installed

Tags:

Python

Ffmpeg