Last active
January 2, 2016 07:49
-
-
Save kpj/8272359 to your computer and use it in GitHub Desktop.
Non-working test
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# requires ffmpeg, mpgtx | |
import sys, os, os.path, datetime | |
import numpy | |
from scipy.io.wavfile import read | |
# adjust this! | |
save_for_plot = False | |
volume_threshold = 250 | |
if len(sys.argv) != 2: | |
sys.exit(1) | |
video_file = sys.argv[1] + ".mpg" | |
final_file = "final_" + video_file | |
# helper functions | |
def frmt(secs): | |
"""Turns seconds into suitable format | |
""" | |
return str(datetime.timedelta(seconds=round(secs))) | |
def aggregate_channel(chan): | |
"""Gathers information of one channel into one number | |
""" | |
return abs(numpy.average(chan)) | |
def save_data(sample_rate, data): | |
with open('data', 'w') as fd: | |
for i, d in enumerate(data): | |
if i % sample_rate != 0: | |
continue | |
ac = aggregate_channel(d) | |
t = 0 if i == 0 else 1 / (sample_rate / i) | |
fd.write('%f %i\n' % (t, ac)) | |
# convert video to mpg | |
if not os.path.exists(video_file): | |
os.system('ffmpeg -i "%s" "%s"' % (sys.argv[1], video_file)) | |
# extract audio | |
audio_file = video_file + ".wav" | |
if not os.path.exists(audio_file): | |
os.system('ffmpeg -i "%s" -ab 160k -ac 2 -ar 44100 -vn "%s"' % (video_file, audio_file)) | |
# read audio | |
sample_rate, data = read(audio_file) | |
# sample rate: # of samples per second | |
sample_num, channel_num = data.shape | |
# only save if wanted | |
if save_for_plot: | |
save_data(sample_rate, data) | |
sys.exit() | |
# normalize audio | |
#dtype = numpy.float64 | |
#dtype = numpy.dtype(dtype) | |
#data = data.astype(dtype) / dtype.type(-numpy.iinfo(data.dtype).min) | |
# analyze audio | |
above = True | |
intersections = [] | |
last_entries = [] | |
le_len = 1000 | |
# get intersections | |
for i, d in enumerate(data): | |
# calc time | |
t = 0 if i == 0 else 1 / (sample_rate / i) | |
# handle average list | |
last_entries.append(aggregate_channel(d)) | |
if len(last_entries) > le_len: | |
last_entries.pop(0) | |
avg = numpy.average(last_entries) | |
if avg < volume_threshold: | |
if above: | |
# were above threshold in previous sample | |
intersections.append(t) | |
above = False | |
else: | |
if not above: | |
# were below threshold in last sample | |
intersections.append(t) | |
above = True | |
intersections.remove(0) | |
# get matching pairs | |
seqs = "" | |
i = 0 | |
while i < len(intersections) - 1: | |
t1 = frmt(intersections[i]) | |
t2 = frmt(intersections[i+1]) | |
if t1 != t2: | |
seqs += "[%s-%s] " % (t1, t2) | |
i += 2 | |
# cut video | |
print(seqs) | |
if os.path.exists(final_file): | |
os.remove(final_file) | |
os.system('mpgtx -j "%s" %s -o "%s"' % (video_file, seqs, final_file)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment