Spaces:
Running
Running
# import os | |
# import gradio as gr | |
# from moviepy.editor import VideoFileClip | |
# from datetime import datetime | |
# 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): | |
# # μ λ‘λ μκ° νμ | |
# upload_time = datetime.now().strftime("%Y-%m-%d %H:%M:%S") | |
# print(f"File uploaded at: {upload_time}") | |
# # λΉλμ€ νμΌμ΄ μμ λ μ²λ¦¬ | |
# 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) | |
# # MP3 λ³ν ν νμΌ κ²½λ‘ λ°ν | |
# 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=gr.File(label="Download MP3"), # λ€μ΄λ‘λ κ°λ₯ν νμΌ μΆλ ₯ | |
# title="MP4 to MP3 Converter", | |
# description="Upload a video file to convert it to MP3 format. Upload time will be displayed in the console." | |
# ) | |
# if __name__ == "__main__": | |
# iface.launch() | |
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): | |
# # μ λ‘λ μκ° νμ | |
# upload_time = datetime.now().strftime("%Y-%m-%d %H:%M:%S") | |
# print(f"File uploaded at: {upload_time}") | |
# # λΉλμ€ νμΌμ΄ μμ λ μ²λ¦¬ | |
# 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) | |
# # μ¬μ©μμκ² μ λ‘λ μ€ λ©μμ§ λ°ν | |
# upload_message = "μλ£λμμ΅λλ€. λ€μ΄λ°μμ νμΈν΄μ£ΌμΈμ." | |
# # MP3 λ³ν ν νμΌ κ²½λ‘ λ°ν | |
# try: | |
# time.sleep(1) # μ μ λκΈ° | |
# mp3_file_path = convert_mp4_to_mp3(video_file.name, save_path) | |
# return upload_message, mp3_file_path # λ€μ΄λ‘λλ₯Ό μν νμΌ κ²½λ‘ λ°ν | |
# except Exception as e: | |
# return f"Error occurred during conversion: {str(e)}" | |
def mp4_to_mp3_converter(video_file): | |
# λΉλμ€ νμΌμ΄ μμ λ μ²λ¦¬ | |
if video_file is None: | |
return "Error: No video file was uploaded." | |
# νμΌ νμ₯μ μ²΄ν¬ | |
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"Error: Unsupported file format. Please upload a file with one of the following extensions: {', '.join(allowed_extensions)}" | |
# μ μ₯ κ²½λ‘ μ€μ | |
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 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="λΉλμ€ νμΌ μ λ‘λ"), | |
outputs=[gr.Markdown(), gr.File(label="λ€μ΄λ‘λ MP3")], # μν λ©μμ§μ λ€μ΄λ‘λ κ°λ₯ν νμΌ μΆλ ₯ | |
title="λΉλμ€μμ μμ± νμΌλ‘ λ³νκΈ°", | |
description="λΉλμ€ νμΌμ μ λ‘λνμ¬ MP3 νμμΌλ‘ λ³νν©λλ€. μ λ‘λ μκ°μ μ½μμ νμλ©λλ€.", | |
allow_flagging=False, | |
) | |
if __name__ == "__main__": | |
iface.launch() | |