Ffmpeg-python: programatically concat multiple trimmed videos

Created on 13 Apr 2020  路  1Comment  路  Source: kkroening/ffmpeg-python

Hi!
Wanted to make a video out of multiple trimmed input videos (same video of input)
I'm trying this:

import ffmpeg


inp = ffmpeg.input('video.mkv')

out = ffmpeg.concat(
    *[
        inp.video.trim(start=50, end=51).setpts('PTS-STARTPTS'),
        inp.audio.filter('atrim', start=50, end=51).filter('asetpts', expr='PTS-STARTPTS')
    ],
    v=1, a=1
).output('out.mkv')


out.run()

this does work for trimming video and audio and creating an output, but if I wanted to make a loop on this portion of video using list multiplication:

out = ffmpeg.concat(
    *[
        inp.video.trim(start=50, end=51).setpts('PTS-STARTPTS'),
        inp.audio.filter('atrim', start=50, end=51).filter('asetpts', expr='PTS-STARTPTS')
    ]*2,
    v=1, a=1
).output('out.mkv')

I got:

ValueError: Encountered setpts('PTS-STARTPTS') <11a03426e6b6> with multiple outgoing edges with same upstream label None; a `split` filter is probably required

as I was reading in docs, using filter_multi_output allow us to have multiple streams of the same input, so I tried with something simple:

import ffmpeg

inp = ffmpeg.input('video.mkv').filter_multi_output('split')

out = ffmpeg.concat(
    *[
        inp.stream(0).video.trim(start=10, end=11).setpts('PTS-STARTPTS'),
        inp.stream(0).audio.filter('atrim', start=10, end=11).filter('asetpts',expr='PTS-STARTPTS')
    ],
    v=1, a=1
).output('out.mkv')

out.run()

but I got the same error.

How can I achieve this ?

Most helpful comment

nevermind I figured it out
thanks, you guys are my heros

import ffmpeg
import random

inp = ffmpeg.input('video.mkv')

# just split over video and audio, audio SHOULD use asplit instead of split
vid = inp.video.filter_multi_output('split')
aud = inp.audio.filter_multi_output('asplit')


# this is just for acumulating the trimmed videos
outs = []

# 500 pieces of videos / audios
for k in range(500):
    x = random.randint(10,500)
    v = vid[k].trim(start=x, end=x+0.05).setpts('PTS-STARTPTS')
    a = aud[k].filter('atrim', start=x, end=x+0.05).filter('asetpts',expr='PTS-STARTPTS')
    outs.extend((v, a)) 

out = ffmpeg.concat(
    *outs,
    v=1,
    a=1 
).output('out.mkv')

out = ffmpeg.overwrite_output(out)
out.run()

thanks for making such a nice project :+1:

>All comments

nevermind I figured it out
thanks, you guys are my heros

import ffmpeg
import random

inp = ffmpeg.input('video.mkv')

# just split over video and audio, audio SHOULD use asplit instead of split
vid = inp.video.filter_multi_output('split')
aud = inp.audio.filter_multi_output('asplit')


# this is just for acumulating the trimmed videos
outs = []

# 500 pieces of videos / audios
for k in range(500):
    x = random.randint(10,500)
    v = vid[k].trim(start=x, end=x+0.05).setpts('PTS-STARTPTS')
    a = aud[k].filter('atrim', start=x, end=x+0.05).filter('asetpts',expr='PTS-STARTPTS')
    outs.extend((v, a)) 

out = ffmpeg.concat(
    *outs,
    v=1,
    a=1 
).output('out.mkv')

out = ffmpeg.overwrite_output(out)
out.run()

thanks for making such a nice project :+1:

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Matttman picture Matttman  路  6Comments

ghost picture ghost  路  3Comments

Sangkwun picture Sangkwun  路  4Comments

tossorrow picture tossorrow  路  6Comments

kishaningithub picture kishaningithub  路  5Comments