note.py 1.99 KB
from flask import Flask, send_file,request,send_from_directory, render_template
import matlab.engine
from werkzeug.utils import secure_filename
import os
import time
import json
app = Flask(__name__)

@app.route('/', methods=['GET'])
def maiddn():
   return render_template('view.html')

@app.route('/upload1', methods=['POST'])
def upload_file1():  
   params = json.loads(request.get_data(), encoding='utf-8')
   data=params['datas']
   timeData=params['timeData']
   print(len(data))
   print(timeData)
   make_wav_file(data, timeData)
   return send_file("out.wav", attachment_filename='test.wav', as_attachment=True)

@app.route('/upload', methods=['POST'])
def upload_file():  
   params = json.loads(request.get_data(), encoding='utf-8')
   data=params['datas']
   timeData=params['timeData']
   print(len(data))
   print(timeData)
   make_wav_file(data, timeData)
   time.sleep(20)
   os.system("ffmpeg -i video.mp4 -i out.wav -c:v libx264 -c:a aac -strict experimental -map 0:v:0 -map 1:a:0 -y ./static/output.mp4")
   return "Success"


def make_wav_file(data, timeData):
   print('convert is loading')
   print(len(data))
   print(timeData)
   print(len(data) / timeData)
   hz = int(len(data) / timeData)
   print("hz=")
   print(hz)
   eng = matlab.engine.start_matlab()
   l1=[]
   for t in data:
      try:
         l1.append(int(t))
      except:
         print(t)
   l2 = [n/4000 for n in l1]
   data = matlab.double(l2)
   
   filename = 'test.wav'
   eng.audiowrite(filename, data, hz, nargout=0)
   eng.quit()
   os.system("ffmpeg -i test.wav -af volume=+10dB -y out.wav")

@app.route('/combine', methods=['POST'])
def combineMp4Wav():
   print(request.files)
   if 'Video' not in request.files:
      print("error")
      return "error"
   file = request.files['Video']
   file.save("video.mp4")
   # os.system("ffmpeg -i video.mp4 -i test.wav -c:v copy -c:a aac -strict experimental -map 0:v:0 -map 1:a:0 -y ./static/output.mp4")
   return "Success"


if __name__ == '__main__':
   app.run(host='0.0.0.0', port=80)