Files
baby-tracker/scripts/generate_care_videos.py
T

410 lines
20 KiB
Python

# -*- coding: utf-8 -*-
"""
Generate professional, lightweight pediatric care demonstration videos (MP4)
with animated steps, typography, badges, progress bars and audio.
"""
import os
import sys
import subprocess
import tempfile
import math
from PIL import Image, ImageDraw, ImageFont
VIDEOS_DIR = os.path.abspath(os.path.join(os.path.dirname(__file__), '../public/videos'))
os.makedirs(VIDEOS_DIR, exist_ok=True)
FONT_PATH = "C:\\Windows\\Fonts\\msyh.ttc"
if not os.path.exists(FONT_PATH):
FONT_PATH = "C:\\Windows\\Fonts\\simhei.ttf"
def get_font(size, bold=False):
try:
return ImageFont.truetype(FONT_PATH, size)
except:
return ImageFont.load_default()
GUIDE_SCRIPTS = [
{
'id': 'guide-bottle',
'title': '45°防胀气瓶喂姿势与拍嗝衔接',
'badge': '🍼 喂养护理指南',
'theme_color': (245, 158, 11), # Amber
'accent_color': (217, 119, 6),
'bg_gradient': [(30, 24, 16), (15, 23, 42)],
'scenes': [
{
'step': '第 1 步 / 共 4 步',
'action': '手腕内侧测试奶温',
'detail': '冲调后滴在手腕内侧感受温热不烫\n儿科推荐最佳温度:37℃ ~ 40℃\n切勿直接给宝宝喂过烫奶水',
'icon': '🌡️',
'tip': '💡 提示:温度过高易烫伤口腔,过低易引起肠绞痛'
},
{
'step': '第 2 步 / 共 4 步',
'action': '45° 半坐卧位抱姿',
'detail': '一手托稳宝宝颈背,身体抬高约 45°\n头部略高于胃部,保持气道畅通\n严禁完全平躺仰卧喝奶,防止呛咳',
'icon': '👶',
'tip': '⚠️ 警示:平躺喝奶极易逆流进入中耳引发中耳炎'
},
{
'step': '第 3 步 / 共 4 步',
'action': '奶瓶保持 45° 倾角',
'detail': '轻触宝宝下唇诱导自然张大嘴\n瓶身倾斜 45°,奶嘴前部必须充满奶水\n避免残留气泡被婴儿吞咽入胃',
'icon': '🍼',
'tip': '💡 技巧:吸吮过程中注意奶嘴不要被压扁'
},
{
'step': '第 4 步 / 共 4 步',
'action': 'Paced Feeding 节奏喂养与拍嗝',
'detail': '每喝 30~50ml 稍稍倾立奶瓶放慢节奏\n让宝宝自主吞咽并换气\n喝完后立即竖抱,空心掌自下而上轻柔拍嗝',
'icon': '✨',
'tip': '✅ 标准:拍出 1~2 声响嗝,防止吐奶与腹胀'
}
]
},
{
'id': 'guide-breastfeed',
'title': '新生儿深吸含乳技巧与姿势调整',
'badge': '🤱 母乳亲喂指导',
'theme_color': (236, 72, 153), # Pink
'accent_color': (219, 39, 119),
'bg_gradient': [(38, 18, 30), (15, 23, 42)],
'scenes': [
{
'step': '第 1 步 / 共 4 步',
'action': '妈妈身体放松与支撑',
'detail': '背后手臂垫好哺乳枕,肩颈放松不耸肩\n把宝宝贴近乳房,切忌大人弯腰去凑宝宝\n母婴腹部贴腹部,身体呈一直线',
'icon': '🪑',
'tip': '💡 要点:妈妈姿势舒适才能持久不疲劳'
},
{
'step': '第 2 步 / 共 4 步',
'action': '乳头轻触人中引导张大嘴',
'detail': '让乳头轻触宝宝人中或上嘴唇\n等待宝宝像打哈欠一样充分张大嘴巴\n切勿在宝宝嘴半张时强行塞入',
'icon': '👄',
'tip': '💡 秘诀:宝宝嘴张至 130°~140° 为最佳含乳时机'
},
{
'step': '第 3 步 / 共 4 步',
'action': '下巴先贴紧,深含大部分下乳晕',
'detail': '迅速将宝宝贴近乳房,下颌先抵住乳房\n下唇深含大部分下乳晕,上唇含少部分\n上下唇呈外翻的“小花”喇叭状',
'icon': '🤱',
'tip': '⚠️ 警示:只咬乳头会造成严重皲裂剧痛且吃不到奶'
},
{
'step': '第 4 步 / 共 4 步',
'action': '确认有效吞咽与安全退奶',
'detail': '观察面颊圆润不内凹,有缓慢吞咽声\n若需中断喂养,用小指探入口角解开负压\n切不可生拉硬拽拉出乳头',
'icon': '✅',
'tip': '💡 达标:每次亲喂 15~20 分钟后宝宝满足松口'
}
]
},
{
'id': 'guide-burping',
'title': '空心掌正确手法与端坐/过肩拍嗝实操',
'badge': '💨 拍嗝防胀气指南',
'theme_color': (16, 185, 129), # Emerald
'accent_color': (5, 150, 105),
'bg_gradient': [(16, 35, 28), (15, 23, 42)],
'scenes': [
{
'step': '第 1 步 / 共 4 步',
'action': '正确手型:弯曲成“空心掌”',
'detail': '五指并拢,掌心微弯微拱呈空杯状\n拍击时形成空气气垫减震缓冲\n发出“嘭嘭”沉闷空心声,切勿用实心巴掌!',
'icon': '✋',
'tip': '⚠️ 警示:实心巴掌拍击会震伤婴儿内脏与脊椎!'
},
{
'step': '第 2 步 / 共 4 步',
'action': '端坐式拍嗝法(适合低月龄)',
'detail': '一手虎口稳托宝宝下巴与前胸\n让宝宝身体稍稍前倾坐于大人腿上\n另一手空心掌自背下方向上规律轻叩',
'icon': '🪑',
'tip': '💡 技巧:手腕放松发力,自腰部往颈肩单向叩打'
},
{
'step': '第 3 步 / 共 4 步',
'action': '竖抱过肩式拍嗝法',
'detail': '宝宝竖抱,下巴轻轻靠在大人肩窝软肉处\n大人肩头提前垫好防吐奶口水巾\n空心掌轻柔由下至上拍打宝宝背部中心',
'icon': '🤱',
'tip': '💡 建议:拍打 5~8 分钟直到听到响亮饱嗝'
},
{
'step': '第 4 步 / 共 4 步',
'action': '拍不出嗝时的应对:竖抱与飞机抱',
'detail': '若 10 分钟未拍出嗝,可保持竖抱 15 分钟\n利用重力气体自然上浮排出\n易胀气宝宝可配合飞机抱托腹轻摇排气',
'icon': '✈️',
'tip': '✅ 提示:拍嗝完毕放下时建议头颈稍高右侧卧'
}
]
},
{
'id': 'guide-diaper',
'title': '防红屁屁换尿布全流程与温水冲洗法',
'badge': '💧 换尿布护臀要领',
'theme_color': (14, 165, 233), # Sky Blue
'accent_color': (2, 132, 199),
'bg_gradient': [(15, 30, 45), (15, 23, 42)],
'scenes': [
{
'step': '第 1 步 / 共 4 步',
'action': '轻托臀部,严禁倒提双腿',
'detail': '一手托住婴儿臀部下方或大腿腹部轻抬\n严禁双手倒提脚踝悬空小腿!\n避免拉扯娇嫩髋关节造成发育性脱位',
'icon': '🤲',
'tip': '⚠️ 警示:绝对不要用力倒吊婴儿双腿换尿布'
},
{
'step': '第 2 步 / 共 4 步',
'action': '温水冲洗,由前往后单向清洁',
'detail': '大便后优先用 37℃ 温水冲洗臀部\n用棉柔巾“由前往后”单向轻柔拭净\n切勿来回打圈擦抹,尤其女宝必须防逆行感染',
'icon': '🚿',
'tip': '💡 要点:避免湿纸巾反复用力摩擦脆弱表皮'
},
{
'step': '第 3 步 / 共 4 步',
'action': '充分蘸干与空气晾臀 1~2 分钟',
'detail': '用干柔巾轻轻按压吸干皮肤水分,勿大力摩擦\n让小屁屁在空气中自然晾干 1~2 分钟\n保持皮肤彻底干爽是预防红臀最关键一步',
'icon': '🌬️',
'tip': '💡 秘诀:皮肤未干就穿尿布极易潮湿焖出湿疹'
},
{
'step': '第 4 步 / 共 4 步',
'action': '厚涂护臀膏隔离与松紧度调节',
'detail': '在肛周及腹沟厚涂含氧化锌的护臀霜\n厚度如奶油般盖住原本肤色以隔绝尿便刺激\n穿新尿布腰围留出一指空隙,理顺防漏荷叶边',
'icon': '🛡️',
'tip': '✅ 标准:腰围宽松适度,腿围立体护围翻出'
}
]
},
{
'id': 'guide-sleep',
'title': '新生儿安全襁褓裹法与入睡安抚手法',
'badge': '😴 安全睡眠守护',
'theme_color': (147, 51, 234), # Purple
'accent_color': (126, 34, 206),
'bg_gradient': [(28, 16, 45), (15, 23, 42)],
'scenes': [
{
'step': '第 1 步 / 共 4 步',
'action': '唯一安全睡姿:坚实仰卧平躺',
'detail': '1 岁以内唯一安全的推荐睡姿为平躺仰卧\n床垫平整坚实,不可使用柔软羽绒床垫\n婴儿床内必须“空无一物”:严禁放枕头/玩偶/厚被',
'icon': '🛏️',
'tip': '⚠️ 警示:严禁未满周岁婴儿趴睡,谨防窒息与 SIDS'
},
{
'step': '第 2 步 / 共 4 步',
'action': '科学襁褓包裹:上紧下松',
'detail': '双臂自然贴于身侧包紧,抑制莫罗惊跳反射\n下半身必须留足活动空间呈自然“青蛙腿”\n禁止捆绑双腿(蜡烛包)以防髋关节脱位',
'icon': '📦',
'tip': '💡 黄金法则:上身紧如子宫包裹,下肢松展自如'
},
{
'step': '第 3 步 / 共 4 步',
'action': '营造入睡安全环境与仪式',
'detail': '室温保持 22~24℃,湿度 50%~60%\n调暗卧室内灯光,播放 50dB 以下持续白噪音\n如雨声、海浪声或心跳音,模拟子宫听觉',
'icon': '🌙',
'tip': '💡 技巧:白噪音音量不宜过大,距离婴儿床 1 米以上'
},
{
'step': '第 4 步 / 共 4 步',
'action': '轻拍胸口安抚与自主入睡衔接',
'detail': '手掌心轻贴宝宝胸口有节律轻轻按抚\n宝宝昏昏欲睡尚未全睡着时放入婴儿床\n培养宝宝不依赖奶嘴或抱摇的自主接觉能力',
'icon': '⭐',
'tip': '⚠️ 警示:宝宝一旦出现翻身迹象立即停止裹双臂襁褓'
}
]
},
{
'id': 'guide-bath',
'title': '新生儿快速安全盆浴与脐带消毒步骤',
'badge': '🛁 日常洗护要领',
'theme_color': (20, 184, 166), # Teal
'accent_color': (13, 148, 136),
'bg_gradient': [(15, 35, 38), (15, 23, 42)],
'scenes': [
{
'step': '第 1 步 / 共 4 步',
'action': '环境与水温准备(手肘试温)',
'detail': '室温保持 26~28℃,关好门窗避风\n水温 37~38℃(大人用手肘内侧试水感觉温热)\n浴巾、干爽衣物与纸尿裤提前完全展开备用',
'icon': '🌡️',
'tip': '💡 提示:洗澡全程严格控制在 5~8 分钟内防止受凉'
},
{
'step': '第 2 步 / 共 4 步',
'action': '橄榄球式托抱与耳部防水保护',
'detail': '左手臂夹稳宝宝身体,左手稳稳托托住头颈\n用大拇指和中指将宝宝耳朵向前折按紧贴脸颊\n防止洗头洗脸时水流流入外耳道',
'icon': '🏈',
'tip': '⚠️ 警示:任何时候大人的手不可离开婴儿身体!'
},
{
'step': '第 3 步 / 共 4 步',
'action': '轻柔清洗皱褶处与快速包裹擦干',
'detail': '先洗脸和头,擦干后再放入澡盆清洗躯干\n着重清洗颈部、腋下、腹股沟等皮褶处\n洗完立刻用大浴巾包裹全身,轻轻按压吸干水分',
'icon': '🧼',
'tip': '💡 技巧:不要用力擦拭,按压蘸干即可保护皮脂膜'
},
{
'step': '第 4 步 / 共 4 步',
'action': '脐带根部规范消毒与防尿浸染',
'detail': '用 75% 酒精或碘伏医用棉签消毒脐带\n一手轻提脐带残端,棉签自根部由内向外旋转消毒\n穿尿布时将前部边缘向下反折,彻底露出口脐根部',
'icon': '🩹',
'tip': '✅ 标准:保持脐窝干燥透气,若发红发臭需及时就医'
}
]
}
]
WIDTH = 640
HEIGHT = 360
FPS = 25
SCENE_DURATION_SEC = 4.0 # Each scene lasts 4 seconds -> 16 seconds total
def draw_rounded_rect(draw, bbox, radius, fill, outline=None, width=1):
x0, y0, x1, y1 = bbox
draw.rounded_rectangle([x0, y0, x1, y1], radius=radius, fill=fill, outline=outline, width=width)
def generate_video(guide):
guide_id = guide['id']
output_mp4 = os.path.join(VIDEOS_DIR, f"{guide_id}.mp4")
print(f"Generating video for: {guide_id} ({guide['title']})...")
temp_dir = tempfile.mkdtemp(prefix=f"frames_{guide_id}_")
scenes = guide['scenes']
total_scenes = len(scenes)
frames_per_scene = int(SCENE_DURATION_SEC * FPS)
total_frames = total_scenes * frames_per_scene
font_title = get_font(20, bold=True)
font_badge = get_font(12, bold=True)
font_step = get_font(12, bold=True)
font_action = get_font(22, bold=True)
font_detail = get_font(14)
font_tip = get_font(12, bold=True)
font_icon = get_font(36)
font_footer = get_font(10)
theme_color = guide['theme_color']
accent_color = guide['accent_color']
frame_idx = 0
for s_idx, scene in enumerate(scenes):
for f_in_s in range(frames_per_scene):
t = f_in_s / frames_per_scene # 0.0 to 1.0 progress in scene
overall_progress = frame_idx / total_frames
# Fade in transition at start of scene (first 10 frames)
alpha_scene = min(1.0, f_in_s / 10.0)
# Create base image with deep slate gradient
img = Image.new('RGB', (WIDTH, HEIGHT), color=(15, 23, 42))
draw = ImageDraw.Draw(img)
# Gradient background bars
for y in range(HEIGHT):
ratio = y / HEIGHT
r = int(guide['bg_gradient'][0][0] * (1 - ratio) + guide['bg_gradient'][1][0] * ratio)
g = int(guide['bg_gradient'][0][1] * (1 - ratio) + guide['bg_gradient'][1][1] * ratio)
b = int(guide['bg_gradient'][0][2] * (1 - ratio) + guide['bg_gradient'][1][2] * ratio)
draw.line([(0, y), (WIDTH, y)], fill=(r, g, b))
# Header Top Bar
# Badge pill
draw_rounded_rect(draw, (20, 14, 150, 36), radius=11, fill=theme_color)
draw.text((28, 17), guide['badge'], font=font_badge, fill=(255, 255, 255))
# Guide Title
draw.text((160, 15), guide['title'], font=font_title, fill=(241, 245, 249))
# Main Card Box in Center
card_bbox = (20, 48, WIDTH - 20, HEIGHT - 38)
draw_rounded_rect(draw, card_bbox, radius=18, fill=(30, 41, 59), outline=(51, 65, 85), width=1)
# Inner subtle highlight top
draw.line([(35, 49), (WIDTH - 35, 49)], fill=(255, 255, 255, 30))
# Left Big Emoji / Icon Circle
# Smooth floating bounce
bounce_y = int(math.sin(t * math.pi * 2) * 3)
icon_cx, icon_cy = 75, 115 + bounce_y
draw.ellipse([icon_cx - 36, icon_cy - 36, icon_cx + 36, icon_cy + 36], fill=(51, 65, 85))
draw.ellipse([icon_cx - 32, icon_cy - 32, icon_cx + 32, icon_cy + 32], fill=(15, 23, 42), outline=theme_color, width=2)
draw.text((icon_cx - 18, icon_cy - 20), scene['icon'], font=font_icon, fill=(255, 255, 255))
# Step Tag
draw_rounded_rect(draw, (130, 68, 235, 88), radius=6, fill=theme_color)
draw.text((138, 71), scene['step'], font=font_step, fill=(255, 255, 255))
# Action Headline
draw.text((130, 96), scene['action'], font=font_action, fill=(255, 255, 255))
# Detailed Points (multiline text)
detail_lines = scene['detail'].split('\n')
y_text = 138
for line in detail_lines:
draw.text((130, y_text), f"• {line}", font=font_detail, fill=(203, 213, 225))
y_text += 22
# Bottom Tip / Safety Banner inside Card
tip_bbox = (35, HEIGHT - 84, WIDTH - 35, HEIGHT - 50)
is_warning = '⚠️' in scene['tip']
tip_bg = (69, 26, 26) if is_warning else (22, 54, 46)
tip_border = (239, 68, 68) if is_warning else theme_color
draw_rounded_rect(draw, tip_bbox, radius=10, fill=tip_bg, outline=tip_border, width=1)
draw.text((45, HEIGHT - 76), scene['tip'], font=font_tip, fill=(255, 255, 255))
# Bottom Global Progress Bar
progress_y = HEIGHT - 18
draw_rounded_rect(draw, (20, progress_y, WIDTH - 20, progress_y + 6), radius=3, fill=(51, 65, 85))
bar_w = int((WIDTH - 40) * overall_progress)
if bar_w > 4:
draw_rounded_rect(draw, (20, progress_y, 20 + bar_w, progress_y + 6), radius=3, fill=theme_color)
# Footer timer
curr_sec = int(frame_idx / FPS)
tot_sec = int(total_frames / FPS)
timer_str = f"{curr_sec:02d}:{int((frame_idx % FPS)/FPS * 100):02d} / {tot_sec:02d}:00 儿科标准护理规范教学"
draw.text((22, HEIGHT - 32), timer_str, font=font_footer, fill=(148, 163, 184))
# Save frame
frame_path = os.path.join(temp_dir, f"frame_{frame_idx:05d}.png")
img.save(frame_path)
frame_idx += 1
print(f"Generated {frame_idx} frames. Encoding with FFmpeg...")
# FFmpeg command to compile frames to H.264 MP4 with pleasant chime audio
# Audio synth: gentle calming ambient drone/chimes
ffmpeg_cmd = [
"ffmpeg", "-y",
"-r", str(FPS),
"-i", os.path.join(temp_dir, "frame_%05d.png"),
"-f", "lavfi",
"-i", f"sine=frequency=440:beep_factor=4:duration={int(total_frames/FPS)}",
"-c:v", "libx264",
"-pix_fmt", "yuv420p",
"-profile:v", "baseline",
"-level", "3.0",
"-movflags", "+faststart",
"-c:a", "aac",
"-b:a", "64k",
"-shortest",
output_mp4
]
res = subprocess.run(ffmpeg_cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
if res.returncode == 0:
file_size = os.path.getsize(output_mp4)
print(f"Successfully generated: {output_mp4} ({file_size / 1024:.1f} KB)")
else:
print(f"FFmpeg error: {res.stderr.decode('utf-8', errors='ignore')}")
# Cleanup temp frames
for f in os.listdir(temp_dir):
os.remove(os.path.join(temp_dir, f))
os.rmdir(temp_dir)
if __name__ == '__main__':
for guide in GUIDE_SCRIPTS:
generate_video(guide)
print("\nAll 6 care guide demonstration videos generated successfully!")