Ricky01anjay commited on
Commit
55e7aef
·
verified ·
1 Parent(s): ea89406

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +77 -54
app.py CHANGED
@@ -8,6 +8,7 @@ import json
8
  from flask import Flask, request, jsonify, render_template_string, send_file
9
  from moviepy.editor import VideoFileClip, TextClip, CompositeVideoClip, concatenate_videoclips
10
  from moviepy.audio.fx.all import audio_fadein, audio_fadeout
 
11
  from faster_whisper import WhisperModel
12
 
13
  app = Flask(__name__)
@@ -19,7 +20,7 @@ JOBS = {}
19
  whisper_model = WhisperModel("base", device="cpu", compute_type="int8")
20
 
21
  # ==========================================
22
- # UI HTML (Tetap sama, elegan & modern)
23
  # ==========================================
24
  HTML_TEMPLATE = """
25
  <!DOCTYPE html>
@@ -27,37 +28,37 @@ HTML_TEMPLATE = """
27
  <head>
28
  <meta charset="UTF-8">
29
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
30
- <title>AI Pro Clip Editor - Smooth Edit</title>
31
  <script src="https://cdn.tailwindcss.com"></script>
32
  </head>
33
- <body class="bg-gray-950 text-gray-200 min-h-screen flex flex-col items-center p-6">
34
  <div class="w-full max-w-2xl bg-gray-900 p-8 rounded-3xl border border-gray-800 shadow-2xl">
35
- <h1 class="text-3xl font-black text-center mb-2 bg-gradient-to-r from-green-400 to-blue-500 bg-clip-text text-transparent">AI SMOOTH CLIPPER</h1>
36
- <p class="text-center text-gray-500 text-sm mb-8">Pemotongan akurat pada jeda napas & kata</p>
37
 
38
  <form id="clipForm" class="space-y-6">
39
- <input type="url" id="videoUrl" class="w-full p-4 bg-gray-800 rounded-xl border border-gray-700 outline-none focus:ring-2 focus:ring-green-500" placeholder="Masukkan Link Video MP4...">
40
- <div class="text-center font-bold text-gray-700 text-xs tracking-widest">ATAU UPLOAD FILE</div>
41
- <input type="file" id="videoFile" accept="video/mp4" class="w-full text-sm text-gray-400 file:mr-4 file:py-2 file:px-4 file:rounded-full file:border-0 file:bg-green-600 file:text-white hover:file:bg-green-700">
42
 
43
- <button type="submit" class="w-full bg-green-600 hover:bg-green-500 text-white font-black py-4 rounded-xl transition-all transform active:scale-95 shadow-xl">BUAT VIDEO VIRAL ✨</button>
44
  </form>
45
 
46
  <div id="statusArea" class="mt-10 hidden">
47
  <div class="flex justify-between mb-2">
48
- <span id="statusText" class="text-sm font-bold text-green-400 italic">Menganalisis...</span>
49
- <span id="percentText" class="text-sm font-black">0%</span>
50
  </div>
51
- <div class="w-full bg-gray-800 rounded-full h-4 p-1">
52
- <div id="progressBar" class="bg-gradient-to-r from-green-500 to-blue-500 h-2 rounded-full transition-all duration-700" style="width: 0%"></div>
53
  </div>
54
  </div>
55
 
56
- <div id="resultArea" class="mt-10 hidden text-center">
57
- <video id="player" controls class="w-full rounded-2xl border border-gray-700 shadow-2xl bg-black mb-6" style="max-height: 450px;"></video>
58
  <div class="flex gap-3">
59
  <a id="downloadBtn" href="#" class="flex-1 bg-white text-black font-black py-3 rounded-xl hover:bg-gray-200 transition">DOWNLOAD HD</a>
60
- <button onclick="location.reload()" class="px-6 bg-gray-800 rounded-xl border border-gray-700 hover:bg-gray-700">BUAT LAGI</button>
61
  </div>
