-
- def prependBlack( self, fMP4Input, black_s, fMP4Dest ):
- print('pipeline -> prependBlack')
- """ Add black_s seconds of black to the start of the video.
- use ffmpeg for efficiency.
- """
- if not fMP4Input:
- return None
-
- if black_s in (None, 0.0):
- return fMP4Input
-
- # intermediate
- video_timescaled = self.tmp_filename('V1_video_timescaled.mp4')
- video_black = self.tmp_filename('V1_black_video.mp4')
- video_list = self.tmp_filename('V1_video_list.txt')
- # output
- video_with_black = fMP4Dest
-
- 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', fMP4Input,
- '-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', fMP4Input, '-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!')
-
- import moviepy
-
- t_fallback_1 = time.time()
-
- # Add 1.0 seconds of black to the start of the video
- video = VideoFileClip(fMP4Input)
- 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
-