jpjp9292 commited on
Commit
d579a03
Β·
verified Β·
1 Parent(s): 4bace7d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +37 -10
app.py CHANGED
@@ -63,37 +63,64 @@ def convert_mp4_to_mp3(video_file_path, output_dir):
63
  video.close()
64
  return output_path
65
 
66
- def mp4_to_mp3_converter(video_file):
67
- # μ—…λ‘œλ“œ μ‹œκ°„ ν‘œμ‹œ
68
- upload_time = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
69
- print(f"File uploaded at: {upload_time}")
 
 
 
 
 
 
 
 
 
70
 
 
 
 
 
 
 
 
 
 
 
 
 
71
  # λΉ„λ””μ˜€ 파일이 없을 λ•Œ 처리
72
  if video_file is None:
73
  return "Error: No video file was uploaded."
 
 
 
 
 
 
 
74
 
75
  # μ €μž₯ 경둜 μ„€μ •
76
  modeltarget = "mp4_to_mp3_conversion"
77
  save_path = os.path.join("/home/user/app", modeltarget)
78
  os.makedirs(save_path, exist_ok=True)
79
 
80
- # μ‚¬μš©μžμ—κ²Œ μ—…λ‘œλ“œ 쀑 λ©”μ‹œμ§€ λ°˜ν™˜
81
- upload_message = "μ—…λ‘œλ“œμ€‘μž…λ‹ˆλ‹€. μž μ‹œλ§Œ κΈ°λ‹€λ €μ£Όμ„Έμš”."
82
-
83
  # MP3 λ³€ν™˜ ν›„ 파일 경둜 λ°˜ν™˜
84
  try:
85
- time.sleep(1) # μž μ‹œ λŒ€κΈ°
86
  mp3_file_path = convert_mp4_to_mp3(video_file.name, save_path)
87
- return upload_message, mp3_file_path # λ‹€μš΄λ‘œλ“œλ₯Ό μœ„ν•œ 파일 경둜 λ°˜ν™˜
88
  except Exception as e:
89
  return f"Error occurred during conversion: {str(e)}"
90
 
 
 
 
91
  # Gradio μΈν„°νŽ˜μ΄μŠ€ μ„€μ •
92
  iface = gr.Interface(
93
  fn=mp4_to_mp3_converter,
94
  inputs=gr.File(label="λΉ„λ””μ˜€ 파일 μ—…λ‘œλ“œ"),
95
  outputs=[gr.Markdown(), gr.File(label="λ‹€μš΄λ‘œλ“œ MP3")], # μƒνƒœ λ©”μ‹œμ§€μ™€ λ‹€μš΄λ‘œλ“œ κ°€λŠ₯ν•œ 파일 좜λ ₯
96
- title="MP4μ—μ„œ MP3둜 λ³€ν™˜κΈ°",
97
  description="λΉ„λ””μ˜€ νŒŒμΌμ„ μ—…λ‘œλ“œν•˜μ—¬ MP3 ν˜•μ‹μœΌλ‘œ λ³€ν™˜ν•©λ‹ˆλ‹€. μ—…λ‘œλ“œ μ‹œκ°„μ€ μ½˜μ†”μ— ν‘œμ‹œλ©λ‹ˆλ‹€.",
98
  allow_flagging=False,
99
  )
 
63
  video.close()
64
  return output_path
65
 
66
+ # def mp4_to_mp3_converter(video_file):
67
+ # # μ—…λ‘œλ“œ μ‹œκ°„ ν‘œμ‹œ
68
+ # upload_time = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
69
+ # print(f"File uploaded at: {upload_time}")
70
+
71
+ # # λΉ„λ””μ˜€ 파일이 없을 λ•Œ 처리
72
+ # if video_file is None:
73
+ # return "Error: No video file was uploaded."
74
+
75
+ # # μ €μž₯ 경둜 μ„€μ •
76
+ # modeltarget = "mp4_to_mp3_conversion"
77
+ # save_path = os.path.join("/home/user/app", modeltarget)
78
+ # os.makedirs(save_path, exist_ok=True)
79
 
