File size: 4,676 Bytes
b0c6a14
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fbd53b0
dc33463
fbd53b0
3db36f3
4bace7d
fbd53b0
 
dc33463
fbd53b0
 
dc33463
fbd53b0
 
 
 
 
d579a03
 
 
 
 
 
 
 
 
 
 
 
 
3db36f3
d579a03
 
 
 
 
 
 
 
 
 
 
 
dc33463
 
 
d579a03
 
 
 
 
 
 
dc33463
3db36f3
dc33463
 
 
 
3db36f3
dc33463
 
d579a03
dc33463
 
 
d579a03
 
 
dc33463
 
 
b0c6a14
4bace7d
d579a03
b0c6a14
 
dc33463
fbd53b0
 
dc33463
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# 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()