Video_to_Audio / app.py
jpjp9292's picture
Update app.py
7d8a534 verified
import os
import gradio as gr
from moviepy.editor import VideoFileClip
from datetime import datetime
import time
def convert_mp4_to_mp3(video_file_path, output_dir):
# MP3 λ³€ν™˜ κ³Όμ •
video = VideoFileClip(video_file_path)
audio = video.audio
output_path = os.path.join(output_dir, os.path.splitext(os.path.basename(video_file_path))[0] + ".mp3")
audio.write_audiofile(output_path)
audio.close()
video.close()
return output_path
def mp4_to_mp3_converter(video_file):
# λΉ„λ””μ˜€ 파일이 없을 λ•Œ 처리
if video_file is None:
return "였λ₯˜: μ—…λ‘œλ“œλœ λΉ„λ””μ˜€ 파일이 μ—†μŠ΅λ‹ˆλ‹€.", None
# 파일 ν™•μž₯자 체크
allowed_extensions = ['mp4', 'webm', 'avi', 'mov', 'mkv']
file_extension = os.path.splitext(video_file.name)[1][1:].lower()
if file_extension not in allowed_extensions:
return f"였λ₯˜: μ§€μ›λ˜μ§€ μ•ŠλŠ” 파일 ν˜•μ‹μž…λ‹ˆλ‹€. λ‹€μŒ ν™•μž₯자 쀑 ν•˜λ‚˜μ˜ νŒŒμΌμ„ μ—…λ‘œλ“œν•˜μ„Έμš”: {', '.join(allowed_extensions)}", None
# μ €μž₯ 경둜 μ„€μ •
modeltarget = "mp4_to_mp3_conversion"
save_path = os.path.join("/home/user/app", modeltarget)
os.makedirs(save_path, exist_ok=True)
# MP3 λ³€ν™˜ 및 κ²°κ³Ό λ°˜ν™˜
try:
mp3_file_path = convert_mp4_to_mp3(video_file.name, save_path)
return f"λ³€ν™˜ μ™„λ£Œ! MP3 파일이 μƒμ„±λ˜μ—ˆμŠ΅λ‹ˆλ‹€.", mp3_file_path
except Exception as e:
return f"λ³€ν™˜ 쀑 였λ₯˜κ°€ λ°œμƒν–ˆμŠ΅λ‹ˆλ‹€: {str(e)}", None
# Gradio μΈν„°νŽ˜μ΄μŠ€ μ„€μ •
iface = gr.Interface(
fn=mp4_to_mp3_converter,
inputs=gr.File(label="λΉ„λ””μ˜€ 파일 μ—…λ‘œλ“œ"),
outputs=[
gr.Markdown(label="μƒνƒœ"),
gr.File(label="λ‹€μš΄λ‘œλ“œ MP3")
],
title="λΉ„λ””μ˜€μ—μ„œ μŒμ„± 파일둜 λ³€ν™˜κΈ°",
description="λΉ„λ””μ˜€ νŒŒμΌμ„ MP3 ν˜•μ‹μœΌλ‘œ λ³€ν™˜ν•©λ‹ˆλ‹€.",
allow_flagging=False,
)
if __name__ == "__main__":
iface.launch()