Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,60 +1,55 @@
|
|
| 1 |
import os
|
| 2 |
-
import moviepy.
|
| 3 |
import gradio as gr
|
|
|
|
| 4 |
|
| 5 |
-
|
| 6 |
-
video =
|
| 7 |
duration = video.duration
|
| 8 |
part_duration = duration / parts
|
| 9 |
|
| 10 |
output_files = []
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 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 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
+
# Остальной код остается без изменений
|