pandora_t_for_time/render.py

134 lines
3.4 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:
if pop:
return items.pop(n)
return items[n]
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()
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]
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
if blur:
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, prefix=''):
fps = 24
files = []
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:
fd.write(project.to_xml())
files.append(path)
return files