coderdecoder01 commited on
Commit
fa0a5bf
·
verified ·
1 Parent(s): 5a8be38

Her exact quote wording + synthesised Indian instrumental soundtrack

Browse files
Files changed (1) hide show
  1. render_invitation.py +142 -18
render_invitation.py CHANGED
@@ -1,15 +1,17 @@
1
  #!/usr/bin/env python3
2
  """
3
- Indian-style animated retirement invitation for Mamta: 1080x1920, 30fps, H.264 MP4.
4
  Maroon/gold palette, rotating mandala, marigold toran, flickering diyas, colour emoji.
5
- v4: legibility pass - critical text in Playfair Display Medium, accent text in weighted Cormorant.
6
  """
7
- import os, math, subprocess, urllib.request, random
8
  import numpy as np
9
  from PIL import Image, ImageDraw, ImageFont, ImageFilter, features
10
 
11
  W, H, FPS = 1080, 1920, 30
 
12
  OUT = "/tmp/mamta_retirement_invitation.mp4"
 
13
  POSTER = "/tmp/poster.jpg"
14
  FDIR = "/tmp/fonts"
15
  os.makedirs(FDIR, exist_ok=True)
@@ -29,8 +31,6 @@ EV = dict(
29
  kicker2="IN HONOUR OF",
30
  body2="on the joyous occasion of her retirement",
31
  small2="30 SEPTEMBER 2026",
32
- # 'serif' = Playfair Display (sturdier, used for anything that must be read clearly)
33
- # 'bodyi' = Cormorant Italic (decorative flourishes only)
34
  message=[
35
  dict(kind="bodyi", size=54, color=CREAM, weight=600,
36
  lines=["\u201cThe bell is ringing one last time\u2026\u201d"]),
@@ -49,7 +49,7 @@ EV = dict(
49
  where_label="WHERE",
50
  where_1="New Ros Common, Kasauli",
51
  sub3="Your presence will make it complete",
52
- quote=["The bell has rung for the last time,", "but your lessons will echo on."],
53
  kicker6="WITH LOVE AND BLESSINGS",
54
  script6="Celebrate With You",
55
  sig="Mamta & family",
@@ -91,7 +91,6 @@ HAS_HINDI = HAS_RAQM and "hindi" in FPATH
91
  print("RAQM:", HAS_RAQM, "| Devanagari usable:", HAS_HINDI, flush=True)
92
 
93
  def _set_weight(f, weight):
94
- """Set only the wght axis of a variable font, leaving other axes at default."""
95
  if not weight:
96
  return
97
  try:
@@ -105,16 +104,9 @@ def _set_weight(f, weight):
105
  vals.append(weight if "eight" in str(name) else dflt)
106
  try:
107
  f.set_variation_by_axes(vals)
108
- print(" weight %s -> %s on %s" % (weight, vals, name_of(f)), flush=True)
109
  except Exception as e:
110
  print(" weight FAIL %s" % repr(e)[:80], flush=True)
111
 
112
- def name_of(f):
113
- try:
114
- return f.getname()
115
- except Exception:
116
- return "?"
117
-
118
  _fc = {}
119
  def F(kind, size, weight=None):
120
  key = (kind, size, weight)
@@ -435,6 +427,131 @@ def make_message_card(width=900, sig=None, sig_size=64):
435
  y += sl.size[1]
436
  return card.crop((0, 0, width, min(Hc, y + 76)))
437
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
438
  # ----------------------------------------------------------------- build assets
439
  print("building assets ...", flush=True)
440
  BG = build_bg().convert("RGBA")
@@ -499,6 +616,8 @@ TOTAL = LAST_END + HOLD
499
  NF = int(round(TOTAL * FPS))
500
  print("timeline start:", [round(s, 2) for s in start], "total %.2fs %d frames" % (TOTAL, NF), flush=True)
501
 
 
 
502
  def scene_alpha(i, t):
503
  s = start[i]
504
  e = TOTAL if i == len(DUR) - 1 else start[i] + DUR[i]
@@ -570,14 +689,18 @@ def render(t):
570
  return Image.fromarray((np.asarray(rgb, dtype=np.float32) * k).astype(np.uint8), "RGB")
571
  return rgb
572
 
573
- # ----------------------------------------------------------------- encode
574
  from imageio_ffmpeg import get_ffmpeg_exe
575
  FFMPEG = get_ffmpeg_exe()
576
  print("ffmpeg:", FFMPEG, flush=True)
577
- proc = subprocess.Popen([FFMPEG, "-y", "-loglevel", "error", "-f", "rawvideo", "-pix_fmt", "rgb24",
578
- "-s", "%dx%d" % (W, H), "-r", str(FPS), "-i", "-",
 
 
 
579
  "-c:v", "libx264", "-preset", "slow", "-crf", "18", "-pix_fmt", "yuv420p",
580
- "-movflags", "+faststart", "-an", OUT], stdin=subprocess.PIPE)
 
581
 
582
  poster_t = start[1] + 2.6
583
  for n in range(NF):
@@ -613,6 +736,7 @@ try:
613
  except Exception as e:
614
  print("visibility:", repr(e)[:120], flush=True)
615
  api.upload_file(path_or_fileobj=OUT, path_in_repo="mamta-retirement-invitation.mp4", repo_id=REPO, repo_type="dataset")
 
616
  api.upload_file(path_or_fileobj=POSTER, path_in_repo="poster.jpg", repo_id=REPO, repo_type="dataset")
617
  for fp in previews:
618
  api.upload_file(path_or_fileobj=fp, path_in_repo="previews/" + os.path.basename(fp), repo_id=REPO, repo_type="dataset")
 
1
  #!/usr/bin/env python3
2
  """
3
+ Indian-style animated retirement invitation for Mamta: 1080x1920, 30fps, H.264 + AAC MP4.
4
  Maroon/gold palette, rotating mandala, marigold toran, flickering diyas, colour emoji.
5
+ v5: her exact quote wording + original synthesised soundtrack (tanpura, sitar melody, bells).
6
  """
7
+ import os, math, subprocess, urllib.request, random, wave
8
  import numpy as np
9
  from PIL import Image, ImageDraw, ImageFont, ImageFilter, features
10
 
11
  W, H, FPS = 1080, 1920, 30
12
+ SR = 44100
13
  OUT = "/tmp/mamta_retirement_invitation.mp4"
14
+ WAV = "/tmp/soundtrack.wav"
15
  POSTER = "/tmp/poster.jpg"
16
  FDIR = "/tmp/fonts"
17
  os.makedirs(FDIR, exist_ok=True)
 
31
  kicker2="IN HONOUR OF",
32
  body2="on the joyous occasion of her retirement",
33
  small2="30 SEPTEMBER 2026",
 
 
34
  message=[
35
  dict(kind="bodyi", size=54, color=CREAM, weight=600,
36
  lines=["\u201cThe bell is ringing one last time\u2026\u201d"]),
 
49
  where_label="WHERE",
50
  where_1="New Ros Common, Kasauli",
51
  sub3="Your presence will make it complete",
52
+ quote=["The bell has rung,", "but the lessons will echo on."],
53
  kicker6="WITH LOVE AND BLESSINGS",
54
  script6="Celebrate With You",
55
  sig="Mamta & family",
 
91
  print("RAQM:", HAS_RAQM, "| Devanagari usable:", HAS_HINDI, flush=True)
92
 
93
  def _set_weight(f, weight):
 
94
  if not weight:
95
  return
96
  try:
 
104
  vals.append(weight if "eight" in str(name) else dflt)
105
  try:
106
  f.set_variation_by_axes(vals)
 
107
  except Exception as e:
108
  print(" weight FAIL %s" % repr(e)[:80], flush=True)
109
 
 
 
 
 
 
 
110
  _fc = {}
111
  def F(kind, size, weight=None):
112
  key = (kind, size, weight)
 
427
  y += sl.size[1]
428
  return card.crop((0, 0, width, min(Hc, y + 76)))
429
 
430
+ # ----------------------------------------------------------------- soundtrack
431
+ def ks_pluck(freq, dur, damp=0.9965, bright=0.42, seed=0):
432
+ """Karplus-Strong plucked string (sitar/tanpura-like)."""
433
+ rng = np.random.default_rng(seed)
434
+ N = max(2, int(round(SR / float(freq))))
435
+ buf = rng.uniform(-1.0, 1.0, N).astype(np.float32)
436
+ for _ in range(2):
437
+ buf = 0.5 * (buf + np.roll(buf, 1))
438
+ buf /= (np.abs(buf).max() + 1e-9)
439
+ n = int(dur * SR)
440
+ out = np.empty(n, dtype=np.float32)
441
+ a, b = 0.5 * (1.0 + bright), 0.5 * (1.0 - bright)
442
+ idx = 0
443
+ for i in range(n):
444
+ cur = buf[idx]
445
+ out[i] = cur
446
+ buf[idx] = damp * (a * cur + b * buf[(idx + 1) % N])
447
+ idx = (idx + 1) % N
448
+ return out
449
+
450
+ def make_bell(dur, base=587.33, decay=1.9):
451
+ t = np.arange(int(dur * SR), dtype=np.float32) / SR
452
+ out = np.zeros_like(t)
453
+ for mult, amp in ((1.0, 1.0), (2.0, 0.34), (2.76, 0.42), (4.07, 0.16), (5.43, 0.09)):
454
+ out += amp * np.sin(2 * np.pi * base * mult * t) * np.exp(-t * (1.6 / decay) * (mult ** 0.4))
455
+ return out / (np.abs(out).max() + 1e-9)
456
+
457
+ def make_pad(dur, chord=(146.83, 220.0, 293.66), amp=1.0):
458
+ t = np.arange(int(dur * SR), dtype=np.float32) / SR
459
+ out = np.zeros_like(t)
460
+ for i, f in enumerate(chord):
461
+ vib = 1.0 + 0.0016 * np.sin(2 * np.pi * (0.16 + 0.05 * i) * t)
462
+ out += np.sin(2 * np.pi * f * vib * t) / (i + 2.0)
463
+ swell = np.minimum(1.0, t / 4.0)
464
+ return amp * swell * out / (np.abs(out).max() + 1e-9)
465
+
466
+ def reverb(x, seed, mix=0.34, decay=1.7, sr=SR):
467
+ rng = np.random.default_rng(seed)
468
+ n = int(decay * sr)
469
+ ir = (rng.standard_normal(n) * np.exp(-np.arange(n) / (0.30 * sr))).astype(np.float32)
470
+ ir[0] = 1.0
471
+ ir /= np.abs(ir).sum() ** 0.5
472
+ L = 1
473
+ while L < len(x) + n:
474
+ L *= 2
475
+ y = np.fft.irfft(np.fft.rfft(x, L) * np.fft.rfft(ir, L), L)[:len(x)].astype(np.float32)
476
+ pk = np.abs(x).max() + 1e-9
477
+ y = y / (np.abs(y).max() + 1e-9) * pk
478
+ return (1.0 - mix) * x + mix * y
479
+
480
+ def build_soundtrack(total):
481
+ S = int((total + 1.2) * SR)
482
+ bus = {k: np.zeros(S, dtype=np.float32) for k in ("drone", "mel", "bell", "pad")}
483
+
484
+ def add(which, t0, sig, amp):
485
+ i = int(t0 * SR)
486
+ if i >= S:
487
+ return
488
+ j = min(S, i + len(sig))
489
+ bus[which][i:j] += sig[:j - i] * amp
490
+
491
+ SA = 146.83 # D3
492
+ def nf(semi):
493
+ return SA * (2.0 ** (semi / 12.0))
494
+
495
+ # tanpura drone: Pa - sa - sa - sa cycle
496
+ cycle = [(7, 0.90), (0, 0.85), (12, 0.72), (12, 0.66)]
497
+ t0, k = 0.0, 0
498
+ while t0 < total:
499
+ semi, amp = cycle[k % len(cycle)]
500
+ add("drone", t0, ks_pluck(nf(semi), 3.4, damp=0.9975, bright=0.30, seed=k * 7 + 1), 0.15 * amp)
501
+ t0 += 2.15
502
+ k += 1
503
+
504
+ # sitar-like melody in Raag Bhoopali (Sa Re Ga Pa Dha)
505
+ melody = [(0.80, 0, 3.0, 0.20), (2.30, 4, 2.6, 0.17), (3.70, 7, 2.8, 0.17),
506
+ (5.30, 9, 2.6, 0.17), (6.90, 12, 3.2, 0.18),
507
+ (9.20, 14, 3.0, 0.15), (11.20, 12, 2.8, 0.14), (13.40, 9, 3.0, 0.14),
508
+ (15.20, 7, 3.0, 0.15),
509
+ (17.40, 12, 2.6, 0.17), (19.00, 9, 2.6, 0.16), (20.60, 12, 2.8, 0.16),
510
+ (22.40, 16, 3.2, 0.17), (24.40, 19, 3.2, 0.18), (26.60, 21, 3.4, 0.18),
511
+ (28.60, 24, 3.6, 0.19), (30.80, 19, 3.6, 0.16), (32.20, 12, 4.6, 0.16)]
512
+ for i, (t0, semi, dur, amp) in enumerate(melody):
513
+ if t0 > total:
514
+ continue
515
+ add("mel", t0, ks_pluck(nf(semi), dur, damp=0.9982, bright=0.46, seed=100 + i * 13), amp)
516
+
517
+ # temple bells on scene changes
518
+ for i, st in enumerate([0.0, 3.95, 8.4, 16.35, 22.8, 26.75]):
519
+ big = i in (0, 5)
520
+ add("bell", st, make_bell(5.0, base=587.33 if big else 493.88, decay=2.2 if big else 1.7),
521
+ 0.16 if big else 0.095)
522
+
523
+ # warm sustained pad
524
+ add("pad", 0.4, make_pad(total + 1.0, amp=1.0), 0.085)
525
+
526
+ # gentle lowpass to keep it soft
527
+ kernel = np.hanning(9)
528
+ kernel /= kernel.sum()
529
+ def lp(x):
530
+ return np.convolve(x, kernel, mode="same").astype(np.float32)
531
+ for k in bus:
532
+ bus[k] = lp(bus[k])
533
+
534
+ L = reverb(bus["drone"] * 0.95 + bus["mel"] + bus["bell"] + bus["pad"], seed=5)
535
+ R = reverb(bus["drone"] * 1.00 + bus["mel"] * 0.94 + bus["bell"] * 0.98 + bus["pad"], seed=9)
536
+ st = np.stack([L, R], axis=1)
537
+
538
+ n = st.shape[0]
539
+ fade_in = int(1.2 * SR)
540
+ fade_out = int(3.0 * SR)
541
+ env = np.ones(n, dtype=np.float32)
542
+ env[:fade_in] = np.linspace(0.0, 1.0, fade_in) ** 0.7
543
+ env[n - fade_out:] = np.linspace(1.0, 0.0, fade_out) ** 1.1
544
+ st *= env[:, None]
545
+
546
+ peak = np.abs(st).max() + 1e-9
547
+ st = np.tanh(st / peak * 1.15) * 0.72
548
+ st = st[:int(total * SR)]
549
+ data = np.clip(st, -1.0, 1.0)
550
+ with wave.open(WAV, "wb") as w:
551
+ w.setnchannels(2); w.setsampwidth(2); w.setframerate(SR)
552
+ w.writeframes((data * 32767.0).astype("<i2").tobytes())
553
+ print("soundtrack: %s %.2fs peak=%.2f" % (WAV, st.shape[0] / float(SR), np.abs(st).max()), flush=True)
554
+
555
  # ----------------------------------------------------------------- build assets
556
  print("building assets ...", flush=True)
557
  BG = build_bg().convert("RGBA")
 
616
  NF = int(round(TOTAL * FPS))
617
  print("timeline start:", [round(s, 2) for s in start], "total %.2fs %d frames" % (TOTAL, NF), flush=True)
618
 
619
+ build_soundtrack(TOTAL)
620
+
621
  def scene_alpha(i, t):
622
  s = start[i]
623
  e = TOTAL if i == len(DUR) - 1 else start[i] + DUR[i]
 
689
  return Image.fromarray((np.asarray(rgb, dtype=np.float32) * k).astype(np.uint8), "RGB")
690
  return rgb
691
 
692
+ # ----------------------------------------------------------------- encode (video + soundtrack)
693
  from imageio_ffmpeg import get_ffmpeg_exe
694
  FFMPEG = get_ffmpeg_exe()
695
  print("ffmpeg:", FFMPEG, flush=True)
696
+ proc = subprocess.Popen([FFMPEG, "-y", "-loglevel", "error",
697
+ "-f", "rawvideo", "-pix_fmt", "rgb24", "-s", "%dx%d" % (W, H),
698
+ "-r", str(FPS), "-i", "-",
699
+ "-i", WAV,
700
+ "-map", "0:v:0", "-map", "1:a:0",
701
  "-c:v", "libx264", "-preset", "slow", "-crf", "18", "-pix_fmt", "yuv420p",
702
+ "-c:a", "aac", "-b:a", "192k", "-ar", str(SR),
703
+ "-shortest", "-movflags", "+faststart", OUT], stdin=subprocess.PIPE)
704
 
705
  poster_t = start[1] + 2.6
706
  for n in range(NF):
 
736
  except Exception as e:
737
  print("visibility:", repr(e)[:120], flush=True)
738
  api.upload_file(path_or_fileobj=OUT, path_in_repo="mamta-retirement-invitation.mp4", repo_id=REPO, repo_type="dataset")
739
+ api.upload_file(path_or_fileobj=WAV, path_in_repo="soundtrack.wav", repo_id=REPO, repo_type="dataset")
740
  api.upload_file(path_or_fileobj=POSTER, path_in_repo="poster.jpg", repo_id=REPO, repo_type="dataset")
741
  for fp in previews:
742
  api.upload_file(path_or_fileobj=fp, path_in_repo="previews/" + os.path.basename(fp), repo_id=REPO, repo_type="dataset")