File size: 3,616 Bytes
4c59373 ddce0c5 4c59373 66e897d 8750e2b 66e897d 8750e2b ddce0c5 7d035a6 ddce0c5 8750e2b 4c59373 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
import gradio as gr
import os
import sys
# Ensure moviepy is properly imported
try:
sys.path.append("/usr/local/lib/python3.10/site-packages")
import moviepy.editor as mp
except ModuleNotFoundError:
print("moviepy ν¨ν€μ§κ° μ€μΉλμ΄ μμ§ μμ΅λλ€. μ€μΉλ₯Ό μ§ν μ€μ
λλ€...")
os.system(f"{sys.executable} -m pip install moviepy")
sys.path.append("/usr/local/lib/python3.10/site-packages")
try:
import moviepy.editor as mp
except ModuleNotFoundError:
print("moviepy λͺ¨λμ λ‘λν μ μμ΅λλ€. μ€μΉ νμΈ ν λ€μ μλνμΈμ.")
sys.exit(1)
def process_video(video, start_time, end_time, resolution, fps, speed, loop):
try:
# Load video
clip = mp.VideoFileClip(video.name)
# Get video info
duration = clip.duration
width, height = clip.size
info = {
"duration": duration,
"resolution": f"{width}x{height}"
}
# Cut clip based on input times
start_time = float(start_time) if start_time else 0
end_time = float(end_time) if end_time else duration
clip = clip.subclip(start_time, end_time)
# Create thumbnails
start_thumbnail = clip.get_frame(start_time) if start_time < duration else None
end_thumbnail = clip.get_frame(end_time) if end_time <= duration else None
# Adjust resolution, fps, speed, and loop count
if resolution:
width, height = map(int, resolution.split('x'))
clip = clip.resize((width, height))
if fps:
clip = clip.set_fps(int(fps))
if speed:
clip = clip.fx(mp.vfx.speedx, float(speed))
gif_path = f"output.gif"
clip.write_gif(gif_path, loop=int(loop))
return start_thumbnail, end_thumbnail, gif_path, info
except Exception as e:
return None, None, None, {"error": str(e)}
def main():
with gr.Blocks() as app:
gr.Markdown("## λμμμ GIFλ‘ λ³ννλ λꡬ")
with gr.Row():
video_input = gr.Video(label="λμμ μ
λ‘λ")
video_info = gr.JSON(label="μμ μ 보")
with gr.Row():
start_time = gr.Textbox(label="μμ μκ° (μ΄)", placeholder="μ: 0")
end_time = gr.Textbox(label="λ μκ° (μ΄)", placeholder="μ: 10")
with gr.Row():
resolution = gr.Textbox(label="ν΄μλ (μ: 640x480)", placeholder="μ: 640x480")
fps = gr.Slider(label="νλ μ μλ (FPS)", minimum=1, maximum=60, step=1, value=24)
speed = gr.Slider(label="μ¬μ μλ (λ°°μ)", minimum=0.1, maximum=3.0, step=0.1, value=1.0)
loop = gr.Slider(label="GIF λ°λ³΅ νμ", minimum=0, maximum=10, step=1, value=0)
with gr.Row():
start_thumbnail = gr.Image(label="μμ μΈλ€μΌ")
end_thumbnail = gr.Image(label="λ μΈλ€μΌ")
gif_preview = gr.Image(label="GIF 미리보기")
gif_download = gr.File(label="GIF λ€μ΄λ‘λ")
generate_button = gr.Button("GIF μμ±")
def generate(video, start, end, res, frame_rate, playback_speed, repeat):
return process_video(video, start, end, res, frame_rate, playback_speed, repeat)
generate_button.click(
generate,
inputs=[video_input, start_time, end_time, resolution, fps, speed, loop],
outputs=[start_thumbnail, end_thumbnail, gif_preview, gif_download]
)
return app
if __name__ == "__main__":
app = main()
app.launch()
|