Majid Abdolhosseini
Majid Abdolhosseini

Reputation: 2301

python cut portion of video "Fastest way"

I'm trying to cut out portion of video file using python(3.7.1) and ffmpeg in my flask (1.0.2) application, this is solution 1

    # solution 1
    from moviepy.editor import *
    from moviepy.video.io.ffmpeg_tools import ffmpeg_extract_subclip

    video = VideoFileClip('/app/videos/video.mkv'.subclip(10, 20)
    video.write_videofile('/app/videos/cutted_video.mp4')

and here is result in flower panel screenshot. as you can see cutting out two videos takes more than two seconds. enter image description here and this is solution 2

    # solution 2
    from moviepy.editor import *
    from moviepy.video.io.ffmpeg_tools import ffmpeg_extract_subclip


    ffmpeg_extract_subclip(
        '/app/videos/video.mkv',
        10,
        20,
        '/app/videos/cutted_video.mp4'
    )

first solution works pretty well, but It takes about 1-2 seconds to cut the video out. instead second solution works very fast ( less than 0.5 sec ) but the output video is just audio plus black screen.

what is the fastest way to cut portion of video in python. If there is any other library which is faster please tell me that.

Upvotes: 1

Views: 7940

Answers (1)

Soorena
Soorena

Reputation: 4442

I use this method and It's pretty fast:

from moviepy.editor import VideoFileClip

clip = VideoFileClip("sample.mp4").subclip(start, end)
clip.to_videofile(outputfile, codec="libx264", temp_audiofile='temp-audio.m4a', remove_temp=True, audio_codec='aac')

Upvotes: 4

Related Questions