62
  </div>
63
  </div>
@@ -71,14 +72,20 @@ HTML_TEMPLATE = """
71
 
72
  if (file) formData.append('video_file', file);
73
  else if (url) formData.append('video_url', url);
74
- else return alert("Pilih video dulu!");
75
 
76
  document.getElementById('statusArea').classList.remove('hidden');
77
  document.getElementById('clipForm').classList.add('hidden');
78
 
79
- const res = await fetch('/generate', { method: 'POST', body: formData });
80
- const data = await res.json();
81
- if (data.job_id) pollStatus(data.job_id);
 
 
 
 
 
 
82
  });
83
 
84
  async function pollStatus(jobId) {
@@ -112,11 +119,11 @@ HTML_TEMPLATE = """
112
 
113
  def time_to_seconds(time_str):
114
  try:
115
- parts = list(map(int, time_str.split(':')))
116
  if len(parts) == 3: return parts[0] * 3600 + parts[1] * 60 + parts[2]
117
  if len(parts) == 2: return parts[0] * 60 + parts[1]
118
  return parts[0]
119
- except: return 0
120
 
121
  def background_processor(job_id, video_path):
122
  try:
@@ -124,7 +131,7 @@ def background_processor(job_id, video_path):
124
  original_height = full_clip.h
125
  total_duration = full_clip.duration
126
 
127
- # 1. Transkripsi dengan akurasi per kata
128
  JOBS[job_id].update({"message": "AI mentranskrip & memetakan jeda...", "progress": 15})
129
  segments_whisper, _ = whisper_model.transcribe(video_path, word_timestamps=True)
130
 
@@ -135,22 +142,19 @@ def background_processor(job_id, video_path):
135
  words_data.append(word)
136
  transcript_text += f"[{int(segment.start//60):02d}:{int(segment.start%60):02d} - {int(segment.end//60):02d}:{int(segment.end%60):02d}] {segment.text.strip()}\n"
137
 
138
- # 2. AI Editor Memilih Klip
139
  JOBS[job_id].update({"message": "Menyusun cerita yang nyambung...", "progress": 35})
140
 
141
- prompt = f"""Kamu adalah Video Editor. Tugasmu memilih 2-3 segmen berdurasi agak panjang (minimal 10-15 detik per klip) dari video ini agar membentuk satu cerita utuh.
142
- JANGAN memilih klip yang terlalu pendek agar video tidak terlihat patah-patah.
 
143
 
144
- Syarat:
145
- 1. Setiap segmen HARUS kalimat utuh.
146
- 2. Ceritanya harus nyambung dari awal sampai akhir.
147
- 3. Balas HANYA dengan JSON array:
148
  [
149
- {{"start": "MM:SS", "end": "MM:SS", "narasi": "Alasan"}},
150
- {{"start": "MM:SS", "end": "MM:SS", "narasi": "Alasan"}}
151
  ]
152
 
153
- Transkrip:
154
  {transcript_text[:3500]}
155
  """
156
  try:
@@ -159,76 +163,93 @@ Transkrip:
159
  json_match = re.search(r'\[\s*\{.*?\}\s*\]', ai_ans, re.DOTALL)
160
  points = json.loads(json_match.group(0)) if json_match else []
161
  except:
162
- points = [{"start": "00:00", "end": "00:15", "narasi": "Fallback"}]
 
 
163
 
164
- if not points: raise Exception("AI gagal merumuskan JSON.")
 
165
 
166
- # 3. LOGIKA SMART BOUNDARY (Pemotongan Akurat di Jeda Kata)
167
- JOBS[job_id].update({"message": "Menghaluskan titik potong audio...", "progress": 55})
168
 
169
  final_clips_to_concat = []
170
  font_path = "/usr/share/fonts/truetype/dejavu/DejaVuSans-Bold.ttf"
171
  if not os.path.exists(font_path): font_path = "Arial"
