cService.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. import util,os, math, time,queue,threading, rpyc
  2. import openshot
  3. rootPath = './'
  4. def filePrepare(name_hash):
  5. cPath = rootPath+name_hash+'/'
  6. try:
  7. os.mkdir(cPath)
  8. except FileExistsError:
  9. pass
  10. sub_dict,img_dict = util.parse_script("script.txt")
  11. util.generate_subtitle_image_from_dict(cPath, sub_dict)
  12. for imgd in img_dict:
  13. print(imgd)
  14. util.downloadFromDrive(cPath,imgd['imgid'],imgd['index'])
  15. util.call_anchor(cPath+'speech.mp3',7)
  16. return sub_dict,img_dict
  17. def genVideo(name_hash,sub_dict,img_dict):
  18. basicPath = rootPath+'basic/'
  19. cPath = rootPath+name_hash+'/'
  20. ck=util.cKey(0,254,0,270)
  21. ck_anchor=util.cKey(0,255,1,320)
  22. t = openshot.Timeline(1280, 720, openshot.Fraction(30000, 1000), 44100, 2, openshot.LAYOUT_STEREO)
  23. t.Open()
  24. main_timer = 0
  25. LOGO_OP = openshot.FFmpegReader(basicPath+"LOGO_OP_4.mp4")
  26. LOGO_OP.Open() # Open the reader
  27. head_duration = LOGO_OP.info.duration
  28. LOGO_OP_clip = util.video_photo_clip(vid=LOGO_OP,layer=4,position=0,end=head_duration)
  29. t.AddClip(LOGO_OP_clip)
  30. main_timer+=head_duration
  31. anchor = openshot.FFmpegReader(cPath+"/speaker.mp4")
  32. anchor.Open()
  33. anchor_clip = util.video_photo_clip(vid=anchor,layer=4,scale_x=0.65,scale_y=0.65,
  34. location_x=0.35,location_y=0.25,position=main_timer, end=anchor.info.duration,ck=ck_anchor,audio=False)
  35. t.AddClip(anchor_clip)
  36. speech = openshot.FFmpegReader(cPath+"/speech.mp3")
  37. speech.Open()
  38. speech_clip = openshot.Clip(speech)
  39. speech_clip.Position(main_timer)
  40. speech_clip.End(anchor.info.duration)
  41. t.AddClip(speech_clip)
  42. main_timer += anchor.info.duration
  43. anchor.Close()
  44. speech.Close()
  45. sub_img_list = [None] * len(sub_dict)
  46. sub_clip_list = [None] * len(sub_dict)
  47. for sub_obj in sub_dict:
  48. idx = int(sub_obj['index'])
  49. sub_img_list[idx] = openshot.QtImageReader(cPath +str(idx)+'.png')
  50. sub_img_list[idx].Open()
  51. sub_clip_list[idx] = util.video_photo_clip(vid=sub_img_list[idx], layer=5,location_x=0.069, location_y=0.89
  52. ,position=head_duration+sub_obj['start'],end=sub_obj['duration'])
  53. t.AddClip(sub_clip_list[idx])
  54. sub_img_list[idx].Close()
  55. img_list = [None] * len(img_dict)
  56. img_clip_list = [None] * len(img_dict)
  57. for img_d in img_dict:
  58. idx = int(img_d['index'])
  59. print(cPath +str(idx) +'img.jpg')
  60. try:
  61. img_list[idx] = openshot.QtImageReader(cPath +str(idx) +'img.jpg')
  62. img_list[idx].Open()
  63. except:
  64. img_list[idx] = openshot.QtImageReader(cPath +str(idx) +'img.png')
  65. img_list[idx].Open()
  66. img_clip_list[idx] = util.video_photo_clip(vid=img_list[idx], layer=3
  67. ,position=head_duration+img_d['start'],end=img_d['duration'])
  68. t.AddClip(img_clip_list[idx])
  69. img_list[idx].Close()
  70. w = util.video_writer_init("output.mp4")
  71. w.Open()
  72. frames = int(t.info.fps)*int(main_timer)
  73. for n in range(frames):
  74. f=t.GetFrame(n)
  75. w.WriteFrame(f)
  76. t.Close()
  77. w.Close()
  78. class podcast_service(rpyc.Service):
  79. def exposed_gen_video(self,name_hash, sub_dict, img_dict):
  80. genVideo(name_hash, sub_dict, img_dict)
  81. from rpyc.utils.server import ThreadedServer
  82. t = ThreadedServer(podcast_service, port=8838)
  83. print('service started')
  84. t.start()