pandora_t_for_time/render.py
2023-10-08 12:19:05 +01:00

126 lines
3.1 KiB
Python

#!/usr/bin/python3
import json
import os
import subprocess
import sys
import time
import ox
from pi import random
from render_kdenlive import KDEnliveProject
def random_choice(seq, items, pop=False):
n = n_ = len(items) - 1
#print('len', n)
if n == 0:
return items[0]
r = seq()
base = 10
while n > 10:
n /= 10
#print(r)
r += seq()
base += 10
r = int(n_ * r / base)
#print('result', r, items)
if pop:
return items.pop(r)
return items[r]
def chance(seq, chance):
return (seq() / 10) >= chance
def compose(clips, target=150, base=1024):
length = 0
scene = {
'front': {
'V1': [],
'V2': [],
},
'back': {
'V1': [],
'V2': [],
},
'audio': {
'A1': [],
'A2': [],
'A3': [],
'A4': [],
}
}
seq = random(base)
while target - length > 10 and clips:
clip = random_choice(seq, clips, True)
if length + clip['duration'] > target:
break
length += clip['duration']
scene['front']['V1'].append({
'duration': clip['duration'],
'src': clip['foreground'],
"filter": {
'transparency': seq() / 10,
}
})
transparency = seq() / 10
# coin flip which site is visible (50% chance)
if chance(seq, 0.5):
transparency_front = transparency
transparency_back = 0
else:
transparency_back = transparency
transparency_front = 0
scene['front']['V2'].append({
'duration': clip['duration'],
'src': clip['background'],
"filter": {
'transparency': transparency_front
}
})
scene['back']['V1'].append({
'duration': clip['duration'],
'src': clip['background'],
"filter": {
'transparency': transparency_back
}
})
scene['back']['V2'].append({
'duration': clip['duration'],
'src': clip['original'],
"filter": {
'transparency': seq() / 10,
}
})
# 50 % chance to blur original from 0 to 30
if chance(seq, 0.5):
blur = seq() * 3
scene['back']['V2'][-1]['filter']['blur'] = blur
scene['audio']['A1'].append({
'duration': clip['duration'],
'src': clip['original'],
})
scene['audio']['A2'].append({
'duration': clip['duration'],
'src': clip['foreground'],
})
return scene
def render(root, scene):
fps = 24
for timeline, data in scene.items():
print(timeline)
project = KDEnliveProject(root)
tracks = []
for track, clips in data.items():
print(track)
for clip in clips:
project.append_clip(track, clip)
with open(os.path.join(prefix, "%s.kdenlive" % timeline), 'w') as fd:
fd.write(project.to_xml())