-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi.py
More file actions
125 lines (101 loc) · 3.84 KB
/
api.py
File metadata and controls
125 lines (101 loc) · 3.84 KB
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
from flask import Flask, request, jsonify, send_file
import os
import sys
import uuid
import threading
import subprocess
from werkzeug.utils import secure_filename
app = Flask(__name__)
app.config['UPLOAD_FOLDER'] = 'uploads'
app.config['PROCESSED_FOLDER'] = 'processed'
app.config['MAX_CONTENT_LENGTH'] = 500 * 1024 * 1024
os.makedirs(app.config['UPLOAD_FOLDER'], exist_ok=True)
os.makedirs(app.config['PROCESSED_FOLDER'], exist_ok=True)
jobs = {}
@app.route('/api/process', methods=['POST'])
def process_video():
if 'video' not in request.files:
return jsonify({"error": "No video file provided"}), 400
file = request.files['video']
if file.filename == '':
return jsonify({"error": "No file selected"}), 400
job_id = str(uuid.uuid4())
filename = secure_filename(file.filename)
input_path = os.path.join(app.config['UPLOAD_FOLDER'], f"{job_id}_{filename}")
file.save(input_path)
silence_length = request.form.get('silence_length', 700)
silence_threshold = request.form.get('silence_threshold', -35)
context = request.form.get('context', 300)
pause = request.form.get('pause', 500)
use_gpu = request.form.get('use_gpu', 'true').lower() == 'true'
high_quality = request.form.get('high_quality', 'false').lower() == 'true'
output_name = f"processed_{job_id}_{filename}"
output_path = os.path.join(app.config['PROCESSED_FOLDER'], output_name)
def process_thread():
cmd = [
sys.executable,
"main.py",
input_path,
"--output", output_path,
"--silence-length", str(silence_length),
"--silence-threshold", str(silence_threshold),
"--context", str(context),
"--pause", str(pause)
]
if not use_gpu:
cmd.append("--no-gpu")
if high_quality:
cmd.append("--high-quality")
jobs[job_id]['status'] = 'processing'
process = subprocess.Popen(
cmd,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
text=True,
universal_newlines=True
)
output = []
for line in iter(process.stdout.readline, ""):
output.append(line.strip())
jobs[job_id]['logs'].append(line.strip())
process.wait()
if process.returncode == 0:
jobs[job_id]['status'] = 'completed'
jobs[job_id]['output_url'] = f"/api/download/{job_id}"
else:
jobs[job_id]['status'] = 'failed'
jobs[job_id] = {
'status': 'queued',
'filename': filename,
'input_path': input_path,
'output_path': output_path,
'logs': [],
'output_url': None
}
thread = threading.Thread(target=process_thread)
thread.daemon = True
thread.start()
return jsonify({
"job_id": job_id,
"status": "queued",
"message": "Video processing started"
})
@app.route('/api/status/<job_id>', methods=['GET'])
def job_status(job_id):
if job_id not in jobs:
return jsonify({"error": "Job not found"}), 404
job = jobs[job_id]
return jsonify({
"job_id": job_id,
"status": job['status'],
"filename": job['filename'],
"logs": job['logs'],
"output_url": job['output_url']
})
@app.route('/api/download/<job_id>', methods=['GET'])
def download_file(job_id):
if job_id not in jobs or jobs[job_id]['status'] != 'completed':
return jsonify({"error": "Processed file not available"}), 404
return send_file(jobs[job_id]['output_path'], as_attachment=True)
if __name__ == '__main__':
app.run(debug=True, port=5000)