172
 
 
 
173
  for p in points:
174
  raw_start = time_to_seconds(p.get('start', '00:00'))
175
- raw_end = time_to_seconds(p.get('end', '00:15'))
176
 
177
- # Cari kata aktual yang paling dekat dengan pilihan AI
 
 
 
 
 
 
178
  start_word = next((w for w in words_data if w.start >= max(0, raw_start - 2)), None)
179
  end_word = next((w for w in reversed(words_data) if w.end <= min(total_duration, raw_end + 2)), None)
180
 
181
  if start_word and end_word and start_word.start < end_word.end:
182
- # Beri ruang napas 0.3 detik sebelum dan sesudah kata diucapkan
183
- actual_start = max(0, start_word.start - 0.3)
184
- actual_end = min(total_duration, end_word.end + 0.3)
185
  else:
186
- actual_start = max(0, raw_start - 0.3)
187
- actual_end = min(total_duration, raw_end + 0.3)
 
 
188
 
189
- # Potong Video Berdasarkan Kata (Bukan Angka Mentah AI)
190
  subclip = full_clip.subclip(actual_start, actual_end)
191
 
192
- # Terapkan AUDIO FADE IN & OUT agar perpindahan tidak "Klik/Cekrek"
 
 
193
  subclip = subclip.set_audio(
194
- subclip.audio.fx(audio_fadein, 0.1).fx(audio_fadeout, 0.1)
195
  )
196
 
197
- # Tambahkan Subtitle HANYA untuk klip ini (Sinkronisasi 100% sempurna)
198
  txt_clips = []
199
  for wd in words_data:
200
  if wd.start >= actual_start and wd.end <= actual_end:
201
  rel_start = wd.start - actual_start
202
  rel_end = wd.end - actual_start
203
 
 
204
  t = TextClip(
205
  wd.word.strip().upper(),
206
  font=font_path,
207
- fontsize=original_height * 0.06,
208
  color='yellow',
209
  stroke_color='black',
210
- stroke_width=2,
211
  method='label'
212
  ).set_start(rel_start).set_end(rel_end).set_position(('center', int(original_height * 0.85)))
213
 
214
  txt_clips.append(t)
215
 
216
- # Gabungkan Video + Subtitle untuk segmen ini
217
  composite_subclip = CompositeVideoClip([subclip] + txt_clips)
218
  final_clips_to_concat.append(composite_subclip)
219
 
220
- # 4. GABUNGKAN SEMUA KLIP YANG SUDAH JADI
221
  JOBS[job_id].update({"message": "Merender video akhir...", "progress": 70})
222
 
223
  if not final_clips_to_concat:
224
- raise Exception("Gagal memotong video. Durasi tidak valid.")
225
 
 
226
  merged_video = concatenate_videoclips(final_clips_to_concat, method="compose")
227
 
228
  output_filename = f"final_{job_id}.mp4"
229
  output_path = os.path.join(app.config['UPLOAD_FOLDER'], output_filename)
230
  JOBS[job_id]['files_to_delete'].append(output_path)
231
 
 
232
  merged_video.write_videofile(
233
  output_path,
234
  fps=24,
@@ -239,6 +260,7 @@ Transkrip:
239
  logger=None
240
  )
241
 
 
242
  full_clip.close()
243
  merged_video.close()
244
  for c in final_clips_to_concat: c.close()
@@ -246,7 +268,8 @@ Transkrip:
246
  JOBS[job_id].update({"status": "completed", "progress": 100, "message": "Video Selesai!", "result": output_filename})
247
 
248
  except Exception as e:
249
- JOBS[job_id].update({"status": "error", "message": str(e)})
 
250
 
251
  # ==========================================
252
  # ROUTES
 
8
  from flask import Flask, request, jsonify, render_template_string, send_file
9
  from moviepy.editor import VideoFileClip, TextClip, CompositeVideoClip, concatenate_videoclips
