beginner.py 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. import openshot
  2. def cKey(r,g,b,fuzz):
  3. col=openshot.Color()
  4. col.red=openshot.Keyframe(r)
  5. col.green=openshot.Keyframe(g)
  6. col.blue=openshot.Keyframe(b)
  7. return openshot.ChromaKey(col, openshot.Keyframe(fuzz))
  8. def video_photo_clip(video=None, layer=None, position=None, end=None
  9. , scale_x=1, scale_y=1, location_x=0, location_y=0, ck=None, audio=True):
  10. clip = openshot.Clip(video)
  11. clip.Layer(layer)
  12. clip.Position(position)
  13. clip.End(end)
  14. clip.scale_x = openshot.Keyframe(scale_x)
  15. clip.scale_y = openshot.Keyframe(scale_y)
  16. clip.location_x = openshot.Keyframe(location_x)
  17. clip.location_y = openshot.Keyframe(location_y)
  18. if ck != None:
  19. clip.AddEffect(ck)
  20. if audio == True:
  21. clip.has_audio = openshot.Keyframe(1)
  22. else:
  23. clip.has_audio = openshot.Keyframe(0)
  24. return clip
  25. def video_writer_init(path):
  26. w = openshot.FFmpegWriter(path)
  27. w.SetAudioOptions(True, "aac", 44100, 2, openshot.LAYOUT_STEREO, 3000000)
  28. w.SetVideoOptions(True, "libx264", openshot.Fraction(30000, 1000), 1280, 720,
  29. openshot.Fraction(1, 1), False, False, 3000000)
  30. return w
  31. def create_video(bg, title_bg,time):
  32. t = openshot.Timeline(1280, 720, openshot.Fraction(30000, 1000), 44100, 2, openshot.LAYOUT_STEREO)
  33. t.Open()
  34. ck = cKey(0, 254, 0, 270)
  35. ck_anchor = cKey(0, 255, 0, 320)
  36. anchor_video = openshot.FFmpegReader(bg) # 影片
  37. anchor_video.Open()
  38. anchor_clip_video = video_photo_clip(video=anchor_video, layer=2, scale_x=0.59, scale_y=0.59, location_x=-0.04, location_y=-0.04, position=0, end=70, ck=None, audio=True)
  39. t.AddClip(anchor_clip_video)
  40. anchor_video.Close()
  41. anchor_template = openshot.FFmpegReader(title_bg) # 樣板
  42. anchor_template.Open()
  43. anchor_clip_template = video_photo_clip(video=anchor_template, layer=1, scale_x=1, scale_y=1,
  44. location_x=0, location_y=0, position=0, end=70, ck=None, audio=True)
  45. t.AddClip(anchor_clip_template)
  46. anchor_template.Close()
  47. w = video_writer_init("/app/output/A-2-1.mp4")
  48. w.Open()
  49. # frames = int(t.info.fps)*int(time)
  50. frames = t.info.fps.ToInt()*int(time)
  51. print('結果一',frames)
  52. for n in range(frames):
  53. # tmp = n%(int(t.info.fps)*3) +int(t.info.fps)*int(2)
  54. f=t.GetFrame(n)
  55. w.WriteFrame(f)
  56. t.Close()
  57. w.Close()
  58. if __name__ == '__main__':
  59. create_video("/app/input/movie_main/C0048.mp4","/app/input/bg/串場樣板1.mp4",61)