dogdj / app.py
kijeoung's picture
Update app.py
66e897d verified
raw
history blame
3.62 kB
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()