Ricky01anjay commited on
Commit
5d5777e
·
verified ·
1 Parent(s): 77b30b8

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +169 -131
app.py CHANGED
@@ -7,6 +7,7 @@ import json
7
  import time
8
  from flask import Flask, request, jsonify, render_template_string, send_from_directory
9
  import moviepy.editor as mp
 
10
  import whisper
11
  import edge_tts
12
 
@@ -25,9 +26,14 @@ VOICE_MAP = {
25
  print("Loading Whisper Model...")
26
  whisper_model = whisper.load_model("base")
27
 
28
- def translate_with_llm(text, custom_prompt, max_retries=3):
29
- instruction = custom_prompt if custom_prompt else "Terjemahkan teks berikut. Hanya berikan hasil terjemahannya saja tanpa penjelasan tambahan."
30
- full_prompt = f"{instruction}\n\nTeks asli:\n{text}"
 
 
 
 
 
31
 
32
  url = "https://www.puruboy.kozow.com/api/ai/notegpt"
33
  payload = {
@@ -35,170 +41,224 @@ def translate_with_llm(text, custom_prompt, max_retries=3):
35
  "model": "gemini-3-flash-preview",
36
  "chat_mode": "standard"
37
  }
38
- headers = {"Content-Type": "application/json"}
39
-
40
  for attempt in range(max_retries):
41
  try:
42
- response = requests.post(url, json=payload, headers=headers, stream=True)
43
- response.raise_for_status()
44
- translated_text = ""
45
  for line in response.iter_lines():
46
  if line:
47
- decoded_line = line.decode('utf-8')
48
- if decoded_line.startswith("data: "):
49
- json_str = decoded_line[len("data: "):]
50
  try:
51
- data = json.loads(json_str)
52
- if "text" in data:
53
- translated_text += data["text"]
54
- except json.JSONDecodeError:
55
- continue
56
- translated_text = translated_text.strip()
57
- if translated_text:
58
- return translated_text
59
- except Exception:
60
- pass
61
- time.sleep(2 ** attempt)
62
- raise Exception("Gagal mendapatkan terjemahan dari AI.")
 
 
 
 
 
 
63
 
64
  def process_dubbing(task_id, video_path, target_voice, custom_prompt):
65
  try:
66
- tasks[task_id]['status'] = 'Mengekstrak audio...'
67
  video = mp.VideoFileClip(video_path)
68
 
69
  if video.duration > 120.0:
70
- video.close()
71
- os.remove(video_path)
72
- raise Exception("Durasi video maksimal 2 menit.")
 
 
 
 
 
 
 
 
73
 
74
- audio_path = os.path.join(app.config['UPLOAD_FOLDER'], f"{task_id}.wav")
75
- if video.audio is None:
76
- raise Exception("Video tidak memiliki audio.")
77
 
78
- video.audio.write_audiofile(audio_path, logger=None)
 
 
79
 
80
- tasks[task_id]['status'] = 'Transkripsi...'
81
- result = whisper_model.transcribe(audio_path)
82
- original_text = result['text']
83
 
84
- if not original_text.strip():
85
- raise Exception("Suara tidak terdeteksi.")
 
 
 
 
 
86
 
87
- tasks[task_id]['status'] = 'Translasi AI...'
88
- translated_text = translate_with_llm(original_text, custom_prompt)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
89
 
90
- tasks[task_id]['status'] = 'Generasi Suara AI...'
91
- ai_audio_path = os.path.join(app.config['UPLOAD_FOLDER'], f"{task_id}_ai.mp3")
92
- voice = VOICE_MAP.get(target_voice, 'id-ID-ArdiNeural')
93
 
94
- async def generate_tts():
95
- communicate = edge_tts.Communicate(translated_text, voice)
96
- await communicate.save(ai_audio_path)
97
- asyncio.run(generate_tts())
98
-
99
- tasks[task_id]['status'] = 'Merging Video...'
100
  output_video_path = os.path.join(app.config['UPLOAD_FOLDER'], f"{task_id}_output.mp4")
101
- new_audio = mp.AudioFileClip(ai_audio_path)
102
- final_video = video.set_audio(new_audio)
103
  final_video.write_videofile(output_video_path, codec='libx264', audio_codec='aac', logger=None)
104
-
 
105
  video.close()
106
- new_audio.close()
107
- os.remove(audio_path)
108
- os.remove(ai_audio_path)
 
109
 
110
  tasks[task_id]['status'] = 'Selesai'
111
  tasks[task_id]['result_video'] = f"/download/{task_id}_output.mp4"
 
112
  except Exception as e:
 
113
  tasks[task_id]['status'] = 'Error'
114
  tasks[task_id]['error_message'] = str(e)
115
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
116
  HTML_TEMPLATE = """
117
  <!DOCTYPE html>
118
  <html>
119
  <head>
120
- <meta charset="UTF-8">
121
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
122
- <title>AI Dubbing</title>
123
  <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
124
- <style>
125
- body { background:#f4f7f6; }
126
- .container { max-width: 500px; margin-top: 50px; }
127
- .card { border-radius: 15px; border: none; box-shadow: 0 10px 20px rgba(0,0,0,0.05); }
128
- </style>
129
  </head>
130
- <body>
131
- <div class="container">
132
- <div class="card p-4">
133
- <h4 class="text-center mb-4">🎙️ Video Dubbing AI</h4>
134
- <form id="uploadForm">
135
- <div class="mb-3">
136
- <label class="form-label">Video (MP4, Max 2 Min)</label>
137
- <input class="form-control" type="file" id="videoFile" accept="video/*" required>
138
- </div>
139
- <div class="mb-3">
140
- <label class="form-label">Suara Target</label>
141
- <select class="form-select" id="targetVoice">
142
- <option value="id-ID">Indonesia</option>
143
- <option value="en-US">English</option>
144
- <option value="ja-JP">Japanese</option>
145
- </select>
 
 
 
 
 
 
 
 
 
 
 
 
146
  </div>
147
- <div class="mb-3">
148
- <label class="form-label">Custom Prompt (Opsional)</label>
149
- <textarea class="form-control" id="customPrompt" rows="2"></textarea>
 
150
  </div>
151
- <button type="submit" class="btn btn-primary w-100" id="btnSubmit">Proses</button>
152
- </form>
153
- <div id="statusSection" class="mt-4 d-none text-center">
154
- <div class="spinner-border text-primary mb-2"></div>
155
- <p id="statusTxt"></p>
156
- </div>
157
- <div id="resultSection" class="mt-4 d-none">
158
- <video id="resVid" controls class="w-100 rounded mb-3"></video>
159
- <a id="dlBtn" href="#" class="btn btn-success w-100" download>Download</a>
160
  </div>
161
  </div>
162
  </div>
 
163
  <script>
164
  const form = document.getElementById('uploadForm');
165
  form.onsubmit = async (e) => {
166
  e.preventDefault();
167
- const file = document.getElementById('videoFile').files[0];
168
- const v = document.createElement('video');
169
- v.preload = 'metadata';
170
- v.src = URL.createObjectURL(file);
171
- v.onloadedmetadata = () => {
172
- if(v.duration > 120) return alert('Max 2 Menit!');
173
- sendData();
174
- };
175
- };
176
- async function sendData(){
177
  const fd = new FormData();
178
  fd.append('video', document.getElementById('videoFile').files[0]);
179
  fd.append('voice', document.getElementById('targetVoice').value);
180
  fd.append('prompt', document.getElementById('customPrompt').value);
 
181
  document.getElementById('btnSubmit').disabled = true;
182
- document.getElementById('statusSection').classList.remove('d-none');
 
183
  const res = await fetch('/generate', {method:'POST', body:fd});
184
  const data = await res.json();
185
- poll(data.task_id);
186
- }
187
- function poll(id){
 
188
  const itv = setInterval(async () => {
189
- const res = await fetch('/status?task_id='+id);
190
  const data = await res.json();
191
  document.getElementById('statusTxt').innerText = data.status;
192
- if(data.status === 'Selesai'){
 
193
  clearInterval(itv);
194
- document.getElementById('statusSection').classList.add('d-none');
195
- document.getElementById('resultSection').classList.remove('d-none');
196
  document.getElementById('resVid').src = data.result_video;
197
  document.getElementById('dlBtn').href = data.result_video;
198
  document.getElementById('btnSubmit').disabled = false;
199
- } else if(data.status === 'Error'){
200
  clearInterval(itv);
201
- alert(data.error_message);
202
  location.reload();
203
  }
204
  }, 2000);
@@ -208,27 +268,5 @@ HTML_TEMPLATE = """
208
  </html>
209
  """
210
 
211
- @app.route('/')
212
- def index():
213
- return render_template_string(HTML_TEMPLATE)
214
-
215
- @app.route('/generate', methods=['POST'])
216
- def generate():
217
- file = request.files['video']
218
- task_id = str(uuid.uuid4())
219
- path = os.path.join(app.config['UPLOAD_FOLDER'], f"{task_id}.mp4")
220
- file.save(path)
221
- tasks[task_id] = {'status': 'Queued', 'result_video': None, 'error_message': None}
222
- threading.Thread(target=process_dubbing, args=(task_id, path, request.form.get('voice'), request.form.get('prompt'))).start()
223
- return jsonify({'task_id': task_id})
224
-
225
- @app.route('/status')
226
- def status():
227
- return jsonify(tasks.get(request.args.get('task_id'), {}))
228
-
229
- @app.route('/download/<f>')
230
- def download(f):
231
- return send_from_directory(app.config['UPLOAD_FOLDER'], f)
232
-
233
  if __name__ == '__main__':
234
  app.run(host='0.0.0.0', port=7860)
 
7
  import time
8
  from flask import Flask, request, jsonify, render_template_string, send_from_directory
9
  import moviepy.editor as mp
10
+ from moviepy.audio.fx.all import volumex, time_stretch
11
  import whisper
12
  import edge_tts
13
 
 
26
  print("Loading Whisper Model...")
27
  whisper_model = whisper.load_model("base")
28
 
29
+ def translate_segments_llm(segments, custom_prompt, max_retries=3):
30
+ """Mengirim segmen ke AI untuk diterjemahkan dalam format JSON"""
31
+ instruction = custom_prompt if custom_prompt else "Terjemahkan teks dalam JSON ini ke bahasa target. Tetap pertahankan format JSON, jangan ubah nilai 'start' dan 'end'."
32
+
33
+ # Sederhanakan input untuk AI agar hemat token dan akurat
34
+ input_data = [{"start": s['start'], "end": s['end'], "text": s['text']} for s in segments]
35
+
36
+ full_prompt = f"{instruction}\n\nFormat Output harus valid JSON Array:\n{json.dumps(input_data)}"
37
 
38
  url = "https://www.puruboy.kozow.com/api/ai/notegpt"
39
  payload = {
 
41
  "model": "gemini-3-flash-preview",
42
  "chat_mode": "standard"
43
  }
44
+
 
45
  for attempt in range(max_retries):
46
  try:
47
+ response = requests.post(url, json=payload, timeout=60)
48
+ # Karena API ini menggunakan streaming data:, kita perlu parse manual
49
+ full_response = ""
50
  for line in response.iter_lines():
51
  if line:
52
+ decoded = line.decode('utf-8')
53
+ if decoded.startswith("data: "):
 
54
  try:
55
+ data = json.loads(decoded[6:])
56
+ full_response += data.get("text", "")
57
+ except: continue
58
+
59
+ # Cari bagian JSON di dalam response
60
+ start_idx = full_response.find('[')
61
+ end_idx = full_response.rfind(']') + 1
62
+ if start_idx != -1 and end_idx != -1:
63
+ return json.loads(full_response[start_idx:end_idx])
64
+ except Exception as e:
65
+ print(f"Retry {attempt}: {e}")
66
+ time.sleep(2)
67
+
68
+ return input_data # Fallback ke data asli jika gagal
69
+
70
+ async def generate_segment_tts(text, voice, output_path):
71
+ communicate = edge_tts.Communicate(text, voice)
72
+ await communicate.save(output_path)
73
 
74
  def process_dubbing(task_id, video_path, target_voice, custom_prompt):
75
  try:
76
+ tasks[task_id]['status'] = 'Menganalisis Suara (Whisper)...'
77
  video = mp.VideoFileClip(video_path)
78
 
79
  if video.duration > 120.0:
80
+ raise Exception("Durasi maksimal 2 menit.")
81
+
82
+ # 1. Transkripsi dengan Timestamp
83
+ audio_temp_path = os.path.join(app.config['UPLOAD_FOLDER'], f"{task_id}_temp.wav")
84
+ video.audio.write_audiofile(audio_temp_path, logger=None)
85
+
86
+ result = whisper_model.transcribe(audio_temp_path, verbose=False)
87
+ segments = result['segments'] # Mendapatkan list dengan start, end, text
88
+
89
+ tasks[task_id]['status'] = 'Menerjemahkan per Segmen...'
90
+ translated_segments = translate_segments_llm(segments, custom_prompt)
91
 
92
+ tasks[task_id]['status'] = 'Memproses Dubbing & Sinkronisasi...'
 
 
93
 
94
+ dubbed_clips = []
95
+ # Kita akan membuat overlay audio
96
+ original_audio = video.audio
97
 
98
+ # Buat background audio yang volumenya 10%
99
+ # Tapi kita ingin ducking dinamis, jadi sementara kita siapkan list audio baru
 
100
 
101
+ for i, seg in enumerate(translated_segments):
102
+ start_t = seg['start']
103
+ end_t = seg['end']
104
+ duration_target = end_t - start_t
105
+
106
+ if duration_target <= 0 or not seg['text'].strip():
107
+ continue
108
 
109
+ seg_audio_path = os.path.join(app.config['UPLOAD_FOLDER'], f"{task_id}_seg_{i}.mp3")
110
+ voice = VOICE_MAP.get(target_voice, 'id-ID-ArdiNeural')
111
+
112
+ # Generate TTS
113
+ asyncio.run(generate_segment_tts(seg['text'], voice, seg_audio_path))
114
+
115
+ # Load audio segment
116
+ gen_audio = mp.AudioFileClip(seg_audio_path)
117
+
118
+ # HITUNG KECEPATAN (Time Stretch)
119
+ # Jika audio TTS lebih panjang dari durasi video asli, percepat.
120
+ speed_factor = gen_audio.duration / duration_target
121
+ if speed_factor > 1.0:
122
+ # Maksimal percepat 2x agar tidak rusak suaranya
123
+ speed_factor = min(speed_factor, 2.0)
124
+ gen_audio = gen_audio.fx(time_stretch, speed_factor)
125
+
126
+ # Set posisi audio di timestamp yang benar
127
+ gen_audio = gen_audio.set_start(start_t).set_duration(duration_target)
128
+ dubbed_clips.append(gen_audio)
129
 
130
+ # 2. AUDIO MIXING (DUCKING)
131
+ # Turunkan volume asli ke 10%
132
+ bg_audio = original_audio.fx(volumex, 0.1)
133
 
134
+ # Gabungkan semua dubbing ke satu track
135
+ final_dub_audio = mp.CompositeAudioClip([bg_audio] + dubbed_clips)
136
+
137
+ # 3. MERGE KE VIDEO
138
+ final_video = video.set_audio(final_dub_audio)
 
139
  output_video_path = os.path.join(app.config['UPLOAD_FOLDER'], f"{task_id}_output.mp4")
 
 
140
  final_video.write_videofile(output_video_path, codec='libx264', audio_codec='aac', logger=None)
141
+
142
+ # Cleanup
143
  video.close()
144
+ for f in os.listdir(app.config['UPLOAD_FOLDER']):
145
+ if f.startswith(f"{task_id}_seg_") or f.endswith("_temp.wav"):
146
+ try: os.remove(os.path.join(app.config['UPLOAD_FOLDER'], f))
147
+ except: pass
148
 
149
  tasks[task_id]['status'] = 'Selesai'
150
  tasks[task_id]['result_video'] = f"/download/{task_id}_output.mp4"
151
+
152
  except Exception as e:
153
+ print(f"Error: {str(e)}")
154
  tasks[task_id]['status'] = 'Error'
155
  tasks[task_id]['error_message'] = str(e)
156
 
157
+ # --- UI & Routes (Sama dengan sebelumnya namun dengan penyesuaian) ---
158
+
159
+ @app.route('/')
160
+ def index():
161
+ return render_template_string(HTML_TEMPLATE)
162
+
163
+ @app.route('/generate', methods=['POST'])
164
+ def generate():
165
+ if 'video' not in request.files: return jsonify({'error': 'No file'})
166
+ file = request.files['video']
167
+ task_id = str(uuid.uuid4())
168
+ path = os.path.join(app.config['UPLOAD_FOLDER'], f"{task_id}.mp4")
169
+ file.save(path)
170
+ tasks[task_id] = {'status': 'Queued', 'result_video': None, 'error_message': None}
171
+
172
+ threading.Thread(target=process_dubbing, args=(task_id, path, request.form.get('voice'), request.form.get('prompt'))).start()
173
+ return jsonify({'task_id': task_id})
174
+
175
+ @app.route('/status')
176
+ def status():
177
+ return jsonify(tasks.get(request.args.get('task_id'), {}))
178
+
179
+ @app.route('/download/<f>')
180
+ def download(f):
181
+ return send_from_directory(app.config['UPLOAD_FOLDER'], f)
182
+
183
  HTML_TEMPLATE = """
184
  <!DOCTYPE html>
185
  <html>
186
  <head>
187
+ <title>AI Sync Dubbing</title>
188
+ <meta name="viewport" content="width=device-width, initial-scale=1">
 
189
  <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
 
 
 
 
 
190
  </head>
191
+ <body class="bg-light">
192
+ <div class="container py-5">
193
+ <div class="card mx-auto shadow" style="max-width: 500px;">
194
+ <div class="card-body">
195
+ <h4 class="text-center mb-4">🎙️ AI Dubbing Sync (Ducking On)</h4>
196
+ <form id="uploadForm">
197
+ <div class="mb-3">
198
+ <label class="form-label">Video (Max 2 Menit)</label>
199
+ <input type="file" id="videoFile" class="form-control" accept="video/*" required>
200
+ </div>
201
+ <div class="mb-3">
202
+ <label class="form-label">Bahasa Target</label>
203
+ <select id="targetVoice" class="form-select">
204
+ <option value="id-ID">Indonesia</option>
205
+ <option value="en-US">English</option>
206
+ <option value="ja-JP">Japanese</option>
207
+ </select>
208
+ </div>
209
+ <div class="mb-3">
210
+ <label class="form-label">Instruksi AI (Opsional)</label>
211
+ <textarea id="customPrompt" class="form-control" placeholder="Contoh: Gunakan bahasa gaul..."></textarea>
212
+ </div>
213
+ <button type="submit" id="btnSubmit" class="btn btn-primary w-100">Mulai Proses</button>
214
+ </form>
215
+
216
+ <div id="loader" class="mt-4 d-none text-center">
217
+ <div class="spinner-border text-primary mb-2"></div>
218
+ <p id="statusTxt">Mengunggah...</p>
219
  </div>
220
+
221
+ <div id="result" class="mt-4 d-none">
222
+ <video id="resVid" controls class="w-100 rounded"></video>
223
+ <a id="dlBtn" href="#" class="btn btn-success w-100 mt-2" download>Download Video</a>
224
  </div>
 
 
 
 
 
 
 
 
 
225
  </div>
226
  </div>
227
  </div>
228
+
229
  <script>
230
  const form = document.getElementById('uploadForm');
231
  form.onsubmit = async (e) => {
232
  e.preventDefault();
 
 
 
 
 
 
 
 
 
 
233
  const fd = new FormData();
234
  fd.append('video', document.getElementById('videoFile').files[0]);
235
  fd.append('voice', document.getElementById('targetVoice').value);
236
  fd.append('prompt', document.getElementById('customPrompt').value);
237
+
238
  document.getElementById('btnSubmit').disabled = true;
239
+ document.getElementById('loader').classList.remove('d-none');
240
+
241
  const res = await fetch('/generate', {method:'POST', body:fd});
242
  const data = await res.json();
243
+ pollStatus(data.task_id);
244
+ };
245
+
246
+ async function pollStatus(id) {
247
  const itv = setInterval(async () => {
248
+ const res = await fetch('/status?task_id=' + id);
249
  const data = await res.json();
250
  document.getElementById('statusTxt').innerText = data.status;
251
+
252
+ if(data.status === 'Selesai') {
253
  clearInterval(itv);
254
+ document.getElementById('loader').classList.add('d-none');
255
+ document.getElementById('result').classList.remove('d-none');
256
  document.getElementById('resVid').src = data.result_video;
257
  document.getElementById('dlBtn').href = data.result_video;
258
  document.getElementById('btnSubmit').disabled = false;
259
+ } else if(data.status === 'Error') {
260
  clearInterval(itv);
261
+ alert("Gagal: " + data.error_message);
262
  location.reload();
263
  }
264
  }, 2000);
 
268
  </html>
269
  """
270
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
271
  if __name__ == '__main__':
272
  app.run(host='0.0.0.0', port=7860)