#!/usr/bin/env python3 from datetime import timedelta import pyaudio import wave import sys import audioop import requests import subprocess import os import stat def parse_file(fname, CHUNK=44100): outfile = "/tmp/asd" if os.path.isfile(outfile) or stat.S_ISFIFO(os.stat(outfile).st_mode): os.remove(outfile) print("Deleted") os.mkfifo(outfile) p = subprocess.Popen(["ffmpeg", "-nostdin", "-loglevel", "error", "-i", fname, "-c:a", "pcm_s16le", "-vn", "-f", "wav", "-y", outfile]) wf = wave.open(outfile, 'rb') p = pyaudio.PyAudio() width = wf.getsampwidth() data = wf.readframes(CHUNK) chunks = 0 count = 0 initial = 0 ret = [] while len(data) > 0: data = wf.readframes(CHUNK) rms = audioop.rms(data, width) # we need to get at least 60 values with rms < 700 # that's 60 seconds if rms < 750: count += 1 else: if count > 80: itime = timedelta(minutes=initial // 60, seconds=initial % 60) etime = timedelta(minutes=chunks // 60, seconds=chunks % 60) ret.append(itime) ret.append(etime) count = 0 initial = chunks chunks += 1 itime = timedelta(minutes=initial // 60, seconds=initial % 60) etime = timedelta(minutes=chunks // 60, seconds=chunks % 60) ret.append(itime) ret.append(etime) ret = ret[1:-1] # chop the initial and final blank time ret = sorted(list(set([t.total_seconds() for t in ret ]))) p.terminate() return ret if __name__ == '__main__': print(parse_file("processed/movie_20171006_093228.flv"))