Browse Source

add audio timings

David 9 years atrás
parent
commit
5eacce881c
2 changed files with 75 additions and 0 deletions
  1. 66 0
      back/audio_timings.py
  2. 9 0
      back/web.py

+ 66 - 0
back/audio_timings.py

@@ -0,0 +1,66 @@
+#!/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"))

+ 9 - 0
back/web.py

@@ -3,6 +3,7 @@ from constants import BASE_PATH,PORT
 import datetime
 import json
 import tasks
+import audio_timings
 
 from db import db
 from flask import Flask, jsonify, request
@@ -54,6 +55,14 @@ def processed_file(video_id):
     ret["out_fname"]=ret["out_fname"].split("front")[1] #FIXME, path relativo al webserver
     return jsonify(ret)
 
+@app.route(BASE_PATH + '/timings/<video_id>')
+def get_timings(video_id):
+    ret=d.raw(video_id)
+    if ret is not None:
+        timings = audio_timings.parse_file(ret["file"])
+        return jsonify({'timings': timings})
+    return jsonify({})
+    
 @app.route(BASE_PATH + '/crop/', methods=["POST"])
 def crop():
     ret={"status":"Procesando"}