kkroening / ffmpeg-python

Python bindings for FFmpeg - with complex filtering support
Apache License 2.0
9.88k stars 884 forks source link

Errors with trim and concat doesn't work: why? #324

Open Saturnix opened 4 years ago

Saturnix commented 4 years ago

Apologies if this is trivial but I'm new to FFmpeg and I'm trying to follow the documentation but it appears as if it's written in another language.

I have two files, in.mp4 and outro.mp4.

I need a very simple thing: trim in.mp4 to an arbitrary lenght, and concat outro.mp4 after it.

I can do that without the trimming part

    import ffmpeg

    main_video = ffmpeg.input('in.mp4')
    outro = ffmpeg.input('outro.mp4')

    v1 = main_video.video.filter("scale", size='hd720')
    a1 = main_video.audio
    v2 = outro.video
    a2 = outro.audio

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

    out = ffmpeg.output(v3, a3, 'out.mp4')
    out.run()

But if I add the trimming here:

main_video = ffmpeg.input('in.mp4').trim(start_frame=0, end_frame=1000)

I get this error:

ValueError: Encountered trim(end_frame=1000, start_frame=0) <17c7b86357ec> with multiple outgoing edges with same upstream label None; a split filter is probably required

and if I try to do it here:

v1 = main_video.video.filter("scale", size='hd720').trim(start_frame=0, end_frame=1000)
a1 = main_video.audio.trim(start_frame=0, end_frame=1000)

I get these errors:

[Parsed_trim_2 @ 00000295587ca840] Media type mismatch between the 'Parsed_trim_2' filter output pad 0 (video) and the 'Parsed_concat_3' filter input pad 1 (audio)
[AVFilterGraph @ 00000295587cc800] Cannot create the link trim:0 -> concat:1

How can I solve this?

bidspells commented 4 years ago

.. one reason is: your audio needs an 'atrim' filter rather than 'trim'. 'atrim' is not part of the API so you need to call .filter('atrim', ...). The atrim filter does not accept frames but samples. You need to use times to trim video und audio in sync.

My experience: debug a filter chain on the command line and the translate back to ffmpeg-python.

-- Bid

gondolio commented 4 years ago

Hi @Saturnix , wondering if you had found a solution here to the error?

ValueError: Encountered trim(end_frame=1000, start_frame=0) <17c7b86357ec> with multiple outgoing edges with same upstream label None; a split filter is probably required

Saturnix commented 4 years ago

@gondolio

Does this help?

https://stackoverflow.com/questions/60123218/ffmpeg-python-trim-and-concat-doesnt-work

gondolio commented 4 years ago

Thanks @Saturnix , I did indeed end up doing something similar. Basically I refactored my code so that all the various functions I defined always take in two streams (audio/video) and return (at least) two streams (audio/video). Then only at the very end of the code do I concat them.

It works, but is more of a workaround than a proper solution in my opinion.