import os import gradio as gr from moviepy.editor import VideoFileClip 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 "Error: No video file was uploaded." modeltarget = "mp4_to_mp3_conversion" save_path = os.path.join("/home/user/app", modeltarget) os.makedirs(save_path, exist_ok=True) # 비디오 파일 경로가 올바른지 확인 후 변환 try: mp3_file_path = convert_mp4_to_mp3(video_file.name, save_path) return mp3_file_path except Exception as e: return f"Error occurred during conversion: {str(e)}" # Gradio 인터페이스 설정 iface = gr.Interface( fn=mp4_to_mp3_converter, inputs=gr.File(label="Input Video"), outputs="text", # 오류 메시지 또는 파일 경로 반환 title="MP4 to MP3 Converter", description="Upload a video file to convert it to MP3 format." ) if __name__ == "__main__": iface.launch()