1

How can I cut a video to a certain length and add an intro video to it using ffmpeg-python?

I am doing this:

intro = ffmpeg.input(intro)
mainvid = ffmpeg.input(mainvid)

v1 = intro.video
a1 = intro.audio
v2 = mainvid.video
a2 = mainvid.audio

joined = ffmpeg.concat(v1, a1, v2, a2, v=1, a=1).node
v3 = joined[0]
a3 = joined[1]

(
    ffmpeg
    .output(
        v3,
        a3,
        'out.mkv',
        vcodec='libx265', )
    .run()
)

But I don’t know how to cut *mainvid to a certain length like 10 minutes before joining. I know ss will help, but I don’t know how to use it for only mainvid.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
kup
  • 731
  • 5
  • 18

2 Answers2

1

You may use trim and atrim filters for trimming the video and the audio.
The suggested solution is based on the following answer.

  • FFmpeg supports 3 types of concatenation: "Concat demuxer", "Concat protocol" and "Concat filter".
    The method ffmpeg.concat applies Concat filter.
    Since "Concat filter" is used, a practical solution is trimming the video and audio by "chaining" trim and atrim filters.
  • setpts and asetpts filters are required for fixing the timestamps.

Replace v2 = mainvid.video and a2 = mainvid.audio with:

v2 = mainvid.video.filter('trim', start=0, end=600).filter('setpts', 'PTS-STARTPTS')
a2 = mainvid.audio.filter('atrim', start=0, end=600).filter('asetpts', 'PTS-STARTPTS')
Rotem
  • 30,366
  • 4
  • 32
  • 65
1

I managed to do it using:

        intro = ffmpeg.input(intro)
        mainvid = ffmpeg.input(mainvid, ss='00:00:00', t='00:10:00') 

        v1 = intro.video
        a1 = intro.audio
        v2 = mainvid.video
        a2 = mainvid.audio

        joined = ffmpeg.concat(v1, a1, v2, a2, v=1, a=1).node
        v3 = joined[0]
        a3 = joined[1]

        (
            ffmpeg
            .output(
                v3,
                a3,
                'out.mkv', 
                vcodec='libx265', )
            .run()
        )
kup
  • 731
  • 5
  • 18