0

I need to extract multiple segments based on TS from a video using ffmpeg-python.

def trim(input_path, output_path, start, end):
    input_stream = ffmpeg.input(input_path)

    vid = (
        input_stream.video
        .trim(start=start, end=end)
        .setpts('PTS-STARTPTS')
    )
    aud = (
        input_stream.audio
        .filter_('atrim', start=start, end=end)
        .filter_('asetpts', 'PTS-STARTPTS')
    )

    joined = ffmpeg.concat(vid, aud, v=1, a=1).node
    output = ffmpeg.output(joined[0], joined[1], output_path)
    output.run()

The above code indeed extracts a segment from "start" to "end" with audio and saves the result in output_path.

But what if we want to extract multiple segments? Of course we can call the above function once for every segment but for the way ffmpeg works I suspect this is not efficient at all.

Is there a way in ffmpeg-python to specify a list of tuples (start, ends) and extract all the desired segments in one go?

Mauro Gentile
  • 1,463
  • 6
  • 26
  • 37

0 Answers0