jpjp9292 commited on
Commit
7d8a534
β€’
1 Parent(s): 7241525

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +11 -61
app.py CHANGED
@@ -1,52 +1,3 @@
1
- # import os
2
- # import gradio as gr
3
- # from moviepy.editor import VideoFileClip
4
- # from datetime import datetime
5
-
6
- # def convert_mp4_to_mp3(video_file_path, output_dir):
7
- # # MP3 λ³€ν™˜ κ³Όμ •
8
- # video = VideoFileClip(video_file_path)
9
- # audio = video.audio
10
- # output_path = os.path.join(output_dir, os.path.splitext(os.path.basename(video_file_path))[0] + ".mp3")
11
- # audio.write_audiofile(output_path)
12
- # audio.close()
13
- # video.close()
14
- # return output_path
15
-
16
- # def mp4_to_mp3_converter(video_file):
17
- # # μ—…λ‘œλ“œ μ‹œκ°„ ν‘œμ‹œ
18
- # upload_time = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
19
- # print(f"File uploaded at: {upload_time}")
20
-
21
- # # λΉ„λ””μ˜€ 파일이 없을 λ•Œ 처리
22
- # if video_file is None:
23
- # return "Error: No video file was uploaded."
24
-
25
- # # μ €μž₯ 경둜 μ„€μ •
26
- # modeltarget = "mp4_to_mp3_conversion"
27
- # save_path = os.path.join("/home/user/app", modeltarget)
28
- # os.makedirs(save_path, exist_ok=True)
29
-
30
- # # MP3 λ³€ν™˜ ν›„ 파일 경둜 λ°˜ν™˜
31
- # try:
32
- # mp3_file_path = convert_mp4_to_mp3(video_file.name, save_path)
33
- # return mp3_file_path # λ‹€μš΄λ‘œλ“œλ₯Ό μœ„ν•œ 파일 경둜 λ°˜ν™˜
34
- # except Exception as e:
35
- # return f"Error occurred during conversion: {str(e)}"
36
-
37
- # # Gradio μΈν„°νŽ˜μ΄μŠ€ μ„€μ •
38
- # iface = gr.Interface(
39
- # fn=mp4_to_mp3_converter,
40
- # inputs=gr.File(label="Input Video"),
41
- # outputs=gr.File(label="Download MP3"), # λ‹€μš΄λ‘œλ“œ κ°€λŠ₯ν•œ 파일 좜λ ₯
42
- # title="MP4 to MP3 Converter",
43
- # description="Upload a video file to convert it to MP3 format. Upload time will be displayed in the console."
44
- # )
45
-
46
- # if __name__ == "__main__":
47
- # iface.launch()
48
-
49
-
50
  import os
51
  import gradio as gr
52
  from moviepy.editor import VideoFileClip
@@ -66,40 +17,39 @@ def convert_mp4_to_mp3(video_file_path, output_dir):
66
  def mp4_to_mp3_converter(video_file):
67
  # λΉ„λ””μ˜€ 파일이 없을 λ•Œ 처리
68
  if video_file is None:
69
- return "였λ₯˜: μ—…λ‘œλ“œλœ λΉ„λ””μ˜€ 파일이 μ—†μŠ΅λ‹ˆλ‹€."
70
-
71
  # 파일 ν™•μž₯자 체크
72
  allowed_extensions = ['mp4', 'webm', 'avi', 'mov', 'mkv']
73
  file_extension = os.path.splitext(video_file.name)[1][1:].lower()
74
 
75
  if file_extension not in allowed_extensions:
76
- return f"였λ₯˜: μ§€μ›λ˜μ§€ μ•ŠλŠ” 파일 ν˜•μ‹μž…λ‹ˆλ‹€. λ‹€μŒ ν™•μž₯자 쀑 ν•˜λ‚˜μ˜ νŒŒμΌμ„ μ—…λ‘œλ“œν•˜μ„Έμš”: {', '.join(allowed_extensions)}"
77
 
78
  # μ €μž₯ 경둜 μ„€μ •
79
  modeltarget = "mp4_to_mp3_conversion"
80
  save_path = os.path.join("/home/user/app", modeltarget)
81
  os.makedirs(save_path, exist_ok=True)
82
 
83
- # MP3 λ³€ν™˜ ν›„ 파일 경둜 λ°˜ν™˜
84
  try:
85
  mp3_file_path = convert_mp4_to_mp3(video_file.name, save_path)
