AndreySokolov01 commited on
Commit
4b8dbde
1 Parent(s): 48d0e40

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +34 -51
app.py CHANGED
@@ -1,70 +1,53 @@
1
- import os
2
- import moviepy.video.io.VideoFileClip 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
  iface = gr.Interface(
56
  fn=split_video,
57
  inputs=[
58
  gr.File(label="Upload Video"),
59
- gr.Slider(minimum=2, maximum=10, value=2, label="Number of Parts"),
60
- gr.Dropdown(choices=["720p", "1080p", "4K"], label="Resolution"),
61
- gr.Dropdown(choices=["Низкое", "Среднее", "Высокое"], label="Качество"),
62
- gr.Checkbox(label="Включить звук"),
63
- gr.Slider(minimum=0, maximum=2, value=1, label="Громкость"),
64
  ],
65
  outputs=gr.Files(label="Download Split Videos"),
66
  title="Video Splitter",
67
- description="Upload your video and select how many parts you want to split it into."
68
  )
69
 
70
  if __name__ == "__main__":
 
 
 
1
  import gradio as gr
2
+ import moviepy.editor as mp
3
+ from moviepy.video.io.ffmpeg_tools import ffmpeg_extract_subclip
4
 
5
+ def split_video(video_file, split_method, split_value):
6
  video = mp.VideoFileClip(video_file.name)
7
  duration = video.duration
 
8
 
9
+ if split_method == "Free Split":
10
+ split_times = [float(t) for t in split_value.split(",")]
11
+ split_times = [t for t in split_times if t >= 0 and t <= duration]
12
+ split_times.sort()
13
+ split_times = [0] + split_times + [duration]
14
+ clips = [video.subclip(start, end) for start, end in zip(split_times[:-1], split_times[1:])]
15
+ elif split_method == "Average Split":
16
+ num_parts = int(split_value)
17
+ part_duration = duration / num_parts
18
+ clips = [video.subclip(i * part_duration, (i + 1) * part_duration) for i in range(num_parts)]
19
+ elif split_method == "Time Split":
20
+ split_times = [float(t) for t in split_value.split(",")]
21
+ clips = [ffmpeg_extract_subclip(video_file.name, start, end) for start, end in zip([0] + split_times, split_times + [duration])]
22
+ elif split_method == "Size Split":
23
+ target_size = int(split_value) * 1024 * 1024 # Convert from MB to bytes
24
+ clips = []
25
+ start = 0
26
+ while start < duration:
27
+ end = start + 1
28
+ while end <= duration and video.subclip(start, end).size < target_size:
29
+ end += 1
30
+ clips.append(video.subclip(start, end))
31
+ start = end
32
 
33
+ output_files = []
34
+ for i, clip in enumerate(clips):
35
+ output_filename = f"split_{i + 1}.mp4"
36
+ clip.write_videofile(output_filename)
37
+ output_files.append(output_filename)
38
 
39
  return output_files
40
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
41
  iface = gr.Interface(
42
  fn=split_video,
43
  inputs=[
44
  gr.File(label="Upload Video"),
45
+ gr.Dropdown(choices=["Free Split", "Average Split", "Time Split", "Size Split"], label="Split Method"),
46
+ gr.Textbox(label="Split Value (comma-separated for Free Split and Time Split, number for Average Split and Size Split)"),
 
 
 
47
  ],
48
  outputs=gr.Files(label="Download Split Videos"),
49
  title="Video Splitter",
50
+ description="Split your video file into multiple parts using different methods.",
51
  )
52
 
53
  if __name__ == "__main__":