10
  from moviepy.audio.fx.all import audio_fadein, audio_fadeout
11
+ import moviepy.video.fx.all as vfx # Tambahan untuk transisi visual
12
  from faster_whisper import WhisperModel
13
 
14
  app = Flask(__name__)
 
20
  whisper_model = WhisperModel("base", device="cpu", compute_type="int8")
21
 
22
  # ==========================================
23
+ # UI HTML (Tetap Modern)
24
  # ==========================================
25
  HTML_TEMPLATE = """
26
  <!DOCTYPE html>
 
28
  <head>
29
  <meta charset="UTF-8">
30
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
31
+ <title>AI Cinematic Clipper</title>
32
  <script src="https://cdn.tailwindcss.com"></script>
33
  </head>
34
+ <body class="bg-gray-950 text-gray-200 min-h-screen flex flex-col items-center p-6 font-sans">
35
  <div class="w-full max-w-2xl bg-gray-900 p-8 rounded-3xl border border-gray-800 shadow-2xl">
36
+ <h1 class="text-3xl font-black text-center mb-1 bg-gradient-to-r from-purple-400 to-blue-500 bg-clip-text text-transparent">AI CINEMATIC CLIPPER</h1>
37
+ <p class="text-center text-gray-400 text-sm mb-8">Hasil mulus, transisi rapi, narasi nyambung</p>
38
 
39
  <form id="clipForm" class="space-y-6">
40
+ <input type="url" id="videoUrl" class="w-full p-4 bg-gray-800 rounded-xl border border-gray-700 outline-none focus:ring-2 focus:ring-purple-500 transition" placeholder="Masukkan Link Video MP4...">
41
+ <div class="text-center font-bold text-gray-600 text-xs tracking-widest">ATAU UPLOAD FILE</div>
42
+ <input type="file" id="videoFile" accept="video/mp4" class="w-full text-sm text-gray-400 file:mr-4 file:py-2 file:px-4 file:rounded-full file:border-0 file:bg-purple-600 file:text-white hover:file:bg-purple-700 transition">
43
 
44
+ <button type="submit" class="w-full bg-purple-600 hover:bg-purple-500 text-white font-black py-4 rounded-xl transition-all transform active:scale-95 shadow-lg">BUAT VIDEO SEKARANG ✨</button>
45
  </form>
46
 
47
  <div id="statusArea" class="mt-10 hidden">
48
  <div class="flex justify-between mb-2">
49
+ <span id="statusText" class="text-sm font-bold text-purple-400 italic">Memulai AI...</span>
50
+ <span id="percentText" class="text-sm font-black text-white">0%</span>
51
  </div>
52
+ <div class="w-full bg-gray-800 rounded-full h-3 p-1">
53
+ <div id="progressBar" class="bg-gradient-to-r from-purple-500 to-blue-500 h-1.5 rounded-full transition-all duration-700" style="width: 0%"></div>
54
  </div>
55
  </div>
56
 
57
+ <div id="resultArea" class="mt-10 hidden text-center animate-fade-in">
58
+ <video id="player" controls playsinline class="w-full rounded-2xl border border-gray-700 shadow-2xl bg-black mb-6" style="max-height: 450px;"></video>
59
  <div class="flex gap-3">
60
  <a id="downloadBtn" href="#" class="flex-1 bg-white text-black font-black py-3 rounded-xl hover:bg-gray-200 transition">DOWNLOAD HD</a>
61
+ <button onclick="location.reload()" class="px-6 bg-gray-800 text-white rounded-xl border border-gray-700 hover:bg-gray-700 transition">BUAT LAGI</button>
62
  </div>
63
  </div>
64
  </div>
 
72
 
73
  if (file) formData.append('video_file', file);
74
  else if (url) formData.append('video_url', url);
75
+ else return alert("Pilih video atau masukkan link!");
76
 
77
  document.getElementById('statusArea').classList.remove('hidden');
78
  document.getElementById('clipForm').classList.add('hidden');
79
 
80
+ try {
81
+ const res = await fetch('/generate', { method: 'POST', body: formData });
82
+ const data = await res.json();
83
+ if (data.job_id) pollStatus(data.job_id);
84
+ else throw new Error(data.error || "Gagal memulai");
85
+ } catch (err) {
86
+ alert(err.message);
87
+ location.reload();
88
+ }
89
  });
90
 
91
  async function pollStatus(jobId) {
 
119
 
120
  def time_to_seconds(time_str):
121
  try:
122
+ parts = list(map(float, time_str.split(':')))
123
  if len(parts) == 3: return parts[0] * 3600 + parts[1] * 60 + parts[2]
124
  if len(parts) == 2: return parts[0] * 60 + parts[1]
125
  return parts[0]
126
+ except: return 0.0
127
 
128
  def background_processor(job_id, video_path):
129
  try:
 
131
  original_height = full_clip.h
132
  total_duration = full_clip.duration
133
 
134
+ # 1. Transkripsi dengan Akurasi Tinggi
135
  JOBS[job_id].update({"message": "AI mentranskrip & memetakan jeda...", "progress": 15})
136
  segments_whisper, _ = whisper_model.transcribe(video_path, word_timestamps=True)
137
 
 
142
  words_data.append(word)
143
  transcript_text += f"[{int(segment.start//60):02d}:{int(segment.start%60):02d} - {int(segment.end//60):02d}:{int(segment.end%60):02d}] {segment.text.strip()}\n"
144
 
145
+ # 2. AI Editor (Aturan Diperketat)
146
  JOBS[job_id].update({"message": "Menyusun cerita yang nyambung...", "progress": 35})
147
 
148
+ prompt = f"""Kamu adalah Video Editor Profesional. Tugasmu merangkum video ini menjadi maksimal 1 atau 2 klip panjang.
149
+ JANGAN memberikan klip yang pendek-pendek! Setiap klip MINIMAL berdurasi 15 detik sampai 30 detik.
150
+ Klip yang kamu pilih harus menyambung menjadi satu cerita yang masuk akal.
151
 
