VideoTrimv2 / app.py
AndreySokolov01's picture
Update app.py
48d0e40 verified
import os
import moviepy.video.io.VideoFileClip as mp
import gradio as gr
from concurrent.futures import ThreadPoolExecutor
def split_video(video_file, parts, resolution, quality, audio_enabled, volume):
video = mp.VideoFileClip(video_file.name)
duration = video.duration
part_duration = duration / parts
output_files = []
with ThreadPoolExecutor() as executor:
futures = []
for part in range(parts):
start_time = part * part_duration
end_time = (part + 1) * part_duration
futures.append(executor.submit(process_part, video, start_time, end_time, resolution, quality, audio_enabled, volume, part))
for future in futures:
output_filename = future.result()
output_files.append(output_filename)
return output_files
def process_part(video, start_time, end_time, resolution, quality, audio_enabled, volume, part):
part_clip = video.subclip(start_time, end_time)
# Настройка параметров кодирования
if quality == "Низкое":
bitrate = "1M"
elif quality == "Среднее":
bitrate = "2M"
else:
bitrate = "4M"
# Настройка параметров звука
if not audio_enabled:
part_clip.audio = None
else:
part_clip.audio = part_clip.audio.volumex(volume)
# Сохранение видео
output_filename = f"part_{part + 1}.mp4"
part_clip.write_videofile(
output_filename,
codec="libx264",
audio_codec="aac",
bitrate=bitrate,
preset="medium",
ffmpeg_params=["-vf", f"scale={resolution}"],
)
return output_filename
iface = gr.Interface(
fn=split_video,
inputs=[
gr.File(label="Upload Video"),
gr.Slider(minimum=2, maximum=10, value=2, label="Number of Parts"),
gr.Dropdown(choices=["720p", "1080p", "4K"], label="Resolution"),
gr.Dropdown(choices=["Низкое", "Среднее", "Высокое"], label="Качество"),
gr.Checkbox(label="Включить звук"),
gr.Slider(minimum=0, maximum=2, value=1, label="Громкость"),
],
outputs=gr.Files(label="Download Split Videos"),
title="Video Splitter",
description="Upload your video and select how many parts you want to split it into."
)
if __name__ == "__main__":
iface.launch()