Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -69,24 +69,46 @@ def start_cleanup_worker(interval=3600):
|
|
| 69 |
def convert_to_wav(audio_path):
|
| 70 |
if not os.path.isfile(audio_path):
|
| 71 |
return None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 72 |
file_name = os.path.splitext(os.path.basename(audio_path))[0]
|
| 73 |
clean_name = re.sub(r'[^a-zA-Z0-9]+', '_', file_name)
|
| 74 |
clean_name = re.sub(r'_+', '_', clean_name).strip('_')
|
|
|
|
|
|
|
| 75 |
wav_path = os.path.join(
|
| 76 |
os.path.dirname(audio_path),
|
| 77 |
f"{clean_name}_tmp.wav"
|
| 78 |
)
|
|
|
|
| 79 |
try:
|
| 80 |
subprocess.run(
|
| 81 |
-
[
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 82 |
stdout=subprocess.DEVNULL,
|
| 83 |
stderr=subprocess.DEVNULL,
|
| 84 |
check=True
|
| 85 |
)
|
|
|
|
| 86 |
if os.path.isfile(wav_path) and os.path.getsize(wav_path) > 0:
|
| 87 |
return wav_path
|
| 88 |
-
|
|
|
|
| 89 |
pass
|
|
|
|
| 90 |
return None
|
| 91 |
|
| 92 |
|
|
|
|
| 69 |
def convert_to_wav(audio_path):
|
| 70 |
if not os.path.isfile(audio_path):
|
| 71 |
return None
|
| 72 |
+
|
| 73 |
+
# Get extension
|
| 74 |
+
ext = os.path.splitext(audio_path)[1].lower()
|
| 75 |
+
|
| 76 |
+
# If already mp3 or wav, return original file
|
| 77 |
+
if ext in [".mp3", ".wav"]:
|
| 78 |
+
return audio_path
|
| 79 |
+
|
| 80 |
+
# Clean filename
|
| 81 |
file_name = os.path.splitext(os.path.basename(audio_path))[0]
|
| 82 |
clean_name = re.sub(r'[^a-zA-Z0-9]+', '_', file_name)
|
| 83 |
clean_name = re.sub(r'_+', '_', clean_name).strip('_')
|
| 84 |
+
|
| 85 |
+
# Output wav path
|
| 86 |
wav_path = os.path.join(
|
| 87 |
os.path.dirname(audio_path),
|
| 88 |
f"{clean_name}_tmp.wav"
|
| 89 |
)
|
| 90 |
+
|
| 91 |
try:
|
| 92 |
subprocess.run(
|
| 93 |
+
[
|
| 94 |
+
"ffmpeg",
|
| 95 |
+
"-y",
|
| 96 |
+
"-i", audio_path,
|
| 97 |
+
"-ar", "16000",
|
| 98 |
+
"-ac", "1",
|
| 99 |
+
wav_path
|
| 100 |
+
],
|
| 101 |
stdout=subprocess.DEVNULL,
|
| 102 |
stderr=subprocess.DEVNULL,
|
| 103 |
check=True
|
| 104 |
)
|
| 105 |
+
|
| 106 |
if os.path.isfile(wav_path) and os.path.getsize(wav_path) > 0:
|
| 107 |
return wav_path
|
| 108 |
+
|
| 109 |
+
except Exception:
|
| 110 |
pass
|
| 111 |
+
|
| 112 |
return None
|
| 113 |
|
| 114 |
|