next-playground commited on
Commit
81c4606
·
verified ·
1 Parent(s): 79dc878

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +108 -0
app.py ADDED
@@ -0,0 +1,108 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from flask import Flask, request, jsonify, send_file
2
+ import requests
3
+ import os
4
+ import uuid
5
+ from pathlib import Path
6
+ import threading
7
+ import time
8
+
9
+ from pydub import AudioSegment
10
+
11
+ def audio_overlay(audio1, audio2, save_filename):
12
+
13
+
14
+ audio_raw1 = AudioSegment.from_file(audio1)
15
+ audio_raw2 = AudioSegment.from_file(audio2)
16
+
17
+ min_length = min(len(audio_raw1), len(audio_raw2))
18
+ audio_raw1 = audio_raw1[:min_length]
19
+ audio_raw2 = audio_raw2[:min_length]
20
+
21
+ overlayed_audio = audio_raw1.overlay(audio2)
22
+ overlayed_audio.export("/tmp/" + save_filename, format="mp3")
23
+
24
+ # 任务队列和状态存储
25
+ tasks = {}
26
+ running_threads = 0
27
+ condition = threading.Condition()
28
+
29
+ def process_audio_overlay(task_id, audio_url1, audio_url2):
30
+ global running_threads
31
+ with condition:
32
+ while running_threads >= 1:
33
+ tasks[task_id] = {"status": "queue"}
34
+ condition.wait()
35
+ running_threads += 1
36
+ tasks[task_id] = {"status": "processing"}
37
+ try:
38
+ # 下载音频文件到本地
39
+ response1 = requests.get(audio_url1)
40
+ if response1.status_code != 200:
41
+ raise Exception("无效的 URL")
42
+ audio_filename1 = audio_url1.split('/')[-1]
43
+ with open("/tmp/" + audio_filename1, 'wb') as f:
44
+ f.write(response1.content)
45
+ response2 = requests.get(audio_url2)
46
+ if response2.status_code != 200:
47
+ raise Exception("无效的 URL")
48
+ audio_filename2 = audio_url2.split('/')[-1]
49
+ with open("/tmp/" + audio_filename2, 'wb') as f:
50
+ f.write(response2.content)
51
+
52
+ # 执行音频合并操作
53
+ audio = audio_overlay("/tmp/" + audio_filename1, "/tmp/" + audio_filename2, audio_filename1 + "_with_" + audio_filename2 + "overlayed.mp3")
54
+
55
+ # 提供文件的永久直链
56
+ result_audio_url = "/download/" + audio_filename1 + "_with_" + audio_filename2 + "overlayed.mp3"
57
+
58
+ # 更新任务状态
59
+ tasks[task_id] = {
60
+ "status": "completed",
61
+ "result_url": result_audio_url
62
+ }
63
+ except Exception as e:
64
+ tasks[task_id] = {
65
+ "status": "error",
66
+ "message": str(e)
67
+ }
68
+ with condition:
69
+ running_threads -= 1
70
+ condition.notify_all()
71
+
72
+ app = Flask(__name__)
73
+
74
+ @app.route('/', methods=['GET'])
75
+ def hello():
76
+ return "Hello! This is an api server, and it is running successfully. For usage, please contact the person who hosted this api server."
77
+
78
+ @app.route('/api/audio_overlay', methods=['GET'])
79
+ def audio_separation():
80
+ audio_url1 = request.args.get('url1')
81
+ if not audio_url1:
82
+ return jsonify({"error": "URL1 parameter is required"}), 400
83
+ audio_url2 = request.args.get('url2')
84
+ if not audio_url2:
85
+ return jsonify({"error": "URL2 parameter is required"}), 400
86
+
87
+ task_id = str(uuid.uuid4())
88
+ tasks[task_id] = {"status": "processing"}
89
+
90
+ # 异步执行任务
91
+ threading.Thread(target=process_audio_overlay, args=(task_id, audio_url1, audio_url2)).start()
92
+
93
+ return jsonify({"task_id": task_id}), 202
94
+
95
+ @app.route('/api/tasks/<task_id>', methods=['GET'])
96
+ def get_task_status(task_id):
97
+ task = tasks.get(task_id)
98
+ if task:
99
+ return jsonify(task)
100
+ else:
101
+ return jsonify({"error": "Task not found"}), 404
102
+
103
+ @app.route('/download/<filename>', methods=['GET'])
104
+ def download(filename):
105
+ return send_file("/tmp/" + filename, as_attachment=True)
106
+
107
+ if __name__ == '__main__':
108
+ app.run(debug=False)