pandora_t_for_time/render.py

135 lines
3.4 KiB
Python
Raw Normal View History

2023-10-08 11:19:05 +00:00
#!/usr/bin/python3
import json
import os
import subprocess
import sys
import time
import ox
2023-10-09 13:10:34 +00:00
from .pi import random
from .render_kdenlive import KDEnliveProject
2023-10-08 11:19:05 +00:00
def random_choice(seq, items, pop=False):
n = n_ = len(items) - 1
#print('len', n)
if n == 0:
if pop:
return items.pop(n)
return items[n]
2023-10-08 11:19:05 +00:00
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': [],
}
}
all_clips = clips.copy()
2023-10-08 11:19:05 +00:00
seq = random(base)
while target - length > 10 and clips:
clip = random_choice(seq, clips, True)
if not clips:
clips = [c for c in all_clips if c != clip]
2023-10-08 11:19:05 +00:00
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
2023-10-09 13:10:34 +00:00
if blur:
scene['back']['V2'][-1]['filter']['blur'] = blur
2023-10-08 11:19:05 +00:00
scene['audio']['A1'].append({
'duration': clip['duration'],
'src': clip['original'],
})
scene['audio']['A2'].append({
'duration': clip['duration'],
'src': clip['foreground'],
})
return scene
2023-10-09 13:10:34 +00:00
def render(root, scene, prefix=''):
2023-10-08 11:19:05 +00:00
fps = 24
files = []
2023-10-08 11:19:05 +00:00
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)
path = os.path.join(root, prefix + "%s.kdenlive" % timeline)
with open(path, 'w') as fd:
2023-10-08 11:19:05 +00:00
fd.write(project.to_xml())
files.append(path)
return files