-
Notifications
You must be signed in to change notification settings - Fork 1
/
filter_helper.py
executable file
·49 lines (42 loc) · 1.7 KB
/
filter_helper.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import ast
# Functions that apply filters
def apply_speed_v(vstream, speed_change):
speed_change = max(0.05, speed_change)
if(speed_change >= 0.5):
vstream = vstream.filter('setpts', f'{1.0/speed_change}*PTS')
else:
current_speed = 1.0
while(current_speed >= speed_change):
if(current_speed * 0.5 <= speed_change):
vstream = vstream.filter('setpts', f'{current_speed/speed_change}*PTS')
break
vstream = vstream.filter('setpts', '2*PTS')
current_speed *= 0.5
return vstream
def apply_speed_a(astream, speed_change):
speed_change = max(0.05, speed_change)
if(speed_change >= 0.5):
astream = astream.filter('atempo', speed_change)
else:
current_speed = 1.0
while(current_speed >= speed_change):
if(current_speed * 0.5 <= speed_change):
astream = astream.filter('atempo', speed_change/current_speed)
break
astream = astream.filter('atempo', 0.5)
current_speed *= 0.5
return astream
def apply_speed(vstream, astream, speed_change):
vstream = apply_speed_v(vstream, speed_change)
astream = apply_speed_a(astream, speed_change)
return vstream, astream
# Functions that don't apply filters
def eval_arithmetic(expression):
tree = None
try:
tree = ast.parse(expression, mode='eval')
except SyntaxError:
return None
if not all(isinstance(node, (ast.Expression, ast.UnaryOp, ast.unaryop, ast.BinOp, ast.operator, ast.Num)) for node in ast.walk(tree)):
return None
return eval(compile(tree, filename='', mode='eval'))