-
Notifications
You must be signed in to change notification settings - Fork 33
/
main.py
288 lines (235 loc) · 11.8 KB
/
main.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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
from flask import Flask, request, render_template, send_from_directory, make_response, jsonify
from flask_socketio import SocketIO, emit
from datetime import datetime, timedelta
import logging, time, os
from werkzeug.utils import secure_filename
import converter
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
import smtplib
from confidential import *
app = Flask(__name__)
socketio = SocketIO(app) # Turn the flask app into a SocketIO app.
app.config['SEND_FILE_MAX_AGE_DEFAULT'] = 0
app.config['MAX_CONTENT_LENGTH'] = 5 * 1000 * 1000 * 1000 # 5 GB max upload size.
log_format = '[%(levelname)s] %(message)s'
logging.basicConfig(filename='PythonLogs.txt', format=log_format, level=logging.DEBUG)
logger = logging.getLogger()
def current_datetime(description):
client = request.environ.get("HTTP_X_REAL_IP").split(',')[0]
current_datetime = (datetime.now() + timedelta(hours=1)).strftime('%d-%m-%y (%H:%M:%S)')
logger.info(f'{client} {description} at {current_datetime}')
# FFmpeg will write the conversion progress to a txt file. Read the file eery second to get the current conversion progress every second.
def read_progress():
previous_time = '00:00:00'
while True:
with open('progress.txt', 'r') as f:
lines = f.readlines()
# This gives us the amount of the file (HH:MM:SS) that has been converted so far
current_time = lines[-5].split('=')[-1].split('.')[0]
# If the amount converted is the same twice in a row, that means that the conversion is complete.
if previous_time == current_time:
logger.info("Conversion complete. Progress no longer being read.")
break
# Set the value of previous_time to current_time, so we can check if the value of previous_time is the same as the value of current_time in the next iteration of the loop
previous_time = current_time
progress_message = current_time + " [HH:MM:SS]" + " of the file has been converted so far..."
logger.info(progress_message)
# Trigger a new event called "show progress"
socketio.emit('show progress', {'progress': progress_message})
time.sleep(1)
@socketio.on('my event') # Decorator to catch an event called "my event".
def test_connect(): # test_connect() is the event callback function.
current_datetime("connected")
@socketio.on('disconnect')
def test_disconnect():
current_datetime("disconnected")
@app.route("/")
def homepage():
current_datetime("visited homepage")
return render_template("home.html", title="FreeAudioConverter.net")
@app.route("/about", methods=["GET", "POST"])
def about():
current_datetime("visited about page")
return render_template("about.html", title="About")
@app.route("/filetypes")
def filetypes():
current_datetime("visited filetypes")
return render_template("filetypes.html", title="Filetypes")
@app.route("/file-trimmer")
def trimmer():
current_datetime("visited trimmer")
return render_template("trimmer.html", title="File Trimmer")
@app.route("/game")
def game():
current_datetime("visited game")
return render_template("game.html", title="Game")
allowed_filetypes = ["mp3", "aac", "wav", "ogg", "opus", "m4a", "flac", "mka", "wma", "mkv", "mp4", "flv", "wmv","avi", "ac3", "3gp", "MTS", "webm", "ADPCM", "dts", "spx", "caf", "mov"]
@app.route("/", methods=["POST"])
def main():
if request.form["requestType"] == "uploaded":
chosen_file = request.files["chosen_file"]
extension = (chosen_file.filename).split(".")[-1]
if extension not in allowed_filetypes:
return make_response(jsonify({"message": "Incompatible filetype selected."}), 415)
# Make the filename safe
filename_secure = secure_filename(chosen_file.filename)
# Save the uploaded file to the uploads folder.
chosen_file.save(os.path.join("uploads", filename_secure))
response = make_response(jsonify({"message": "File uploaded. Converting..."}), 200)
return response
if request.form["requestType"] == "convert":
try:
socketio.start_background_task(read_progress)
logger.info("Started progress reader.")
except Exception as error:
logger.error(error)
finally:
file_name = request.form["file_name"]
chosen_file = os.path.join("uploads", secure_filename(file_name))
chosen_codec = request.form["chosen_codec"]
# Put the JavaSript FormData into appropriately-named variables:
mp3_encoding_type = request.form["mp3_encoding_type"]
cbr_abr_bitrate = request.form["cbr_abr_bitrate"]
mp3_vbr_setting = request.form["mp3_vbr_setting"]
is_y_switch = request.form["is_y_switch"]
# AAC
fdk_type = request.form["fdk_type"]
fdk_cbr = request.form["fdk_cbr"]
fdk_vbr = request.form["fdk_vbr"]
is_fdk_lowpass = request.form["is_fdk_lowpass"]
fdk_lowpass = request.form["fdk_lowpass"]
# Vorbis
vorbis_encoding = request.form["vorbis_encoding"]
vorbis_quality = request.form["vorbis_quality"]
# Vorbis/Opus
slider_value = request.form["slider_value"]
# AC3
ac3_bitrate = request.form["ac3_bitrate"]
# FLAC
flac_compression = request.form["flac_compression"]
# DTS
dts_bitrate = request.form["dts_bitrate"]
# Opus
opus_cbr_bitrate = request.form["opus-cbr-bitrate"]
opus_encoding_type = request.form["opus-encoding-type"]
# Desired filename
output_name = request.form["output_name"]
# Downmix multi-channel audio to stereo?
is_downmix = request.form["is_downmix"]
# Run the appropritate section of converter.py:
if chosen_codec == 'MP3':
converter.run_mp3(chosen_file, mp3_encoding_type, cbr_abr_bitrate, mp3_vbr_setting, is_y_switch, output_name, is_downmix)
extension = 'mp3'
elif chosen_codec == 'AAC':
converter.run_aac(chosen_file, fdk_type, fdk_cbr, fdk_vbr, output_name, is_downmix, is_fdk_lowpass, fdk_lowpass)
extension = 'm4a'
elif chosen_codec == 'Opus':
converter.run_opus(chosen_file, opus_encoding_type, slider_value, opus_cbr_bitrate, output_name, is_downmix)
extension = 'opus'
elif chosen_codec == 'FLAC':
converter.run_flac(chosen_file, flac_compression, output_name, is_downmix)
extension = 'flac'
elif chosen_codec == 'Vorbis':
converter.run_vorbis(chosen_file, vorbis_encoding, vorbis_quality, slider_value, output_name, is_downmix)
extension = 'ogg'
elif chosen_codec == 'WAV':
converter.run_wav(chosen_file, output_name, is_downmix)
extension = 'wav'
elif chosen_codec == 'MKV':
converter.run_mkv(chosen_file, output_name, is_downmix)
extension = 'mkv'
elif chosen_codec == 'MKA':
converter.run_mka(chosen_file, output_name, is_downmix)
extension = 'mka'
elif chosen_codec == 'ALAC':
converter.run_alac(chosen_file, output_name, is_downmix)
extension = 'm4a'
elif chosen_codec == 'AC3':
converter.run_ac3(chosen_file, ac3_bitrate, output_name, is_downmix)
extension = 'ac3'
elif chosen_codec == 'CAF':
converter.run_caf(chosen_file, output_name, is_downmix)
extension = 'caf'
elif chosen_codec == 'DTS':
converter.run_dts(chosen_file, dts_bitrate, output_name, is_downmix)
extension = 'dts'
else: # The chosen codec is Speex
converter.run_speex(chosen_file, output_name, is_downmix)
extension = 'spx'
converted_file_name = output_name + "." + extension
response = make_response(jsonify({
"message": "File converted. The converted file will now start downloading.",
"downloadFilePath": 'download/' + converted_file_name
}), 200)
return response
@app.route("/download/<path:filename>", methods=["GET"])
def download_file(filename):
just_extension = filename.split('.')[-1]
if just_extension == "m4a":
return send_from_directory(os.getcwd(), filename, mimetype="audio/mp4")
else:
return send_from_directory(os.getcwd(), filename)
# CONTACT PAGE
@app.route("/contact", methods=["GET", "POST"])
def contact():
if request.method == "POST":
send_from = "[email protected]"
send_to = "[email protected]"
text = MIMEMultipart()
text['From'] = send_from
text['To'] = send_to
text['Subject'] = "Your Website"
body = request.form['message']
text.attach(MIMEText(body, 'plain'))
server = smtplib.SMTP(host='smtp-mail.outlook.com', port=587)
server.ehlo()
server.starttls()
server.ehlo()
server.login(my_email, password)
text = text.as_string()
server.sendmail(send_from, send_to, text)
return make_response("Message sent!", 200)
else:
current_datetime("visited contact page")
return render_template("contact.html", title="Contact")
# FILE TRIMMER
@app.route("/file-trimmer", methods=["GET", "POST"])
def trim_file():
if request.form["request_type"] == "upload_complete":
current_datetime("File uploaded")
chosen_file = request.files["chosen_file"]
extension = (chosen_file.filename).split(".")[-1]
if extension not in allowed_filetypes:
return make_response(jsonify({"message": "Incompatible filetype selected."}), 415)
# Make the filename safe
filename_secure = secure_filename(chosen_file.filename)
# Save the uploaded file to the uploads folder.
chosen_file.save(os.path.join("uploads", filename_secure))
response = make_response(jsonify({"message": "File uploaded. Converting..."}), 200)
return response
if request.form["request_type"] == "trim":
file_name = request.form["filename"]
chosen_file = os.path.join("uploads", secure_filename(file_name))
filename = request.form["filename"]
start_time = request.form["start_time"]
end_time = request.form["end_time"]
ext = "." + filename.split(".")[-1]
just_name = filename.split(".")[0]
output_name = just_name + " [trimmed]" + ext
os.system(f'ffmpeg -y -i "{chosen_file}" -ss {start_time} -to {end_time} -c copy "{output_name}"')
current_datetime("Trim complete")
response = make_response(jsonify({
"message": "File converted. The converted file will now start downloading.",
"downloadFilePath": 'download/' + output_name
}), 200)
return response
@app.route("/download-trimmed-file/<path:filename>", methods=["GET"])
def download_trimmed_file(filename):
just_extension = filename.split('.')[-1]
if just_extension == "m4a":
return send_from_directory(os.getcwd(), filename, mimetype="audio/mp4")
else:
return send_from_directory(os.getcwd(), filename)
if __name__ == "__main__":
socketio.run(app)