Skip to content

Instantly share code, notes, and snippets.

@Elfsong
Created February 19, 2019 12:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Elfsong/33ac197e33d4caf6646ce5768a645f53 to your computer and use it in GitHub Desktop.
Save Elfsong/33ac197e33d4caf6646ce5768a645f53 to your computer and use it in GitHub Desktop.
movie_studio
import os
import time
from multiprocessing import Process
import cocos
import cv2
import numpy as np
import pyglet
from PIL import ImageGrab
from cocos.actions import CallFunc
from cocos.actions.interval_actions import FadeIn, FadeOut, Delay
from cocos.director import director
from cocos.scenes import SplitColsTransition
class Background(cocos.layer.Layer):
_handlers_enabled = True
def __init__(self, image_name, next_scene, duration):
super(Background, self).__init__()
self.next_scene = next_scene
self.background = cocos.sprite.Sprite(image_name)
self.background.position = 1920 / 2, 1080 / 2
self.background.scale = 1
self.add(self.background, z=0)
action = Delay(duration) + CallFunc(self.action_end)
self.background.do(action)
def action_end(self):
if self.next_scene:
cocos.director.director.replace(cocos.scenes.FlipY3DTransition(self.next_scene, duration=1))
else:
cocos.director.director.pop()
class CharacterLayer(cocos.layer.Layer):
def __init__(self, character_list):
super(CharacterLayer, self).__init__()
for character in character_list:
character_image_path = os.path.join(character_collection_path, character["category"],
character["file_name"])
if character["file_name"].endswith("gif"):
temp_spirit = cocos.sprite.Sprite(pyglet.image.load_animation(character_image_path))
else:
temp_spirit = cocos.sprite.Sprite(pyglet.image.load(character_image_path))
temp_spirit.scale = character["scale"]
temp_spirit.rotation = character["rotation"]
temp_spirit.position = character["position"]
temp_spirit.scale_x = -1 if character["flip"][0] else 1
temp_spirit.scale_y = -1 if character["flip"][1] else 1
self.add(temp_spirit, z=1)
class Bubble(cocos.layer.Layer):
def __init__(self, text):
super(Bubble, self).__init__()
bubble = cocos.sprite.Sprite("dialog.png")
bubble.position = 1920 / 2 + 150, 1080 / 2
bubble.scale = 0.6
label = cocos.text.Label(text,
font_name='Times New Roman',
font_size=20,
color=(234, 122, 121, 112),
anchor_x='center', anchor_y='center')
label.position = 1920 / 2 + 150, 1080 / 2
fadein_action = FadeIn(1.0)
bubble.do(fadein_action)
label.do(fadein_action)
self.add(bubble, z=2)
self.add(label, z=3)
class BubbleLayer(cocos.layer.Layer):
def __init__(self, dialoglist):
super(BubbleLayer, self).__init__()
for bubble in dialoglist:
temp_bubble = cocos.sprite.Sprite(bubble_collection_path)
temp_label = cocos.text.Label(bubble["text"], font_name='Times New Roman', font_size=24,
color=(234, 122, 121, 112), anchor_x='center', anchor_y='center')
temp_bubble.position = bubble["position"]
temp_label.position = bubble["position"]
temp_action = cocos.actions.Hide() + \
Delay(bubble["start"]) + \
cocos.actions.Show() + \
FadeIn(0.25) + \
Delay(bubble["end"] - bubble["start"] - 0.5) + \
FadeOut(0.25) + \
cocos.actions.Hide()
temp_bubble.do(temp_action)
temp_label.do(temp_action)
self.add(temp_bubble)
self.add(temp_label)
def run_proc1(name):
director.init(width=1920, height=1080, caption="story_video", fullscreen=False)
temp_init_scene = cocos.scene.Scene()
init_scene = cocos.scene.Scene()
scene_1 = cocos.scene.Scene()
scene_2 = cocos.scene.Scene()
scene_3 = cocos.scene.Scene()
scene_4 = cocos.scene.Scene()
scene_4_character_list = [
{
"category": "elephant",
"file_name": "elephant.png",
"scale": 0.6,
"rotation": 0,
"position": [1479, 200],
"flip": [False, False]
},
{
"category": "mouse",
"file_name": "mouse.png",
"scale": 0.5,
"rotation": -40,
"position": [683, 400],
"flip": [True, False]
}
]
scene_4_dialog_list = [
{
"text": "哎呀 真是对不起",
"start": 125 - 113,
"end": 129 - 113,
"position": [1479 + 200, 200 + 200],
},
{
"text": "真好玩 真好玩",
"start": 134 - 113,
"end": 137 - 113,
"position": [683 + 200, 400 + 200],
},
]
scene_4_bg_image_path = os.path.join(background_collection_path, "buildingside.png")
scene_4.add(Background(scene_4_bg_image_path, next_scene=None, duration=22), z=1)
scene_4.add(CharacterLayer(character_list=scene_4_character_list), z=2)
scene_4.add(BubbleLayer(dialoglist=scene_4_dialog_list), z=3)
scene_3_character_list = [
{
"category": "elephant",
"file_name": "elephant.png",
"scale": 0.6,
"rotation": 0,
"position": [1479, 200],
"flip": [False, False]
},
{
"category": "mouse",
"file_name": "mouse.png",
"scale": 0.6,
"rotation": 0,
"position": [1083, 200],
"flip": [True, False]
}
]
scene_3_dialog_list = [
{
"text": "小老鼠 我帮你扇扇风吧",
"start": 105 - 99,
"end": 110 - 99,
"position": [1479 + 200, 200 + 200],
},
{
"text": "那真是谢谢你了",
"start": 112 - 99,
"end": 115 - 99,
"position": [1083 + 200, 200 + 200],
},
]
scene_3_bg_image_path = os.path.join(background_collection_path, "buildingside.png")
scene_3.add(Background(scene_3_bg_image_path, next_scene=scene_4, duration=14), z=1)
scene_3.add(CharacterLayer(character_list=scene_3_character_list), z=2)
scene_3.add(BubbleLayer(dialoglist=scene_3_dialog_list), z=3)
scene_2_character_list = [
{
"category": "elephant",
"file_name": "elephant.png",
"scale": 0.6,
"rotation": 0,
"position": [666, 400],
"flip": [True, False]
},
{
"category": "bear",
"file_name": "bear.png",
"scale": 0.6,
"rotation": 0,
"position": [1333, 450],
"flip": [False, False]
}
]
scene_2_dialog_list = [
{
"text": "好孩子 谢谢你",
"start": 92 - 75,
"end": 95 - 75,
"position": [1333 + 200, 450 + 200],
},
{
"text": "不用谢 不用谢",
"start": 97 - 75,
"end": 99 - 75,
"position": [666 + 200, 400 + 200],
},
]
scene_2_bg_image_path = os.path.join(background_collection_path, "riverside.png")
scene_2.add(Background(scene_2_bg_image_path, next_scene=scene_3, duration=24), z=1)
scene_2.add(CharacterLayer(character_list=scene_2_character_list), z=2)
scene_2.add(BubbleLayer(dialoglist=scene_2_dialog_list), z=3)
scene_1_character_list = [
{
"category": "elephant",
"file_name": "elephant.png",
"scale": 0.6,
"rotation": 0,
"position": [694, 400],
"flip": [True, False]
},
{
"category": "cattle",
"file_name": "cattle.png",
"scale": 0.6,
"rotation": 0,
"position": [1283, 500],
"flip": [False, False]
}
]
scene_1_dialog_list = [
{
"text": "好凉快呀 真是谢谢你",
"start": 69 - 45,
"end": 72 - 45,
"position": [1283 + 200, 500 + 200],
},
{
"text": "不用谢 不用谢",
"start": 74 - 45,
"end": 76 - 45,
"position": [694 + 200, 400 + 200],
},
]
scene_1_bg_image_path = os.path.join(background_collection_path, "farmland.png")
scene_1.add(Background(scene_1_bg_image_path, next_scene=scene_2, duration=30), z=1)
scene_1.add(CharacterLayer(character_list=scene_1_character_list), z=2)
scene_1.add(BubbleLayer(dialoglist=scene_1_dialog_list), z=3)
init_scene_character_list = [
{
"category": "xiaoice",
"file_name": "xiaoice.gif",
"scale": 0.8,
"rotation": 0,
"position": [1920 / 2 - 500, 1080 / 2 - 200],
"flip": [False, False]
},
]
init_scene_dialog_list = [
{
"text": "小朋友 准备好",
"start": 0,
"end": 2,
"position": [1920 / 2 - 300, 1080 / 2 + 140],
},
{
"text": "小朋友 准备好",
"start": 2,
"end": 4.5,
"position": [1920 / 2 - 300, 1080 / 2 + 140],
},
{
"text": "听小冰姐姐给你讲故事",
"start": 4.5,
"end": 9,
"position": [1920 / 2 - 300, 1080 / 2 + 140],
},
{
"text": "茜茜小朋友你好",
"start": 23,
"end": 25,
"position": [1920 / 2 - 300, 1080 / 2 + 140],
},
{
"text": "我是你的好朋友小冰姐姐",
"start": 25,
"end": 28.5,
"position": [1920 / 2 - 300, 1080 / 2 + 140],
},
{
"text": "欢迎来到你的童话世界",
"start": 28.5,
"end": 31.5,
"position": [1920 / 2 - 300, 1080 / 2 + 140],
},
{
"text": "今天我要给你讲的故事叫做小象的大耳朵",
"start": 31.5,
"end": 37,
"position": [1920 / 2 - 300, 1080 / 2 + 140],
},
{
"text": "在这个故事里 主人公的名字就叫茜茜",
"start": 37,
"end": 44,
"position": [1920 / 2 - 300, 1080 / 2 + 140],
}
]
init_scene_bg_image_path = os.path.join(background_collection_path, "bedroom.jpg")
init_scene.add(Background(init_scene_bg_image_path, next_scene=scene_1, duration=45), z=1)
init_scene.add(CharacterLayer(character_list=init_scene_character_list), z=2)
init_scene.add(BubbleLayer(dialoglist=init_scene_dialog_list), z=3)
temp_init_scene.add(Background(init_scene_bg_image_path, next_scene=init_scene, duration=10), z=1)
director.run(temp_init_scene)
def run_proc2(name):
import win32gui
time.sleep(1)
fceuxHWND = win32gui.FindWindow(None, 'story_video')
print(fceuxHWND)
rect = win32gui.GetWindowRect(fceuxHWND)
# print("Recording")
time.sleep(1)
current_screen = ImageGrab.grab(bbox=rect)
height, weight = current_screen.size
print(height, weight)
fourcc = cv2.VideoWriter_fourcc(*'XVID')
video = cv2.VideoWriter('generated.avi', fourcc, 16, (height, weight))
duration = 5
start_time = time.time()
while time.time() - start_time <= duration:
im = ImageGrab.grab()
imm = cv2.cvtColor(np.array(im), cv2.COLOR_RGB2BGR)
video.write(imm)
video.release()
cv2.destroyAllWindows()
print("Finished!")
# Basic path information
ROOT_PATH = r"D:\ChildStory"
TRANSFORMER_PATH = os.path.join(ROOT_PATH, "Transformer")
RESOURCE_PATH = os.path.join(ROOT_PATH, "Resources")
background_collection_path = os.path.join(RESOURCE_PATH, "background")
character_collection_path = os.path.join(RESOURCE_PATH, "character")
bubble_collection_path = os.path.join(RESOURCE_PATH, "talkbubble", "dialog.png")
if __name__ == "__main__":
p1 = Process(target=run_proc1, args=('test',))
p2 = Process(target=run_proc2, args=('test',))
p1.start()
p2.start()
p1.join()
p2.join()
print('End')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment