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!
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With