AndreySokolov01 commited on
Commit
a52a536
1 Parent(s): 301c99a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +45 -50
app.py CHANGED
@@ -1,60 +1,55 @@
1
  import os
2
- import moviepy.editor_async as mp_async
3
  import gradio as gr
 
4
 
5
- async def split_video(video_file, parts, resolution, quality, audio_enabled, volume):
6
- video = mp_async.VideoFileClip(video_file.name)
7
  duration = video.duration
8
  part_duration = duration / parts
9
 
10
  output_files = []
11
- for part in range(parts):
12
- start_time = part * part_duration
13
- end_time = (part + 1) * part_duration
14
- part_clip = await video.subclip_async(start_time, end_time)
15
-
16
- # Настройка параметров кодирования
17
- if quality == "Низкое":
18
- bitrate = "1M"
19
- elif quality == "Среднее":
20
- bitrate = "2M"
21
- else:
22
- bitrate = "4M"
23
-
24
- # Настройка параметров звука
25
- if not audio_enabled:
26
- part_clip.audio = None
27
- else:
28
- part_clip.audio = part_clip.audio.volumex(volume)
29
-
30
- # Сохранение видео
31
- output_filename = f"part_{part + 1}.mp4"
32
- await part_clip.write_videofile_async(
33
- output_filename,
34
- codec="libx264",
35
- audio_codec="aac",
36
- bitrate=bitrate,
37
- preset="medium",
38
- ffmpeg_params=["-vf", f"scale={resolution}"],
39
- )
40
- output_files.append(output_filename)
41
 
42
  return output_files
43
 
44
- iface = gr.Interface(
45
- fn=split_video,
46
- inputs=[
47
- gr.File(label="Upload Video"),
48
- gr.Slider(minimum=2, maximum=10, value=2, label="Number of Parts"),
49
- gr.Dropdown(choices=["720p", "1080p", "4K"], label="Resolution"),
50
- gr.Dropdown(choices=["Низкое", "Среднее", "Высокое"], label="Качество"),
51
- gr.Checkbox(label="Включить звук"),
52
- gr.Slider(minimum=0, maximum=2, value=1, label="Громкость"),
53
- ],
54
- outputs=gr.Files(label="Download Split Videos"),
55
- title="Video Splitter",
56
- description="Upload your video and select how many parts you want to split it into."
57
- )
58
-
59
- if __name__ == "__main__":
60
- iface.launch(debug=True)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import os
2
+ import moviepy.editor as mp
3
  import gradio as gr
4
+ from concurrent.futures import ThreadPoolExecutor
5
 
6
+ def split_video(video_file, parts, resolution, quality, audio_enabled, volume):
7
+ video = mp.VideoFileClip(video_file.name)
8
  duration = video.duration
9
  part_duration = duration / parts
10
 
11
  output_files = []
12
+ with ThreadPoolExecutor() as executor:
13
+ futures = []
14
+ for part in range(parts):
15
+ start_time = part * part_duration
16
+ end_time = (part + 1) * part_duration
17
+ futures.append(executor.submit(process_part, video, start_time, end_time, resolution, quality, audio_enabled, volume, part))
18
+
19
+ for future in futures:
20
+ output_filename = future.result()
21
+ output_files.append(output_filename)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
22
 
23
  return output_files
24
 
25
+ def process_part(video, start_time, end_time, resolution, quality, audio_enabled, volume, part):
26
+ part_clip = video.subclip(start_time, end_time)
27
+
28
+ # Настройка параметров кодирования
29
+ if quality == "Низкое":
30
+ bitrate = "1M"
31
+ elif quality == "Среднее":
32
+ bitrate = "2M"
33
+ else:
34
+ bitrate = "4M"
35
+
36
+ # Настройка параметров звука
37
+ if not audio_enabled:
38
+ part_clip.audio = None
39
+ else:
40
+ part_clip.audio = part_clip.audio.volumex(volume)
41
+
42
+ # Сохранение видео
43
+ output_filename = f"part_{part + 1}.mp4"
44
+ part_clip.write_videofile(
45
+ output_filename,
46
+ codec="libx264",
47
+ audio_codec="aac",
48
+ bitrate=bitrate,
49
+ preset="medium",
50
+ ffmpeg_params=["-vf", f"scale={resolution}"],
51
+ )
52
+
53
+ return output_filename
54
+
55
+ # Остальной код остается без изменений