86
- return mp3_file_path # λ‹€μš΄λ‘œλ“œλ₯Ό μœ„ν•œ 파일 경둜 λ°˜ν™˜
87
  except Exception as e:
88
- return f"λ³€ν™˜ 쀑 였λ₯˜κ°€ λ°œμƒν–ˆμŠ΅λ‹ˆλ‹€: {str(e)}"
89
-
90
-
91
-
92
 
93
  # Gradio μΈν„°νŽ˜μ΄μŠ€ μ„€μ •
94
  iface = gr.Interface(
95
  fn=mp4_to_mp3_converter,
96
-
97
  inputs=gr.File(label="λΉ„λ””μ˜€ 파일 μ—…λ‘œλ“œ"),
98
- outputs=[gr.Markdown(), gr.File(label="λ‹€μš΄λ‘œλ“œ MP3")], # μƒνƒœ λ©”μ‹œμ§€μ™€ λ‹€μš΄λ‘œλ“œ κ°€λŠ₯ν•œ 파일 좜λ ₯
 
 
 
99
  title="λΉ„λ””μ˜€μ—μ„œ μŒμ„± 파일둜 λ³€ν™˜κΈ°",
100
  description="λΉ„λ””μ˜€ νŒŒμΌμ„ MP3 ν˜•μ‹μœΌλ‘œ λ³€ν™˜ν•©λ‹ˆλ‹€.",
101
  allow_flagging=False,
102
  )
103
 
104
  if __name__ == "__main__":
105
- iface.launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import os
2
  import gradio as gr
3
  from moviepy.editor import VideoFileClip
 
17
  def mp4_to_mp3_converter(video_file):
18
  # λΉ„λ””μ˜€ 파일이 없을 λ•Œ 처리
19
  if video_file is None:
20
+ return "였λ₯˜: μ—…λ‘œλ“œλœ λΉ„λ””μ˜€ 파일이 μ—†μŠ΅λ‹ˆλ‹€.", None
21
+
22
  # 파일 ν™•μž₯자 체크
23
  allowed_extensions = ['mp4', 'webm', 'avi', 'mov', 'mkv']
24
  file_extension = os.path.splitext(video_file.name)[1][1:].lower()
25
 
26
  if file_extension not in allowed_extensions:
27
+ return f"였λ₯˜: μ§€μ›λ˜μ§€ μ•ŠλŠ” 파일 ν˜•μ‹μž…λ‹ˆλ‹€. λ‹€μŒ ν™•μž₯자 쀑 ν•˜λ‚˜μ˜ νŒŒμΌμ„ μ—…λ‘œλ“œν•˜μ„Έμš”: {', '.join(allowed_extensions)}", None
28
 
29
  # μ €μž₯ 경둜 μ„€μ •
30
  modeltarget = "mp4_to_mp3_conversion"
31
  save_path = os.path.join("/home/user/app", modeltarget)
32
  os.makedirs(save_path, exist_ok=True)
33
 
34
+ # MP3 λ³€ν™˜ 및 κ²°κ³Ό λ°˜ν™˜
35
  try:
36
  mp3_file_path = convert_mp4_to_mp3(video_file.name, save_path)
37
+ return f"λ³€ν™˜ μ™„λ£Œ! MP3 파일이 μƒμ„±λ˜μ—ˆμŠ΅λ‹ˆλ‹€.", mp3_file_path
38
  except Exception as e:
39
+ return f"λ³€ν™˜ 쀑 였λ₯˜κ°€ λ°œμƒν–ˆμŠ΅λ‹ˆλ‹€: {str(e)}", None
 
 
 
40
 
41
  # Gradio μΈν„°νŽ˜μ΄μŠ€ μ„€μ •
42
  iface = gr.Interface(
43
  fn=mp4_to_mp3_converter,
 
44
  inputs=gr.File(label="λΉ„λ””μ˜€ 파일 μ—…λ‘œλ“œ"),
45
+ outputs=[
46
+ gr.Markdown(label="μƒνƒœ"),
47
+ gr.File(label="λ‹€μš΄λ‘œλ“œ MP3")
48
+ ],
49
  title="λΉ„λ””μ˜€μ—μ„œ μŒμ„± 파일둜 λ³€ν™˜κΈ°",
50
  description="λΉ„λ””μ˜€ νŒŒμΌμ„ MP3 ν˜•μ‹μœΌλ‘œ λ³€ν™˜ν•©λ‹ˆλ‹€.",
51
  allow_flagging=False,
52
  )
53
 
54
  if __name__ == "__main__":
55
+ iface.launch()