kader1997 commited on
Commit
22dab7d
·
verified ·
1 Parent(s): 02b06fb

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +55 -56
app.py CHANGED
@@ -23,62 +23,61 @@ def safe_subclip(clip, start, end):
23
 
24
 
25
  def remove_silence(video_path, threshold=-30, progress=gr.Progress()):
26
-     if not video_path:
27
-         return None, "يرجى رفع فيديو أولاً."
28
-
29
-     output_filename = f"final_result_{int(time.time())}.mp4"
30
-     video = None
31
-     final_video = None
32
-
33
-     try:
34
-         progress(0, desc="تحميل الفيديو...")
35
-         video = VideoFileClip(video_path)
36
-
37
-         progress(0.2, desc="تحليل موجات الصوت...")
38
-         fps_audio = 22050
39
-         audio_array = video.audio.to_soundarray(fps=fps_audio)
40
-
41
-         if len(audio_array.shape) > 1:
42
-             audio_array = np.mean(audio_array, axis=1)
43
-
44
-         progress(0.4, desc="اكتشاف مناطق الصمت...")
45
-         intervals = librosa.effects.split(audio_array, top_db=abs(threshold))
46
-
47
-         if len(intervals) == 0:
48
-             return None, "تنبيه: الفيديو صامت تماماً بناءً على الإعدادات الحالية."
49
-
50
-         progress(0.5, desc="تجميع المقاطع الصوتية...")
51
-         keep_clips = []
52
-         for start_idx, end_idx in intervals:
53
-             t_start = start_idx / fps_audio
54
-             t_end = end_idx / fps_audio
55
-             keep_clips.append(safe_subclip(video, t_start, t_end))
56
-
57
-         progress(0.7, desc="بدء التصدير النهائي (قد يستغرق وقتاً)...")
58
-         final_video = concatenate_videoclips(keep_clips)
59
-
60
-         # التصدير بأقصى سرعة ممكنة لمعالج i5
61
-         final_video.write_videofile(
62
-             output_filename,
63
-             fps=video.fps,
64
-             codec="libx264",
65
-             audio_codec="aac",
66
-             threads=4,
67
-             preset="ultrafast",
68
-             logger=None
69
-         )
70
-
71
-         progress(1.0, desc="اكتملت العملية!")
72
-
73
-         # تحرير الذاكرة والملفات
74
-         video.close()
75
-         final_video.close()
76
-
77
-         return output_filename, "تمت المعالجة بنجاح! يمكنك تحميل الفيديو الآن."
78
-
79
-     except Exception as e:
80
-         if video: video.close()
81
-         return None, f"خطأ تقني: {str(e)}"
82
 
83
  # واجهة المستخدم الاحترافية
84
  with gr.Blocks(theme=gr.themes.Soft(), title="SilentCut Pro") as demo:
 
23
 
24
 
25
  def remove_silence(video_path, threshold=-30, progress=gr.Progress()):
26
+ if not video_path:
27
+ return None, "يرجى رفع فيديو أولاً."
28
+
29
+ output_filename = f"final_result_{int(time.time())}.mp4"
30
+ video = None
31
+ final_video = None
32
+
33
+ try:
34
+ progress(0, desc="تحميل الفيديو...")
35
+ video = VideoFileClip(video_path)
36
+
37
+ progress(0.2, desc="تحليل موجات الصوت...")
38
+ fps_audio = 22050
39
+ audio_array = video.audio.to_soundarray(fps=fps_audio)
40
+
41
+ if len(audio_array.shape) > 1:
42
+ audio_array = np.mean(audio_array, axis=1)
43
+
44
+ progress(0.4, desc="اكتشاف مناطق الصمت...")
45
+ intervals = librosa.effects.split(audio_array, top_db=abs(threshold))
46
+
47
+ if len(intervals) == 0:
48
+ return None, "تنبيه: الفيديو صامت تماماً بناءً على الإعدادات الحالية."
49
+
50
+ progress(0.5, desc="تجميع المقاطع الصوتية...")
51
+ keep_clips = []
52
+ for start_idx, end_idx in intervals:
53
+ t_start = start_idx / fps_audio
54
+ t_end = end_idx / fps_audio
55
+ keep_clips.append(safe_subclip(video, t_start, t_end))
56
+
57
+ progress(0.7, desc="بدء التصدير النهائي (قد يستغرق وقتاً)...")
58
+ final_video = concatenate_videoclips(keep_clips)
59
+
60
+ final_video.write_videofile(
61
+ output_filename,
62
+ fps=video.fps,
63
+ codec="libx264",
64
+ audio_codec="aac",
65
+ threads=4,
66
+ preset="ultrafast",
67
+ logger=None
68
+ )
69
+
70
+ progress(1.0, desc="اكتملت العملية!")
71
+
72
+ video.close()
73
+ final_video.close()
74
+
75
+ return output_filename, "تمت المعالجة بنجاح! يمكنك تحميل الفيديو الآن."
76
+
77
+ except Exception as e:
78
+ if video:
79
+ video.close()
80
+ return None, f"خطأ تقني: {str(e)}"
 
81
 
82
  # واجهة المستخدم الاحترافية
83
  with gr.Blocks(theme=gr.themes.Soft(), title="SilentCut Pro") as demo: