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()