Video_to_Audio / app.py
jpjp9292's picture
Update app.py
b0c6a14 verified
raw
history blame
3.6 kB
# 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
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="λΉ„λ””μ˜€ 파일 μ—…λ‘œλ“œ"),
outputs=gr.File(label="λ‹€μš΄λ‘œλ“œ MP3"),
title="MP4μ—μ„œ MP3둜 λ³€ν™˜κΈ°",
description="λΉ„λ””μ˜€ νŒŒμΌμ„ μ—…λ‘œλ“œν•˜μ—¬ MP3 ν˜•μ‹μœΌλ‘œ λ³€ν™˜ν•©λ‹ˆλ‹€. μ—…λ‘œλ“œ μ‹œκ°„μ€ μ½˜μ†”μ— ν‘œμ‹œλ©λ‹ˆλ‹€.",
allow_flagging=False,
)
# JavaScript μ½”λ“œ 직접 μ‚½μž…
iface.load_js("""
function showAlert() {
alert("μ—…λ‘œλ“œ μ€‘μž…λ‹ˆλ‹€. μž μ‹œλ§Œ κΈ°λ‹€λ €μ£Όμ„Έμš”.");
}
document.querySelector("input[type='file']").addEventListener("change", showAlert);
""")
if __name__ == "__main__":
iface.launch()