Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extract part of a video using ffmpeg_extract_subclip - black frames

Tags:

python

ffmpeg

I'm trying to use: "ffmpeg_extract_subclip" for extracting part of a video.

And I'm facing a few problems:

1.when I'm cutting a small video (1-3seconds) I'm getting black frames, only audio is working. 2.when I'm cutting longer video, the output video is stuck 2-3 seconds before the end.

This is my simple code:

from moviepy.video.io.ffmpeg_tools import ffmpeg_extract_subclip

input_video_path = 'myPath/vid1.mp4'
output_video_path = 'myPath/output/vid1.mp4'
t1 = 6.5
t2 = 16    # random numbers, my last attempt..
    
ffmpeg_extract_subclip(input_video_path, t1, t2, targetname=output_video_path)

I tried to look inside the code: ffmpeg_extract_subclip Function

But still couldn't understand what's wrong.. :(

I'm still trying, and if anyone knows the problem or have a different approach, that will be amazing.

Thanks a lot for your help!

like image 762
albert1905 Avatar asked Sep 10 '18 12:09

albert1905


2 Answers

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.

like image 181
Vitalii Volkov Avatar answered Oct 10 '22 05:10

Vitalii Volkov


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

like image 7
princelySid Avatar answered Oct 10 '22 04:10

princelySid