kader1997 commited on
Commit
01bdeef
·
verified ·
1 Parent(s): b36cfc4

Upload 3 files

Browse files
Files changed (3) hide show
  1. app.py +112 -0
  2. packages.txt +1 -0
  3. requirements.txt +5 -0
app.py ADDED
@@ -0,0 +1,112 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import time
3
+ import numpy as np
4
+ import librosa
5
+ import gradio as gr
6
+
7
+ # ضبط مسار ImageMagick - تأكد من صحة هذا المسار في جهازك
8
+ os.environ["IMAGEMAGICK_BINARY"] = r"C:\Program Files\ImageMagick-7.1.2-Q16-HDRI\magick.exe"
9
+
10
+ # استيراد مرن يتوافق مع كل النسخ (1.0, 2.0, 2.1+)
11
+ try:
12
+ from moviepy.editor import VideoFileClip, concatenate_videoclips
13
+ except ImportError:
14
+ from moviepy import VideoFileClip, concatenate_videoclips
15
+
16
+
17
+ def safe_subclip(clip, start, end):
18
+ """دالة ذكية تكتشف اسم الدالة الصحيح بناءً على إصدار المكتبة"""
19
+ if hasattr(clip, 'subclipped'):
20
+ return clip.subclipped(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
+ video = None
30
+ final_video = None
31
+
32
+ try:
33
+ progress(0, desc="تحميل الفيديو...")
34
+ video = VideoFileClip(video_path)
35
+
36
+ progress(0.2, desc="تحليل موجات الصوت...")
37
+ fps_audio = 22050
38
+ audio_array = video.audio.to_soundarray(fps=fps_audio)
39
+
40
+ if len(audio_array.shape) > 1:
41
+ audio_array = np.mean(audio_array, axis=1)
42
+
43
+ progress(0.4, desc="اكتشاف مناطق الصمت...")
44
+ intervals = librosa.effects.split(audio_array, top_db=abs(threshold))
45
+
46
+ if len(intervals) == 0:
47
+ return None, "تنبيه: الفيديو صامت تماماً بناءً على الإعدادات الحالية."
48
+
49
+ progress(0.5, desc="تجميع المقاطع الصوتية...")
50
+ keep_clips = []
51
+ for start_idx, end_idx in intervals:
52
+ t_start = start_idx / fps_audio
53
+ t_end = end_idx / fps_audio
54
+ keep_clips.append(safe_subclip(video, t_start, t_end))
55
+
56
+ progress(0.7, desc="بدء التصدير النهائي (قد يستغرق وقتاً)...")
57
+ final_video = concatenate_videoclips(keep_clips)
58
+
59
+ # التصدير بأقصى سرعة ممكنة لمعالج i5
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
+ # تحرير الذاكرة والملفات
73
+ video.close()
74
+ final_video.close()
75
+
76
+ return output_filename, "تمت المعالجة بنجاح! يمكنك تحميل الفيديو الآن."
77
+
78
+ except Exception as e:
79
+ if video: video.close()
80
+ return None, f"خطأ تقني: {str(e)}"
81
+
82
+
83
+ # واجهة المستخدم الاحترافية
84
+ with gr.Blocks(theme=gr.themes.Soft(), title="SilentCut Pro") as demo:
85
+ gr.Markdown("""
86
+ # 🎬 SilentCut Pro
87
+ ### حذف الصمت آلياً | سرعة عالية | جودة فائقة
88
+ """)
89
+
90
+ with gr.Row():
91
+ with gr.Column():
92
+ v_in = gr.Video(label="ارفع الفيديو (MP4, AVI, MOV)")
93
+ slider = gr.Slider(
94
+ minimum=-60, maximum=-10, value=-30,
95
+ label="حساسية الصمت (ديسيبل)",
96
+ info="قلل الرقم (مثلاً -40) إذا كان البرنامج يقص أجزاء من كلامك."
97
+ )
98
+ btn = gr.Button("🚀 بدء حذف الصمت", variant="primary")
99
+
100
+ with gr.Column():
101
+ v_out = gr.Video(label="الفيديو الناتج")
102
+ status = gr.Textbox(label="الحالة والتقدم", interactive=False)
103
+
104
+ btn.click(
105
+ fn=remove_silence,
106
+ inputs=[v_in, slider],
107
+ outputs=[v_out, status]
108
+ )
109
+
110
+ if __name__ == "__main__":
111
+ # تم تفعيل share=True ليعطيك رابطاً خارجياً إذا أردت استخدامه من جهاز آخر
112
+ demo.launch(share=False)
packages.txt ADDED
@@ -0,0 +1 @@
 
 
1
+ imagemagick
requirements.txt ADDED
@@ -0,0 +1,5 @@
 
 
 
 
 
 
1
+ moviepy
2
+ gradio
3
+ librosa
4
+ numpy
5
+ imageio-ffmpeg