MagicPodcast/generate_demuxer.py

110 lines
2.8 KiB
Python
Raw Normal View History

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 set_up_threshold(new_value: int):
"""Setup threshold value"""
global threshold_at_point
if new_value > 0 and isinstance(new_value, int):
threshold_at_point = int(new_value)
return True
else:
return False
def s(n: int):
"""Frame conversions"""
global graph_density
return n / graph_density
def seconds(units: int):
"""Return seconds."""
return math.floor(s(units) % 60)
def minutes(units: int):
"""Return minutes."""
return math.floor(s(units) / 60)
def hours(units: int):
"""Return hours."""
return math.floor(s(units) / 60 / 60)
def formatTime(units: int):
"""Format time HH:MM:SS."""
return f"{hours(units)}:{minutes(units)}:{seconds(units)}"
def new_mode(m, s):
"""Set new node"""
data = m
data[s['label']] = s['loud']
return data
def mode_to_string(mode):
"""Convert mode type to string"""
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):
"""Main method to run generating demuxer.txt"""
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")