152
+ Balas HANYA dengan JSON array:
 
 
 
153
  [
154
+ {{"start": "MM:SS", "end": "MM:SS", "narasi": "Alasan memilih segmen ini"}}
 
155
  ]
156
 
157
+ Transkrip Video:
158
  {transcript_text[:3500]}
159
  """
160
  try:
 
163
  json_match = re.search(r'\[\s*\{.*?\}\s*\]', ai_ans, re.DOTALL)
164
  points = json.loads(json_match.group(0)) if json_match else []
165
  except:
166
+ points = [{"start": "00:00", "end": "00:20", "narasi": "Fallback"}]
167
+
168
+ if not points: raise Exception("AI gagal merumuskan skenario.")
169
 
170
+ # URUTKAN WAKTU agar video tidak terbolak-balik
171
+ points = sorted(points, key=lambda x: time_to_seconds(x.get('start', '00:00')))
172
 
173
+ # 3. Logika Smart Boundary & Transisi Visual
174
+ JOBS[job_id].update({"message": "Memotong dan merender efek...", "progress": 55})
175
 
176
  final_clips_to_concat = []
177
  font_path = "/usr/share/fonts/truetype/dejavu/DejaVuSans-Bold.ttf"
178
  if not os.path.exists(font_path): font_path = "Arial"
179
 
180
+ last_end_time = -1 # Tracker untuk mencegah overlap
181
+
182
  for p in points:
183
  raw_start = time_to_seconds(p.get('start', '00:00'))
184
+ raw_end = time_to_seconds(p.get('end', '00:20'))
185
 
186
+ # Anti-Overlap (Mencegah klip tabrakan)
187
+ if raw_start <= last_end_time:
188
+ raw_start = last_end_time + 0.5
189
+ if raw_end <= raw_start + 2: # Lewati jika durasi klip terlalu pendek (error AI)
190
+ continue
191
+
192
+ # Sensor Kata: Potong tepat sebelum kata pertama dan sesudah kata terakhir
193
  start_word = next((w for w in words_data if w.start >= max(0, raw_start - 2)), None)
194
  end_word = next((w for w in reversed(words_data) if w.end <= min(total_duration, raw_end + 2)), None)
195
 
196
  if start_word and end_word and start_word.start < end_word.end:
197
+ actual_start = max(0, start_word.start - 0.2)
198
+ actual_end = min(total_duration, end_word.end + 0.2)
 
199
  else:
200
+ actual_start = max(0, raw_start - 0.2)
201
+ actual_end = min(total_duration, raw_end + 0.2)
202
+
203
+ last_end_time = actual_end # Update tracker
204
 
205
+ # Ambil Subclip
206
  subclip = full_clip.subclip(actual_start, actual_end)
207
 
208
+ # EFEK TRANSISI PRO: Visual Fade + Audio Fade (0.15 detik)
209
+ # Ini akan menghilangkan jump cut kasar dan audio pop
210
+ subclip = subclip.fx(vfx.fadein, 0.15).fx(vfx.fadeout, 0.15)
211
  subclip = subclip.set_audio(
212
+ subclip.audio.fx(audio_fadein, 0.15).fx(audio_fadeout, 0.15)
213
  )
214
 
215
+ # Generate Subtitle HANYA untuk klip ini
216
  txt_clips = []
217
  for wd in words_data:
218
  if wd.start >= actual_start and wd.end <= actual_end:
219
  rel_start = wd.start - actual_start
220
  rel_end = wd.end - actual_start
221
 
222
+ # Style subtitle dibuat lebih tebal agar mudah dibaca
223
  t = TextClip(
224
  wd.word.strip().upper(),
225
  font=font_path,
226
+ fontsize=original_height * 0.065,
227
  color='yellow',
228
  stroke_color='black',
229
+ stroke_width=3,
230
  method='label'
231
  ).set_start(rel_start).set_end(rel_end).set_position(('center', int(original_height * 0.85)))
232
 
233
  txt_clips.append(t)
234
 
235
+ # Gabungkan gambar video dengan subtitle
236
  composite_subclip = CompositeVideoClip([subclip] + txt_clips)
237
  final_clips_to_concat.append(composite_subclip)
238
 
239
+ # 4. GABUNGKAN SEMUA
240
  JOBS[job_id].update({"message": "Merender video akhir...", "progress": 70})
241
 
242
  if not final_clips_to_concat:
243
+ raise Exception("AI gagal memilih klip yang valid. Coba ulangi.")
244
 
245
+ # Compose clips
246
  merged_video = concatenate_videoclips(final_clips_to_concat, method="compose")
247
 
248
  output_filename = f"final_{job_id}.mp4"
249
  output_path = os.path.join(app.config['UPLOAD_FOLDER'], output_filename)
250
  JOBS[job_id]['files_to_delete'].append(output_path)
251
 
252
+ # Render Optimize
253
  merged_video.write_videofile(
254
  output_path,
255
  fps=24,
 
260
  logger=None
261
  )
262
 
263
+ # Clean Memory Beban Berat
264
  full_clip.close()
265
  merged_video.close()
266
  for c in final_clips_to_concat: c.close()
 
268
  JOBS[job_id].update({"status": "completed", "progress": 100, "message": "Video Selesai!", "result": output_filename})
269
 
270
  except Exception as e:
271
+ print(f"ERROR JOB {job_id}: {str(e)}")
272
+ JOBS[job_id].update({"status": "error", "message": "Gagal merender video. Silakan coba lagi."})
273
 
274
  # ==========================================
275
  # ROUTES