80
+ # # μ‚¬μš©μžμ—κ²Œ μ—…λ‘œλ“œ 쀑 λ©”μ‹œμ§€ λ°˜ν™˜
81
+ # upload_message = "μ™„λ£Œλ˜μ—ˆμŠ΅λ‹ˆλ‹€. λ‹€μš΄λ°›μ•„μ„œ ν™•μΈν•΄μ£Όμ„Έμš”."
82
+
83
+ # # MP3 λ³€ν™˜ ν›„ 파일 경둜 λ°˜ν™˜
84
+ # try:
85
+ # time.sleep(1) # μž μ‹œ λŒ€κΈ°
86
+ # mp3_file_path = convert_mp4_to_mp3(video_file.name, save_path)
87
+ # return upload_message, mp3_file_path # λ‹€μš΄λ‘œλ“œλ₯Ό μœ„ν•œ 파일 경둜 λ°˜ν™˜
88
+ # except Exception as e:
89
+ # return f"Error occurred during conversion: {str(e)}"
90
+
91
+ def mp4_to_mp3_converter(video_file):
92
  # λΉ„λ””μ˜€ 파일이 없을 λ•Œ 처리
93
  if video_file is None:
94
  return "Error: No video file was uploaded."
95
+
96
+ # 파일 ν™•μž₯자 체크
97
+ allowed_extensions = ['mp4', 'webm', 'avi', 'mov', 'mkv']
98
+ file_extension = os.path.splitext(video_file.name)[1][1:].lower()
99
+
100
+ if file_extension not in allowed_extensions:
101
+ return f"Error: Unsupported file format. Please upload a file with one of the following extensions: {', '.join(allowed_extensions)}"
102
 
103
  # μ €μž₯ 경둜 μ„€μ •
104
  modeltarget = "mp4_to_mp3_conversion"
105
  save_path = os.path.join("/home/user/app", modeltarget)
106
  os.makedirs(save_path, exist_ok=True)
107
 
 
 
 
108
  # MP3 λ³€ν™˜ ν›„ 파일 경둜 λ°˜ν™˜
109
  try:
 
110
  mp3_file_path = convert_mp4_to_mp3(video_file.name, save_path)
111
+ return mp3_file_path # λ‹€μš΄λ‘œλ“œλ₯Ό μœ„ν•œ 파일 경둜 λ°˜ν™˜
112
  except Exception as e:
113
  return f"Error occurred during conversion: {str(e)}"
114
 
115
+
116
+
117
+
118
  # Gradio μΈν„°νŽ˜μ΄μŠ€ μ„€μ •
119
  iface = gr.Interface(
120
  fn=mp4_to_mp3_converter,
121
  inputs=gr.File(label="λΉ„λ””μ˜€ 파일 μ—…λ‘œλ“œ"),
122
  outputs=[gr.Markdown(), gr.File(label="λ‹€μš΄λ‘œλ“œ MP3")], # μƒνƒœ λ©”μ‹œμ§€μ™€ λ‹€μš΄λ‘œλ“œ κ°€λŠ₯ν•œ 파일 좜λ ₯
123
+ title="λΉ„λ””μ˜€μ—μ„œ μŒμ„± 파일둜 λ³€ν™˜κΈ°",
124
  description="λΉ„λ””μ˜€ νŒŒμΌμ„ μ—…λ‘œλ“œν•˜μ—¬ MP3 ν˜•μ‹μœΌλ‘œ λ³€ν™˜ν•©λ‹ˆλ‹€. μ—…λ‘œλ“œ μ‹œκ°„μ€ μ½˜μ†”μ— ν‘œμ‹œλ©λ‹ˆλ‹€.",
125
  allow_flagging=False,
126
  )