ae_movie_export_video.py 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. from tools import tools
  2. class MyTools(tools.Tools):
  3. def _initialize(self):
  4. self.add_argument_file('--in_path', 'in_path')
  5. self.add_argument_file('--out_path', 'out_path', exists=None)
  6. self.add_argument_dir('--export_path', 'export_path', exists=None)
  7. self.add_argument('--export_name', 'export_name')
  8. self.add_argument('--project_name', 'project_name')
  9. self.add_argument('--write_name', 'write_name')
  10. self.add_argument('--write_path', 'write_path')
  11. self.add_argument_bool('--alpha', 'alpha')
  12. self.add_argument_bool('--use_sha1', 'use_sha1')
  13. pass
  14. def _run(self, args):
  15. ffmpeg_path = self.get_tools_path("ffmpeg.exe")
  16. if self.exist_file(ffmpeg_path) is False:
  17. self.error_result("Not found ffmpeg.exe")
  18. return False
  19. pass
  20. sha1path = None
  21. if args.use_sha1 is True:
  22. sha1hex, sha1path = self.get_file_sha1_new_path(args.in_path, ".ogv")
  23. sha1tag = "export_video alpha:{0}".format(args.alpha)
  24. temp_path = self.get_sha1_cach_path(sha1tag, sha1hex + ".export_video")
  25. out_path = args.export_path + ".store_video" + "/" + sha1path
  26. pass
  27. else:
  28. temp_path = self.get_temp_path("export_video.info")
  29. out_path = self.change_ext(args.out_path, "ogv")
  30. pass
  31. if args.alpha is True:
  32. if args.use_sha1 is False or ((args.out_path is not None and self.exist_file(out_path) is False) or self.exist_file(temp_path) is False):
  33. if out_path is not None:
  34. self.make_dir_for_file(out_path)
  35. pass
  36. command_args = ["-loglevel", "error"
  37. , "-y"
  38. , "-threads", "4"
  39. , "-i", "{0}".format(args.in_path)
  40. , "-vf", "split [a], pad=iw:ih*2 [b], [a] alphaextract, [b] overlay=0:h"
  41. , "-vcodec", "libtheora"
  42. , "-f", "ogg"
  43. , "-map_metadata", "-1"
  44. , "-an"
  45. , "-q", "10"
  46. , "-pix_fmt", "yuv420p"
  47. , "{0}".format(out_path)
  48. ]
  49. if self.command_call("ffmpeg video with alpha", ffmpeg_path, command_args) is False:
  50. return False
  51. pass
  52. with open(temp_path, "w") as f:
  53. pass
  54. pass
  55. if args.write_name is not None:
  56. if args.use_sha1 is True:
  57. print("write_name = Movies2_Video_{0}\n".format(sha1path))
  58. pass
  59. else:
  60. print("write_name = Movies2_{0}_{1}_{2}\n".format(args.project_name, "Video", args.write_name))
  61. pass
  62. pass
  63. if args.write_path is not None:
  64. if args.use_sha1 is True:
  65. ogv_write_path = sha1path
  66. pass
  67. else:
  68. ogv_write_path = self.change_ext(args.write_path, "ogv")
  69. pass
  70. print("write_path = {0}\n".format(ogv_write_path))
  71. pass
  72. print("codec = 1\n")
  73. pass
  74. else:
  75. if args.use_sha1 is False or ((args.out_path is not None and self.exist_file(out_path) is False) or self.exist_file(temp_path) is False):
  76. if out_path is not None:
  77. self.make_dir_for_file(out_path)
  78. pass
  79. command_args = ["-loglevel", "error"
  80. , "-y"
  81. , "-threads", "4"
  82. , "-i", "{0}".format(args.in_path)
  83. , "-vcodec", "libtheora"
  84. , "-f", "ogg"
  85. , "-map_metadata", "-1"
  86. , "-an"
  87. , "-q", "10"
  88. , "-pix_fmt", "yuv420p"
  89. , "{0}".format(out_path)
  90. ]
  91. if self.command_call("ffmpeg video without alpha", ffmpeg_path, command_args) is False:
  92. return False
  93. pass
  94. with open(temp_path, "w") as f:
  95. pass
  96. pass
  97. if args.write_name is not None:
  98. if args.use_sha1 is True:
  99. print("write_name = {0}\n".format(sha1path))
  100. pass
  101. else:
  102. print("write_name = Movies2_{0}_{1}_{2}\n".format(args.project_name, "Video", args.write_name))
  103. pass
  104. pass
  105. if args.write_path is not None:
  106. if args.use_sha1 is True:
  107. ogv_write_path = sha1path
  108. pass
  109. else:
  110. ogv_write_path = self.change_ext(args.write_path, "ogv")
  111. pass
  112. print("write_path = {0}\n".format(ogv_write_path))
  113. pass
  114. print("codec = 2\n")
  115. pass
  116. return True
  117. pass
  118. pass
  119. tools.run(MyTools)