Skip to content

Instantly share code, notes, and snippets.

@Romern
Created April 21, 2019 10:11
Show Gist options
  • Save Romern/9ff892d833249847cc9d6025de46d927 to your computer and use it in GitHub Desktop.
Save Romern/9ff892d833249847cc9d6025de46d927 to your computer and use it in GitHub Desktop.
import soundfile as sf
import pyloudnorm as pyln
from numpy import inf
data, rate = sf.read("example_file.wav") # load audio (with shape (samples, channels))
meter = pyln.Meter(rate) # create BS.1770 meter
threshold = -inf
loudness = []
currently_quiet = False
timestamps = []
timestamps.append(0)
for i in range(0, len(data),rate):
if ((not currently_quiet) and (meter.integrated_loudness(data[i:i + rate]) <= threshold)) or (currently_quiet and (meter.integrated_loudness(data[i:i + rate]) > threshold)):
timestamps.append(i/rate)
currently_quiet = not currently_quiet
with open('outfile.txt', 'a') as the_file:
the_file.write(';FFMETADATA1\n')
for i in range(0, len(timestamps)-1):
the_file.write('[CHAPTER]\nTIMEBASE=1/1000\nSTART={}\nEND={}\ntitle=Chapter {}\n'.format(int(timestamps[i])*1000,int(timestamps[i+1])*1000,i))
print(timestamps)
#;FFMETADATA1
#[CHAPTER]
#TIMEBASE=1/1000
#START=0
#END=10000
#title=Chapter 01
#[CHAPTER]
#TIMEBASE=1/1000
#START=10000
#END=20000
#title=Chapter 02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment