|
|
@ -1,14 +1,12 @@
|
|
|
|
from flask import Flask, render_template, request, flash, redirect, url_for
|
|
|
|
from flask import Flask, render_template, request, flash, redirect, url_for
|
|
|
|
from werkzeug.utils import secure_filename
|
|
|
|
from werkzeug.utils import secure_filename
|
|
|
|
from PIL import Image
|
|
|
|
from PIL import Image
|
|
|
|
import os
|
|
|
|
import os, random, datetime
|
|
|
|
# import ffmpeg
|
|
|
|
from ffmpeg import FFmpeg
|
|
|
|
import datetime;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
app = Flask(__name__)
|
|
|
|
app = Flask(__name__)
|
|
|
|
|
|
|
|
|
|
|
|
app.config['UPLOAD_FOLDER'] = "uploads"
|
|
|
|
app.config['UPLOAD_FOLDER'] = "media"
|
|
|
|
app.config['TMP_FOLDER'] = "tmp"
|
|
|
|
app.config['TMP_FOLDER'] = "tmp"
|
|
|
|
|
|
|
|
|
|
|
|
def get_extension(filename):
|
|
|
|
def get_extension(filename):
|
|
|
@ -26,6 +24,14 @@ def submitted():
|
|
|
|
print("doingsubmit")
|
|
|
|
print("doingsubmit")
|
|
|
|
return render_template('index.html', error=request.args.get('error'), submitted=True)
|
|
|
|
return render_template('index.html', error=request.args.get('error'), submitted=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def get_random_subfolder():
|
|
|
|
|
|
|
|
# folder = os.listdir(app.config['UPLOAD_FOLDER'])
|
|
|
|
|
|
|
|
folder = [f for f in os.listdir(app.config['UPLOAD_FOLDER']) if not os.path.isfile(os.path.join(app.config['UPLOAD_FOLDER'], f))]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
random.shuffle(folder)
|
|
|
|
|
|
|
|
print(folder[0])
|
|
|
|
|
|
|
|
return folder[0]
|
|
|
|
|
|
|
|
|
|
|
|
@app.route('/upload', methods=['POST'])
|
|
|
|
@app.route('/upload', methods=['POST'])
|
|
|
|
def upload_file():
|
|
|
|
def upload_file():
|
|
|
@ -45,28 +51,53 @@ def upload_file():
|
|
|
|
ext = get_extension(file.filename)
|
|
|
|
ext = get_extension(file.filename)
|
|
|
|
temp_path = os.path.join(app.config['TMP_FOLDER'], filename)
|
|
|
|
temp_path = os.path.join(app.config['TMP_FOLDER'], filename)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# random_sub = random.choice(os.listdir(app.config['UPLOAD_FOLDER']))
|
|
|
|
|
|
|
|
# sub_folder = os.path.join(app.config['UPLOAD_FOLDER'], random_sub)
|
|
|
|
|
|
|
|
sub_folder = os.path.join(app.config['UPLOAD_FOLDER'], get_random_subfolder())
|
|
|
|
|
|
|
|
print("saving to:")
|
|
|
|
|
|
|
|
print(sub_folder)
|
|
|
|
|
|
|
|
|
|
|
|
if ext in ['png', 'jpg', 'jpeg', 'gif']:
|
|
|
|
if ext in ['png', 'jpg', 'jpeg', 'gif']:
|
|
|
|
print("dealing with an image!")
|
|
|
|
print("dealing with an image!")
|
|
|
|
img = Image.open(file)
|
|
|
|
img = Image.open(file)
|
|
|
|
img.thumbnail((100, 100), Image.LANCZOS)
|
|
|
|
img.thumbnail((100, 100), Image.LANCZOS)
|
|
|
|
|
|
|
|
|
|
|
|
img.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
|
|
|
|
img.save(os.path.join(sub_folder, filename))
|
|
|
|
return redirect(url_for('submitted'))
|
|
|
|
return redirect(url_for('submitted'))
|
|
|
|
elif ext in ['mp4', 'avi', 'mov', 'flv']:
|
|
|
|
elif ext in ['mp4', 'avi', 'mov', 'flv']:
|
|
|
|
out_path = os.path.join(app.config['UPLOAD_FOLDER'], filename.rsplit('.', 1)[0].lower() + '.mp4')
|
|
|
|
file.save(temp_path)
|
|
|
|
file.save(out_path)
|
|
|
|
|
|
|
|
# stream = ffmpeg.input(temp_path)
|
|
|
|
out_path = os.path.join(sub_folder, filename.rsplit('.', 1)[0].lower() + '.mp4')
|
|
|
|
# stream = ffmpeg.hflip(stream)
|
|
|
|
|
|
|
|
# stream = ffmpeg.output(stream, out_path)
|
|
|
|
ffmpeg = (
|
|
|
|
# ffmpeg.run(stream)
|
|
|
|
FFmpeg()
|
|
|
|
# ffmpeg.input(temp_path).output(out_path,vf="scale=300:230", crf=24).run()
|
|
|
|
.option("y")
|
|
|
|
# os.remove(temp_path)
|
|
|
|
.input(temp_path)
|
|
|
|
|
|
|
|
.output(
|
|
|
|
|
|
|
|
out_path,
|
|
|
|
|
|
|
|
crf=40,
|
|
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ffmpeg.execute()
|
|
|
|
|
|
|
|
os.remove(temp_path)
|
|
|
|
return redirect(url_for('submitted'))
|
|
|
|
return redirect(url_for('submitted'))
|
|
|
|
elif ext in ['mp3', 'wav', 'flac']:
|
|
|
|
elif ext in ['mp3', 'wav', 'flac']:
|
|
|
|
out_name = filename.rsplit('.', 1)[0].lower() + '.mp3'
|
|
|
|
out_path = os.path.join(sub_folder, filename.rsplit('.', 1)[0].lower() + '.mp3')
|
|
|
|
file.save(os.path.join(app.config['UPLOAD_FOLDER'], out_name))
|
|
|
|
file.save(temp_path)
|
|
|
|
# ffmpeg.input(temp_path).output(os.path.join(app.config['UPLOAD_FOLDER'], out_name)).run()
|
|
|
|
|
|
|
|
# os.remove(temp_path)
|
|
|
|
ffmpeg = (
|
|
|
|
|
|
|
|
FFmpeg()
|
|
|
|
|
|
|
|
.option("y")
|
|
|
|
|
|
|
|
.input(temp_path)
|
|
|
|
|
|
|
|
.output(
|
|
|
|
|
|
|
|
out_path,
|
|
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ffmpeg.execute()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
os.remove(temp_path)
|
|
|
|
return redirect(url_for('submitted'))
|
|
|
|
return redirect(url_for('submitted'))
|
|
|
|
else:
|
|
|
|
else:
|
|
|
|
print("not an image!")
|
|
|
|
print("not an image!")
|
|
|
|