def prependBlack( self, video_input, black_s=1.0 ): """ Add black_s seconds of black to the start of the video. use ffmpeg for efficiency. """ if black_s is None: black_s = 1.0 # intermediate video_timescaled = tmp_filename('_video_timescaled.mp4') video_black = tmp_filename('_black_video.mp4') video_list = tmp_filename('_video_list.txt') # output video_with_black = ('_video_with_black.mp4') try: t1 = time.time() # ffmpeg -i fullvideo.mp4 -vf trim=0:3,geq=0:128:128 -af atrim=0:3,volume=0 -video_track_timescale 600 3sec.mp4 args_create_black = ['ffmpeg', '-y', '-i', video_input, '-vf', 'trim=0:{},geq=0:128:128'.format(float(black_s)), '-af', 'atrim=0:1,volume=0', '-video_track_timescale', '600', '-strict', '-2', '-threads', '0', video_black] subprocess.run(args_create_black, check=True) t2 = time.time() print('Elapsed time creating black:', t2 - t1) # ffmpeg -i fullvideo.mp4 -c copy -video_track_timescale 600 full600.mp4 args_ensure_speed = ['ffmpeg', '-y', '-i', video_input, '-c', 'copy', '-video_track_timescale', '600', '-threads', '0', video_timescaled] subprocess.run(args_ensure_speed, check=True) t3 = time.time() print('Elapsed time for timescale conversion:', t3 - t2) # create a list of concatencated videos with open(video_list, 'w') as f: f.write('file ' + video_black + '\n' + 'file ' + video_timescaled + '\n') # ffmpeg -f concat -i list.txt -c copy merged.mp4 args_concat = ['ffmpeg', '-y', '-f', 'concat', '-safe', '0', '-i', video_list, '-c', 'copy', '-threads', '0', video_with_black] subprocess.run(args_concat, check=True) t4 = time.time() print('Elapsed time concatenating videos:', t4 - t3) # in case ffmpeg does not support the input video feature, use other backend except subprocess.CalledProcessError as e: print('Error:', e) print('Fallback to moviepy library!') t_fallback_1 = time.time() # Add 1.0 seconds of black to the start of the video video = VideoFileClip(video_input) black_clip = ColorClip(video.size, color=(0, 0, 0), duration=1.0) final_clip = concatenate_videoclips([black_clip, video]) final_clip.write_videofile(video_with_black, fps=video.fps) t_fallback_2 = time.time() print('Elapsed time for fallback adding black to video with moviepy:', t_fallback_2 - t_fallback_1) return video_with_black