spacepaste

  1.  
  2. def prependBlack( self, fMP4Input, black_s, fMP4Dest ):
  3. print('pipeline -> prependBlack')
  4. """ Add black_s seconds of black to the start of the video.
  5. use ffmpeg for efficiency.
  6. """
  7. if not fMP4Input:
  8. return None
  9. if black_s in (None, 0.0):
  10. return fMP4Input
  11. # intermediate
  12. video_timescaled = self.tmp_filename('V1_video_timescaled.mp4')
  13. video_black = self.tmp_filename('V1_black_video.mp4')
  14. video_list = self.tmp_filename('V1_video_list.txt')
  15. # output
  16. video_with_black = fMP4Dest
  17. try:
  18. t1 = time.time()
  19. # ffmpeg -i fullvideo.mp4 -vf trim=0:3,geq=0:128:128 -af atrim=0:3,volume=0 -video_track_timescale 600 3sec.mp4
  20. args_create_black = ['ffmpeg', '-y', '-i', fMP4Input,
  21. '-vf', 'trim=0:{},geq=0:128:128'.format(float(black_s)),
  22. '-af', 'atrim=0:1,volume=0',
  23. '-video_track_timescale', '600', '-strict', '-2',
  24. '-threads', '0', video_black]
  25. subprocess.run(args_create_black, check=True)
  26. t2 = time.time()
  27. print('Elapsed time creating black:', t2 - t1)
  28. # ffmpeg -i fullvideo.mp4 -c copy -video_track_timescale 600 full600.mp4
  29. args_ensure_speed = ['ffmpeg', '-y', '-i', fMP4Input, '-c', 'copy',
  30. '-video_track_timescale', '600', '-threads', '0',
  31. video_timescaled]
  32. subprocess.run(args_ensure_speed, check=True)
  33. t3 = time.time()
  34. print('Elapsed time for timescale conversion:', t3 - t2)
  35. # create a list of concatencated videos
  36. with open(video_list, 'w') as f:
  37. f.write('file ' + video_black + '\n' +
  38. 'file ' + video_timescaled + '\n')
  39. # ffmpeg -f concat -i list.txt -c copy merged.mp4
  40. args_concat = ['ffmpeg', '-y', '-f', 'concat', '-safe', '0',
  41. '-i', video_list, '-c', 'copy', '-threads', '0',
  42. video_with_black]
  43. subprocess.run(args_concat, check=True)
  44. t4 = time.time()
  45. print('Elapsed time concatenating videos:', t4 - t3)
  46. # in case ffmpeg does not support the input video feature, use other backend
  47. except subprocess.CalledProcessError as e:
  48. print('Error:', e)
  49. print('Fallback to moviepy library!')
  50. import moviepy
  51. t_fallback_1 = time.time()
  52. # Add 1.0 seconds of black to the start of the video
  53. video = VideoFileClip(fMP4Input)
  54. black_clip = ColorClip(video.size, color=(0, 0, 0),
  55. duration=1.0)
  56. final_clip = concatenate_videoclips([black_clip, video])
  57. final_clip.write_videofile(video_with_black, fps=video.fps)
  58. t_fallback_2 = time.time()
  59. print('Elapsed time for fallback adding black to video with moviepy:',
  60. t_fallback_2 - t_fallback_1)
  61. return video_with_black
  62.