Domanda

Is there an easy way to trim a video file in python. If I know the start time and end time, I want to save the new file as multiple shorter files based on input.

example pseudo code:

function cut_video(original_vid, start, stop):
    original_vid.start_time(start)
    original_vid.end_time(stop)
    original_vid.trim() #based on times
    new_vid = original_vid
    return new_vid
È stato utile?

Soluzione

Found a solution:

def get_ffmpeg_bin():
    ffmpeg_dir = helper_functions.get_ffmpeg_dir_path()
    FFMPEG_BIN = os.path.join(ffmpeg_dir, "ffmpeg.exe")
    return FFMPEG_BIN


def split_vid_from_path(video_file_path, start_time, durration):
    ffmpeg_binary =  get_ffmpeg_bin()
    output_file_name = get_next_file_name(video_file_path)
    pipe = sp.Popen([ffmpeg_binary,"-v", "quiet", "-y", "-i", video_file_path, "-vcodec", "copy", "-acodec", "copy",
                 "-ss", start_time, "-t", durration, "-sn", output_file_name ])


    pipe.wait()
    return True


sample_vid = os.path.join(get_sample_vid_dir_path(), "Superman-01-The_Mad_Scientist.mp4")
split_vid_from_path(sample_vid, "00:00:00", "00:00:17")

https://www.ffmpeg.org/ffmpeg.html -> this documentation allows you to add your own custom flags to your ffmpeg wrapper.

Something to note, you may want to verify the user is giving you valid data.

Altri suggerimenti

A nice module to do these kids of things is moviepy.

Example Code:

from moviepy.editor import *

video = VideoFileClip("myHolidays.mp4").subclip(50,60)

# Make the text. Many more options are available.
txt_clip = ( TextClip("My Holidays 2013",fontsize=70,color='white')
             .set_position('center')
             .set_duration(10) )

result = CompositeVideoClip([video, txt_clip]) # Overlay text on video
result.write_videofile("myHolidays_edited.webm",fps=25) # Many options...

The documentation of this module can be found at https://pypi.org/project/moviepy/

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top