Skip to content

Commit

Permalink
feat: draft implementation of video conversion
Browse files Browse the repository at this point in the history
  • Loading branch information
TheJoin95 committed Jul 25, 2023
1 parent 36e2eab commit 68a66de
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 6 deletions.
33 changes: 28 additions & 5 deletions src/ign-api/convert_image.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from flask import Flask
from flask import jsonify, abort
from flask import request
from flask import request, send_file
from flask_cors import CORS, cross_origin
from flask import Blueprint

Expand All @@ -9,6 +9,7 @@
from rq import Queue
from rq.job import Job
from worker import conn
import os

q = Queue(connection=conn)
API_VERSION = '/v1'
Expand All @@ -21,12 +22,13 @@ def convert_queue():
go_nord = setup_instance(request)
output_path = ''
response = {'success': True}
file = None

if (request.files.get('file') != None):
image = go_nord.open_image(request.files.get('file').stream)
elif (request.form.get('file_path') != None):
file = request.files.get('file')
elif (request.form.get('file_path') != None): # not used in the website
image = go_nord.open_image(request.form.get('file_path'))
elif (request.form.get('b64_input') != None):
elif (request.form.get('b64_input') != None): # not used in the website
image = go_nord.base64_to_image(request.form.get('b64_input'))
else:
abort(400, 'You need to provide at least a valid image or image path')
Expand All @@ -35,10 +37,27 @@ def convert_queue():
output_path = request.form.get('output_path')

conn.incr('conversion_count', 1)
job = q.enqueue(f=convert_image, ttl=900, failure_ttl=900, job_timeout='180s', args=(go_nord, image, output_path, request.form.get('b64_output'), response))

if is_image(file.filename):
image = go_nord.open_image(request.files.get('file').stream)
job = q.enqueue(f=convert_image, ttl=900, failure_ttl=900, job_timeout='180s', args=(go_nord, image, output_path, request.form.get('b64_output'), response))
elif is_video(file.filename):
path_to_video = os.path.join('/tmp', file.filename)
#file.save(path_to_video)
job = q.enqueue(f=convert_video, ttl=900, failure_ttl=900, job_timeout='180s', args=(go_nord, path_to_video))
else:
abort(400, 'No valid file: you can upload video in mp4, avi, webp and mov and images (up to 16MB)')

return job.id

def is_video(filename):
return '.' in filename and \
filename.rsplit('.', 1)[1].lower() in ['mp4', 'mov', 'avi', 'webm']

def is_image(filename):
return '.' in filename and \
filename.rsplit('.', 1)[1].lower() in ['png', 'jpg', 'jpeg']

def convert_image(go_nord, image, save_path, b64_output, response):
image = go_nord.convert_image(image, save_path=save_path)
if (b64_output != None):
Expand All @@ -48,6 +67,10 @@ def convert_image(go_nord, image, save_path, b64_output, response):

return response

def convert_video(go_nord, path_to_video):
output_papth = go_nord.convert_video(path_to_video, 'custom_palette', save_path='/tmp')
return send_file(output_papth)

def setup_instance(req):
go_nord = GoNord()

Expand Down
4 changes: 3 additions & 1 deletion src/ign-api/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
app = Flask(__name__)
cors = CORS(app)
app.config['CORS_HEADERS'] = 'Content-Type'
app.config['UPLOAD_FOLDER'] = '/tmp'
app.config['MAX_CONTENT_LENGTH'] = 16 * 1000 * 1000 # 16MB

app.register_blueprint(convert_async_api)

Expand Down Expand Up @@ -58,7 +60,7 @@ def quantize():

return jsonify(response)

@app.route(API_VERSION + "/convert", methods=["POST"])
@app.route(API_VERSION + "/convert", methods=["POST"]) # not used in website
@cross_origin(origin='*')
def convert():
go_nord = setup_instance(request)
Expand Down

0 comments on commit 68a66de

Please sign in to comment.