MagicPodcast/generate_demuxer.py

92 lines
2.4 KiB
Python

import math
from find_loudness import process_find_loudness
graph_density = 8000
threshold_at_point = 1
inertia_s = 0.3
inertia_samples = inertia_s * graph_density
def s(n: int):
global graph_density
return n / graph_density
def seconds(units: int):
return math.floor(s(units) % 60)
def minutes(units: int):
return math.floor(s(units) / 60)
def hours(units: int):
return math.floor(s(units) / 60 / 60)
def formatTime(units: int):
return f"{hours(units)}:{minutes(units)}:{seconds(units)}"
def new_mode(m, s):
data = m
data[s['label']] = s['loud']
return data
def mode_to_string(mode):
if mode['left'] and mode['right']:
return 'both'
elif mode['left']:
return 'left'
elif mode['right']:
return 'right'
else:
return "none"
def run(tmp_dir):
global inertia_samples
out_demuxer = 'demuxer.txt'
with open(out_demuxer, 'w') as demuxer:
# Execute process_find_loudness for left and right side
left_loudness = process_find_loudness(
tmp_dir + "/audio/leftraw",
threshold_at_point=threshold_at_point,
inertia_samples=inertia_samples,
label="left")
right_loudness = process_find_loudness(
tmp_dir + "/audio/rightraw",
threshold_at_point=threshold_at_point,
inertia_samples=inertia_samples,
label="right")
merged = [*left_loudness, *right_loudness]
sorted_list = sorted(merged, key=lambda x: x['position_start'])
demuxer.write(F"file {tmp_dir}/pics/none.png\n")
last_point = 0
mode = {'left': False, 'right': False}
last_file = ''
total = 0
for i in range(2, len(sorted_list)):
point = sorted_list[i]
mode = new_mode(m=mode, s=point)
file = F"{tmp_dir}/pics/{mode_to_string(mode)}.png"
duration = (point['position_start'] - last_point) / graph_density
demuxer.write(F"duration {duration}\n")
demuxer.write(F"file {file}\n")
last_point = point['position_start']
last_file = file
total += duration * graph_density
demuxer.write(F"duration {sorted_list[len(sorted_list) - 1]['duration'] / graph_density}\n")
demuxer.write(F"file {last_file}\n")
print(F"{total} {formatTime(total)}\n")