Update helper.py
Browse files
helper.py
CHANGED
|
@@ -91,7 +91,7 @@ def cut_video(video_path, max_duration=2):
|
|
| 91 |
# -------------------------------
|
| 92 |
# Convert video to audio safely
|
| 93 |
# -------------------------------
|
| 94 |
-
def video_to_audio(video_input, output_path=
|
| 95 |
"""
|
| 96 |
Convert video (file path, data URL, or base64) to WAV audio using MoviePy.
|
| 97 |
Handles all input formats safely and avoids oversized subprocess arguments.
|
|
@@ -100,6 +100,11 @@ def video_to_audio(video_input, output_path="/tmp/temp_reference.wav", max_durat
|
|
| 100 |
clip = None
|
| 101 |
|
| 102 |
try:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 103 |
# Detect and decode base64 or data URLs
|
| 104 |
if isinstance(video_input, str):
|
| 105 |
if video_input.startswith("data:video/"): # full data URL
|
|
@@ -137,7 +142,6 @@ def video_to_audio(video_input, output_path="/tmp/temp_reference.wav", max_durat
|
|
| 137 |
)
|
| 138 |
clip.close()
|
| 139 |
|
| 140 |
-
# Validate output
|
| 141 |
if not os.path.exists(output_path) or os.path.getsize(output_path) == 0:
|
| 142 |
raise Exception("Output audio file not created or empty")
|
| 143 |
|
|
@@ -160,7 +164,7 @@ def video_to_audio(video_input, output_path="/tmp/temp_reference.wav", max_durat
|
|
| 160 |
logger.debug(f"🧹 Deleted temp file {temp_video_path}")
|
| 161 |
except Exception as e:
|
| 162 |
logger.warning(f"⚠️ Failed to delete temp file: {e}")
|
| 163 |
-
|
| 164 |
# -------------------------------
|
| 165 |
# Ensure WAV format
|
| 166 |
# -------------------------------
|
|
|
|
| 91 |
# -------------------------------
|
| 92 |
# Convert video to audio safely
|
| 93 |
# -------------------------------
|
| 94 |
+
def video_to_audio(video_input, output_path=None, max_duration=2):
|
| 95 |
"""
|
| 96 |
Convert video (file path, data URL, or base64) to WAV audio using MoviePy.
|
| 97 |
Handles all input formats safely and avoids oversized subprocess arguments.
|
|
|
|
| 100 |
clip = None
|
| 101 |
|
| 102 |
try:
|
| 103 |
+
# ✅ Auto-generate output path if None
|
| 104 |
+
if output_path is None:
|
| 105 |
+
output_path = tempfile.NamedTemporaryFile(delete=False, suffix=".wav").name
|
| 106 |
+
logger.debug(f"Auto-created output path: {output_path}")
|
| 107 |
+
|
| 108 |
# Detect and decode base64 or data URLs
|
| 109 |
if isinstance(video_input, str):
|
| 110 |
if video_input.startswith("data:video/"): # full data URL
|
|
|
|
| 142 |
)
|
| 143 |
clip.close()
|
| 144 |
|
|
|
|
| 145 |
if not os.path.exists(output_path) or os.path.getsize(output_path) == 0:
|
| 146 |
raise Exception("Output audio file not created or empty")
|
| 147 |
|
|
|
|
| 164 |
logger.debug(f"🧹 Deleted temp file {temp_video_path}")
|
| 165 |
except Exception as e:
|
| 166 |
logger.warning(f"⚠️ Failed to delete temp file: {e}")
|
| 167 |
+
|
| 168 |
# -------------------------------
|
| 169 |
# Ensure WAV format
|
| 170 |
# -------------------------------
|