-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
171 lines (151 loc) · 6.99 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
from flask import Flask, render_template, request, flash, redirect, send_file, session
from werkzeug.utils import secure_filename
import os
import uuid
from audiosync import sync
VIDEO_UPLOAD_FOLDER = '/home/site/wwwroot/video'
AUDIO_UPLOAD_FOLDER = '/home/site/wwwroot/audio'
COMBINED_UPLOAD_FOLDER = '/home/site/wwwroot/combined'
VIDEO_EXTENSIONS = {'mp4'}
AUDIO_EXTENSIONS = {'wav'}
app = Flask(__name__)
app.config.update(SECRET_KEY='# Insert Secret Key')
app.config['VIDEO_UPLOAD_FOLDER'] = VIDEO_UPLOAD_FOLDER
app.config['AUDIO_UPLOAD_FOLDER'] = AUDIO_UPLOAD_FOLDER
app.config['COMBINED_UPLOAD_FOLDER'] = COMBINED_UPLOAD_FOLDER
def allowed_video_file(filename):
return '.' in filename and \
filename.rsplit('.', 1)[1].lower() in VIDEO_EXTENSIONS
def allowed_audio_file(filename):
return '.' in filename and \
filename.rsplit('.', 1)[1].lower() in AUDIO_EXTENSIONS
@app.route('/')
def index():
if 'videoFile' in session:
os.remove(os.path.join(app.config['VIDEO_UPLOAD_FOLDER'], session['videoFile']))
if 'audioFile' in session:
os.remove(os.path.join(app.config['AUDIO_UPLOAD_FOLDER'], session['audioFile']))
if 'saveFile' in session:
os.remove(session['saveFile'])
[session.pop(key) for key in list(session.keys()) if key != '_flashes']
return render_template('index.html')
@app.route('/video_upload', methods=['GET', 'POST'])
def upload_video():
if request.method == 'POST':
if 'videoFile' in session:
return redirect(request.url_root)
# DEBUG
foldername = app.config['VIDEO_UPLOAD_FOLDER']
if not os.path.exists(foldername):
os.makedirs(foldername)
debugfile = os.path.join(foldername, 'log.txt')
# DEBUG
with open(debugfile, 'w') as logfile:
flash('Uploading...', 'Status')
# check if the post request has the file part
if 'file' not in request.files:
flash('No file part', 'Error')
logfile.write('Error: No file part\n')
return redirect(request.url_root)
file = request.files['file']
# if user does not select file, browser also
# submit an empty part without filename
if not file or file.filename == '':
flash('No selected video file', 'Error')
logfile.write('Error: No selected video file\n')
return redirect(request.url_root)
if allowed_video_file(file.filename):
filename, file_extension = os.path.splitext(file.filename)
session['videoFile'] = secure_filename(filename + uuid.uuid4().hex + file_extension)
session.modified = True
savefilename = os.path.join(app.config['VIDEO_UPLOAD_FOLDER'], session['videoFile'])
logfile.write('Save filename = {}\n'.format(savefilename))
file.save(savefilename)
if os.path.exists(savefilename):
logfile.write('Save successful.\n')
else:
logfile.write('Save failed.\n')
flash('Done uploading video file!', 'Status')
return render_template('index.html')
@app.route('/audio_upload', methods=['GET', 'POST'])
def upload_audio():
if request.method == 'POST':
if 'audioFile' in session:
return redirect(request.url_root)
# DEBUG
foldername = app.config['AUDIO_UPLOAD_FOLDER']
if not os.path.exists(foldername):
os.makedirs(foldername)
debugfile = os.path.join(foldername, 'log.txt')
# DEBUG
with open(debugfile, 'w') as logfile:
flash('Uploading...', 'Status')
# check if the post request has the file part
if 'file' not in request.files:
flash('No file part', 'Error')
logfile.write('Error: No file part\n')
return redirect(request.url_root)
file = request.files['file']
# if user does not select file, browser also
# submit an empty part without filename
if not file or file.filename == '':
flash('No selected audio file', 'Error')
logfile.write('Error: No selected video file\n')
return redirect(request.url_root)
if allowed_audio_file(file.filename):
filename, file_extension = os.path.splitext(file.filename)
session['audioFile'] = secure_filename(filename + uuid.uuid4().hex + file_extension)
session.modified = True
savefilename = os.path.join(app.config['AUDIO_UPLOAD_FOLDER'], session['audioFile'])
logfile.write('Save filename = {}\n'.format(savefilename))
file.save(savefilename)
if os.path.exists(savefilename):
logfile.write('Save successful.\n')
else:
logfile.write('Save failed.\n')
flash('Done uploading audio file!', 'Status')
return render_template('index.html')
@app.route('/process', methods=['GET', 'POST'])
def process():
if request.method == 'POST':
flash('Processing...', 'Status')
# display error msgs
if 'videoFile' not in session and 'audioFile' not in session:
flash('No selected video file or audio file', 'Error')
return redirect(request.url_root)
elif 'videoFile' in session and 'audioFile' not in session:
flash('No selected audio file', 'Error')
return redirect(request.url_root)
elif 'videoFile' not in session and 'audioFile' in session:
flash('No selected video file', 'Error')
return redirect(request.url_root)
fft = request.form['fft']
hopLength = request.form['hopLength']
samplingRate = request.form['samplingRate']
duration = request.form['duration']
# python function call to sync
video = os.path.join(app.config['VIDEO_UPLOAD_FOLDER'], session['videoFile'])
audio = os.path.join(app.config['AUDIO_UPLOAD_FOLDER'], session['audioFile'])
files, _ = sync(videofile=video, audiofile=audio, n_fft=fft, hop_size=hopLength, sampling_rate=samplingRate, duration_limit=duration)
session['saveFile'] = files
flash('Processing finished!', 'Status')
os.remove(os.path.join(app.config['VIDEO_UPLOAD_FOLDER'], session['videoFile']))
os.remove(os.path.join(app.config['AUDIO_UPLOAD_FOLDER'], session['audioFile']))
session.pop('videoFile', None)
session.pop('audioFile', None)
return render_template('index.html')
@app.route('/download', methods=['GET', 'POST'])
def download():
# download
if 'saveFile' not in session:
flash('Nothing to download!', 'Error')
return redirect(request.url_root)
path = session['saveFile']
return send_file(path, as_attachment=True)
if __name__ == '__main__':
# debug
if False:
app.run()
# live
else:
app.run(host="0.0.0.0", port=80)