How to efficiently and automatedly join video clips using short transitions?

1

0

I am looking to join a set of video clips (with audio) using some short transitions (ideally crossfade) in a straightforward, automated and efficient way.

IE:

ClipA ---> ClipB --> ClipC --> ClipD
[aaaaaaaa][bbbbbbbb][cccccccc][dddddddd]
(--> indicate crossfades, not to scale!)

To clarify:

  • by straightforward, ideally it would involve using 'standard' tools (ie ones that I am already familiar with such ffmpeg, but less well-known tools like melt definitely qualify, and I'm willing to explore new horizons like MoviePy)
  • by automated I mean something that can be parameterised and put into a python/shell/etc script
  • by efficient I mean quick-to-encode or minimising re-encoding

At the moment I'm using python and ffmpeg to chop up a video file already in Matroska/h264/aac using the stream copy parameter (very fast) based on timecodes in a text file. Then melt sequentially strings these clips one after the other using short luma (crossfade) transitions (very slow due to re-encoding entire sequence).

It seems to me that out of these long video clips only a few seconds needs to be processed and re-encoded- the transitions themselves. The rest can be copied. Is there an already-extant / intuitive way of doing this? My naïve approach would be to further chop the clips into subclips, using melt to create the transitions, and using the concat filter to glue the mess together.

Eg:

ClipA1 ClipA2 ClipB1 ClipB2 ClipB3 ClipC1 ClipC2 ClipC3 ClipD1 Clip D2
[aaaaaaa] + [a][b] + [bbbbbb] + [b][c] + [cccccc] + [c][d] + [ddddddd]
(+ indicates concat-ing of subclips; [x][y] indicates a short xfade from x to y)

However, I am reticent to charge off on what may be a fool's errand as I anticipate melt/ffmpeg-related gotchas around timing of cuts and audio synchronisation; and I don't want to miss sections of video or audio due to I-frame placement or what have you. Plus I have a feeling I am not the first person to run into this, so I am curious as to how those smarter than I have solved this- my solutions tend to the naïve as I say!

Hope this has been clear. Cheers for reading and thanks in advance!


For context, my intention is to reduce unedited livestreamed video game footage into an interesting / "useful" video; that is cut out the boring fluff of loading stuff up, sorting connectivity issues etc. But it is also generalisable to highlights, a montage to show progression. Since it is something that in theory will come up every time there is new footage, I have a strong interest in doing it the most efficient (proactively laziest) way.

bertieb

Posted 2015-06-23T00:33:11.233

Reputation: 6 181

I think this is what you are looking for. But still it process the whole video.

– Chamath – 2015-06-23T05:45:20.487

@Chamath thanks for that- yes it does incur a re-encode but it may in fact form part of the solution; see a related question I've asked: http://superuser.com/questions/931969/creating-videos-for-ffmpegs-concat-demuxer-to-avoid-a-large-re-encode

– bertieb – 2015-06-24T11:52:16.213

Answers

5

I didn't have time to test it but this should work if you want to go with MoviePy:

from moviepy.editor import *

clips = [ VideoFileClip("vid1.mp4"),
          VideoFileClip("vid2.mp4"),
          VideoFileClip("vid3.mp4"), ... ]


fade_duration = 1 # 1-second fade-in for each clip
clips = [clip.crossfadein(fade_duration) for clip in clips]

final_clip = concatenate_videoclips(clips, padding = -fade_duration)

# You can write any format, in any quality.
final_clip.write_videofile("final.mp4", bitrate="5000k")

Zulko

Posted 2015-06-23T00:33:11.233

Reputation: 151

As I am unfamiliar with MoviePy, this is very useful- thank you! However, it incurs a transcode across the entire duration of video unfortunately; trying with 2 clips is giving a predicted ~2hr encode duration. Is this unavoidable or have I not used your sample code correctly? – bertieb – 2015-06-23T12:00:20.430

@bertieb same problem here, it takes too long, did you find anything efficient ? – Mathematics – 2019-06-02T11:11:20.480