Spaces:
Running
Running
File size: 3,300 Bytes
fbd53b0 2b0570f fbd53b0 2b0570f fbd53b0 2b0570f fbd53b0 2b0570f fbd53b0 2b0570f fbd53b0 2b0570f |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 |
import os
import streamlit as st
from moviepy.editor import VideoFileClip
from datetime import datetime
# MP4 -> MP3 λ³ν ν¨μ
def convert_mp4_to_mp3(video_file_path, output_dir):
video = VideoFileClip(video_file_path)
audio = video.audio
# νμ¬ μκ° κΈ°λ°μΌλ‘ νμΌλͺ
μμ±
timestamp = datetime.now().strftime("%Y-%m-%d_%H-%M-%S")
output_path = os.path.join(output_dir, f"video_{timestamp}.mp3")
audio.write_audiofile(output_path)
audio.close()
video.close()
return output_path
# Streamlit μ± μΈν°νμ΄μ€
def main():
st.title("MP4 to MP3 Converter")
st.write("Upload an MP4 video file to convert it to MP3 format.")
# νμΌ μ
λ‘λ μμ ―
uploaded_file = st.file_uploader("Choose an MP4 file", type=["mp4"])
if uploaded_file is not None:
# λ³ν λ° μ μ₯ κ²½λ‘ μ€μ
modeltarget = "mp4_to_mp3_conversion"
save_path = os.path.join("/home/user/app", modeltarget)
os.makedirs(save_path, exist_ok=True)
# μ
λ‘λλ νμΌμ μμ νμΌλ‘ μ μ₯
video_file_path = os.path.join(save_path, uploaded_file.name)
with open(video_file_path, "wb") as f:
f.write(uploaded_file.getbuffer())
# MP4 -> MP3 λ³ν
try:
mp3_file_path = convert_mp4_to_mp3(video_file_path, save_path)
st.success("Conversion successful!")
# MP3 νμΌ λ€μ΄λ‘λ λ§ν¬ μμ±
with open(mp3_file_path, "rb") as mp3_file:
st.download_button(
label="Download MP3",
data=mp3_file,
file_name=os.path.basename(mp3_file_path),
mime="audio/mpeg"
)
except Exception as e:
st.error(f"Error occurred during conversion: {str(e)}")
if __name__ == "__main__":
main()
# 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()
|