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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +57 -82
app.py CHANGED
@@ -21,89 +21,64 @@ def safe_subclip(clip, start, end):
21
  return clip.subclip(start, end)
22
 
23
 
 
24
  def remove_silence(video_path, threshold=-30, progress=gr.Progress()):
25
- if not video_path:
26
- return None, "يرجى رفع فيديو أولاً."
27
-
28
- output_filename = f"final_result_{int(time.time())}.mp4"
29
- # ملف مؤقت للفيديو المصلح
30
- fixed_input = f"fixed_temp_{int(time.time())}.mp4"
31
- video = None
32
- final_video = None
33
-
34
- try:
35
- # --- الخطوة الاحترافية: إصلاح المزامنة قبل البدء ---
36
- progress(0.1, desc="جاري معالجة الفيديو لضمان تزامن الصوت (CFR)...")
37
- # تحويل الفيديو إلى 30 إطار ثابت في الثانية لضمان تطابق توقيت الصوت مع الصورة
38
- conversion_command = (
39
- f'ffmpeg -i "{video_path}" -filter:v fps=30 -vsync cfr '
40
- f'-c:a copy "{fixed_input}" -y -loglevel quiet'
41
- )
42
- os.system(conversion_command)
43
-
44
- # التأكد من نجاح عملية التحويل، وإلا نستخدم الملف الأصلي كخطة بديلة
45
- if os.path.exists(fixed_input):
46
- working_path = fixed_input
47
- else:
48
- working_path = video_path
49
-
50
- # --- البدء في معالجة الفيديو المصلح ---
51
- progress(0.2, desc="تحميل الفيديو المصلح...")
52
- video = VideoFileClip(working_path)
53
-
54
- progress(0.3, desc="تحليل موجات الصوت...")
55
- fps_audio = 22050
56
- audio_array = video.audio.to_soundarray(fps=fps_audio)
57
-
58
- if len(audio_array.shape) > 1:
59
- audio_array = np.mean(audio_array, axis=1)
60
-
61
- progress(0.4, desc="اكتشاف مناطق الصمت...")
62
- intervals = librosa.effects.split(audio_array, top_db=abs(threshold))
63
-
64
- if len(intervals) == 0:
65
- return None, "تنبيه: الفيديو صامت تماماً بناءً على الإعدادات الحالية."
66
-
67
- progress(0.6, desc="تجميع المقاطع الصوتية...")
68
- keep_clips = []
69
- for start_idx, end_idx in intervals:
70
- t_start = start_idx / fps_audio
71
- t_end = end_idx / fps_audio
72
- # استخدام الدالة الذكية للقص
73
- keep_clips.append(safe_subclip(video, t_start, t_end))
74
-
75
- progress(0.8, desc="بدء التصدير النهائي (تحسين للأداء)...")
76
- final_video = concatenate_videoclips(keep_clips)
77
-
78
- # التصدير مع الحفاظ على fps=30 لضمان المزامنة في الناتج النهائي
79
- final_video.write_videofile(
80
- output_filename,
81
- fps=30,
82
- codec="libx264",
83
- audio_codec="aac",
84
- threads=4,
85
- preset="ultrafast",
86
- logger=None
87
- )
88
-
89
- progress(1.0, desc="اكتملت العملية بنجاح!")
90
-
91
- # إغلاق الملفات لتحرير الذاكرة
92
- video.close()
93
- final_video.close()
94
-
95
- # حذف الملف المؤقت المصلح لتوفير مساحة القرص
96
- if os.path.exists(fixed_input):
97
- os.remove(fixed_input)
98
-
99
- return output_filename, "تمت المعالجة بنجاح مع مزامنة كاملة للصوت!"
100
-
101
- except Exception as e:
102
- if video: video.close()
103
- # محاولة تنظيف الملف المؤقت في حال حدوث خطأ
104
- if os.path.exists(fixed_input):
105
- os.remove(fixed_input)
106
- return None, f"خطأ تقني: {str(e)}"
107
 
108
  # واجهة المستخدم الاحترافية
109
  with gr.Blocks(theme=gr.themes.Soft(), title="SilentCut Pro") as demo:
 
21
  return clip.subclip(start, end)
22
 
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: