Spaces:
Running
Running
Update: ≤3s WeChat-compatible live photos, sets 3 & 4 (fill-in-the-blank), in-browser maker tool
0daf81d verified Download source/studio/package.py from Ethscriptions/eths: direct link, hf CLI and curl.
- Browser
- Download file 7.98 kB
-
https://huggingface.co/spaces/Ethscriptions/eths/resolve/main/source/studio/package.py
- Command line
-
hf download hf://spaces/Ethscriptions/eths/source/studio/package.py
-
curl -L -o package.py https://huggingface.co/spaces/Ethscriptions/eths/resolve/main/source/studio/package.py
7.98 kB
| """Pair each rendered clip as an Apple Live Photo (JPG + MOV with audio), build web previews and per-set download ZIPs.""" | |
| import json, os, shutil, subprocess, sys, time, zipfile | |
| sys.path.insert(0, '/workspaces/studio') | |
| from pair_live_photo import pair | |
| OUT = '/tmp/work/out' | |
| PUB = '/workspaces/frontend/public/live' | |
| os.makedirs(PUB, exist_ok=True) | |
| SETS = [ | |
| dict(key='a', items='items.json', name='第一组 · 生活照(AI 生成)', folder='日常英语实况图-第一组', zip='daily-english-live-photos.zip', | |
| title='9个天天见的东西,英语你能说出几个?', | |
| intro='每天都在用,却不一定叫得出英文名👀'), | |
| dict(key='b', items='items2.json', name='第二组 · 真实照片(网络授权图片)', folder='日常英语实况图-第二组', zip='daily-english-live-photos-set2.zip', | |
| title='这9个东西天天见,英语你都会说吗?', intro='天天见到的东西,换成英文就卡壳?👀', credits='credits2.json'), | |
| dict(key='c', items='items3.json', name='第三组 · 看图填空(G _ _)', folder='日常英语实况图-第三组-看图填空', | |
| zip='daily-english-live-photos-set3.zip', title='看图填空:G _ _ 你能填对几个?', | |
| intro='全是三个字母的单词,看图猜一猜是什么👀', hide_list=True, | |
| tags='#英语单词 #看图猜单词 #英语学习 #实况图 #每日英语 #单词打卡'), | |
| dict(key='d', items='items4.json', name='第四组 · 看图填空(_ A Y)', folder='日常英语实况图-第四组-看图填空', | |
| zip='daily-english-live-photos-set4.zip', title='看图填空:_ A Y 你能填对几个?', | |
| intro='后面都是 AY,就差第一个字母,看图猜一猜👀', hide_list=True, | |
| tags='#英语单词 #看图猜单词 #英语学习 #实况图 #每日英语 #单词打卡'), | |
| ] | |
| README = """日常英语 · 实况图素材包({n} 张) | |
| 每张实况图 = 一对同名的 .JPG + .MOV(例如 {example}.JPG / {example}.MOV)。 | |
| 两个文件里写入了相同的苹果实况标识,视频带声音(英语发音 + 音效)。 | |
| Mac 导入方法: | |
| 1. 解压本压缩包; | |
| 2. 全选全部 JPG 和 MOV,一起拖进「照片」App(或 照片 → 文件 → 导入); | |
| 3. 照片会把每一对识别为一张 LIVE 实况照片; | |
| 4. 通过 iCloud 照片或隔空投送到 iPhone,在小红书 / 抖音发图文时选择这些照片并保持「实况」开启。 | |
| 注意:JPG 和 MOV 必须成对导入;不要用其他软件重新转码 MOV,否则会丢失配对信息。 | |
| 尺寸 1080×1440(3:4 竖屏),30fps,H.264 + AAC。 | |
| 建议标题:{title} | |
| 建议正文: | |
| {body} | |
| """ | |
| def body_for(s, items): | |
| return (s['intro'] + '\n先看封面猜一猜,再长按实况看答案(记得开声音🔊)\n评论区报一下你答对了几个~\n\n' | |
| + ('' if s.get('hide_list') else ' / '.join(i['zh'] for i in items) + '\n\n') | |
| + ('图片来源:Wikimedia Commons(作者与授权见素材包内说明)\n\n' if s.get('credits') else '') | |
| + s.get('tags', '#英语单词 #日常英语 #英语口语 #实况图 #每日英语 #英语学习 #单词打卡')) | |
| def build(s): | |
| items = json.load(open(f"/workspaces/studio/{s['items']}")) | |
| dist = f"/workspaces/.work/dist/{s['folder']}" | |
| shutil.rmtree(dist, ignore_errors=True) | |
| os.makedirs(dist) | |
| out_items = [] | |
| for k, it in enumerate(items, 1): | |
| d = os.path.join(OUT, it['id']) | |
| meta = json.load(open(os.path.join(d, 'render.json'))) | |
| live = os.path.join(d, 'live') | |
| shutil.rmtree(live, ignore_errors=True) | |
| m = pair(os.path.join(d, 'cover.jpg'), os.path.join(d, 'motion.mp4'), live, meta['coverTime'], meta['fps']) | |
| assert m['metadataVerified'] | |
| base = f"LIVE_{it['id']}_{it['en'].replace(' ', '-')}" | |
| shutil.copy(os.path.join(live, 'live.jpg'), os.path.join(dist, base + '.JPG')) | |
| shutil.copy(os.path.join(live, 'live.mov'), os.path.join(dist, base + '.MOV')) | |
| subprocess.run(['ffmpeg', '-v', 'error', '-y', '-i', os.path.join(d, 'cover.jpg'), '-vf', 'scale=720:-2', '-q:v', '3', | |
| os.path.join(PUB, f"cover_{it['id']}.jpg")], check=True) | |
| subprocess.run(['ffmpeg', '-v', 'error', '-y', '-i', os.path.join(d, 'motion.mp4'), '-vf', 'scale=720:-2', | |
| '-c:v', 'libx264', '-crf', '24', '-preset', 'medium', '-pix_fmt', 'yuv420p', '-c:a', 'aac', '-b:a', '128k', | |
| '-movflags', '+faststart', os.path.join(PUB, f"preview_{it['id']}.mp4")], check=True) | |
| out_items.append({'id': it['id'], 'index': k, 'zh': it['zh'], 'en': it['en'], 'ipa': it['ipa'], 'cat': it['cat'], | |
| 'duration': round(meta['duration'], 2), 'cover': f"cover_{it['id']}.jpg", | |
| 'preview': f"preview_{it['id']}.mp4", 'identifier': m['identifier']}) | |
| print('paired', base, flush=True) | |
| body = body_for(s, items) | |
| open(os.path.join(dist, '使用说明.txt'), 'w', encoding='utf-8').write( | |
| README.format(n=len(items), example=f"LIVE_{items[0]['id']}_{items[0]['en'].replace(' ', '-')}", title=s['title'], body=body)) | |
| credits = [] | |
| if s.get('credits'): | |
| credits = json.load(open(f"/workspaces/studio/{s['credits']}")) | |
| lines = ['图片来源与授权(均为允许商用的知识共享 / 公有领域授权)', '', | |
| '署名(BY)类授权要求注明作者;署名-相同方式共享(BY-SA)类授权要求衍生作品以相同授权发布。', ''] | |
| for c in credits: | |
| lines += [f"{c['id']} {c['zh']} / {c['en']}", f" 文件:{c['file']}", f" 作者:{c['author']}", | |
| f" 授权:{c['license']} {c.get('license_url') or ''}", f" 来源:{c['page']}", ''] | |
| open(os.path.join(dist, '图片来源与授权.txt'), 'w', encoding='utf-8').write('\n'.join(lines)) | |
| zpath = os.path.join(PUB, s['zip']) | |
| with zipfile.ZipFile(zpath, 'w', compression=zipfile.ZIP_STORED) as z: | |
| for f in sorted(os.listdir(dist)): | |
| z.write(os.path.join(dist, f), arcname=f"{s['folder']}/{f}") | |
| mb = round(os.path.getsize(zpath) / 1024 / 1024, 1) | |
| print('zip', zpath, mb, 'MB', flush=True) | |
| return {'key': s['key'], 'name': s['name'], 'zip': s['zip'], 'zipMB': mb, 'items': out_items, | |
| 'copy': {'title': s['title'], 'body': body}, | |
| 'credits': [{'id': c['id'], 'zh': c['zh'], 'en': c['en'], 'author': c['author'], 'license': c['license'], | |
| 'licenseUrl': c.get('license_url') or '', 'page': c['page']} for c in credits]} | |
| only = sys.argv[1].split(',') if len(sys.argv) > 1 else [s['key'] for s in SETS] | |
| manifest_path = '/tmp/work/pack_manifest.json' | |
| prev = json.load(open(manifest_path)) if os.path.exists(manifest_path) else {} | |
| for s in SETS: | |
| if s['key'] in only: | |
| prev[s['key']] = build(s) | |
| json.dump(prev, open(manifest_path, 'w'), ensure_ascii=False) | |
| pack = {'version': str(int(time.time())), 'sets': [prev[s['key']] for s in SETS if s['key'] in prev]} | |
| ts = """// Generated by studio/package.py — Live Photo pack manifest. | |
| export interface LiveItem { | |
| id: string | |
| index: number | |
| zh: string | |
| en: string | |
| ipa: string | |
| cat: string | |
| duration: number | |
| cover: string | |
| preview: string | |
| identifier: string | |
| } | |
| export interface Credit { | |
| id: string | |
| zh: string | |
| en: string | |
| author: string | |
| license: string | |
| licenseUrl: string | |
| page: string | |
| } | |
| export interface LiveSet { | |
| key: string | |
| name: string | |
| zip: string | |
| zipMB: number | |
| items: LiveItem[] | |
| copy: { title: string; body: string } | |
| credits: Credit[] | |
| } | |
| export interface LivePack { | |
| version: string | |
| sets: LiveSet[] | |
| } | |
| export const pack: LivePack = """ + json.dumps(pack, ensure_ascii=False, indent=2) + "\n" | |
| open('/workspaces/frontend/src/data/livePack.ts', 'w', encoding='utf-8').write(ts) | |
| print('manifest written', [s['key'] for s in pack['sets']]) | |