Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -6,9 +6,18 @@ import glob
|
|
| 6 |
import shutil
|
| 7 |
import gradio as gr
|
| 8 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
ffmpeg_path = shutil.which("ffmpeg")
|
| 10 |
-
fdkaac_path = os.path.abspath("./fdkaac")
|
| 11 |
yt_dlp_path = shutil.which("yt-dlp")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
|
| 13 |
if not ffmpeg_path:
|
| 14 |
raise FileNotFoundError("FFmpeg not found in PATH.")
|
|
@@ -17,7 +26,6 @@ if not yt_dlp_path:
|
|
| 17 |
if not os.path.isfile(fdkaac_path):
|
| 18 |
raise FileNotFoundError("fdkaac binary not found in current directory.")
|
| 19 |
|
| 20 |
-
os.system("chmod +x fdkaac")
|
| 21 |
accel = 'auto'
|
| 22 |
video_base_opts = ['-crf', '63', '-c:v', 'libx264', '-tune', 'zerolatency']
|
| 23 |
|
|
@@ -27,44 +35,45 @@ os.makedirs(UPLOAD_FOLDER, exist_ok=True)
|
|
| 27 |
os.makedirs(CONVERTED_FOLDER, exist_ok=True)
|
| 28 |
|
| 29 |
async def run_subprocess(cmd):
|
|
|
|
| 30 |
process = await asyncio.create_subprocess_exec(
|
| 31 |
-
*cmd,
|
|
|
|
|
|
|
|
|
|
| 32 |
)
|
| 33 |
stdout, stderr = await process.communicate()
|
| 34 |
if process.returncode != 0:
|
|
|
|
| 35 |
raise subprocess.CalledProcessError(process.returncode, cmd, stderr.decode())
|
|
|
|
| 36 |
return stdout.decode(), stderr.decode()
|
| 37 |
|
| 38 |
async def convert_video_task(input_path, downscale, faster, use_mp3):
|
| 39 |
if use_mp3:
|
| 40 |
-
# Only extract MP3 audio, no splitting
|
| 41 |
output_path = os.path.join(CONVERTED_FOLDER, f"{uuid.uuid4()}.mp3")
|
| 42 |
ffmpeg_cmd = [
|
| 43 |
-
|
| 44 |
'-c:a', 'libmp3lame', '-b:a', '8k', '-ar', '24000', '-ac', '1', output_path
|
| 45 |
]
|
| 46 |
await run_subprocess(ffmpeg_cmd)
|
| 47 |
return output_path, None
|
| 48 |
|
| 49 |
-
# Non-MP3 path: split audio and video
|
| 50 |
audio_wav = os.path.join(CONVERTED_FOLDER, f"{uuid.uuid4()}.wav")
|
| 51 |
audio_output = os.path.join(CONVERTED_FOLDER, f"{uuid.uuid4()}.m4a")
|
| 52 |
video_output = os.path.join(CONVERTED_FOLDER, f"{uuid.uuid4()}.mp4")
|
| 53 |
|
| 54 |
-
# Extract mono WAV
|
| 55 |
await run_subprocess([
|
| 56 |
-
|
| 57 |
])
|
| 58 |
|
| 59 |
-
# Compress WAV → AAC with local fdkaac
|
| 60 |
await run_subprocess([
|
| 61 |
-
|
| 62 |
-
|
| 63 |
])
|
| 64 |
|
| 65 |
-
# Compress video with extreme low quality
|
| 66 |
video_cmd = [
|
| 67 |
-
|
| 68 |
*video_base_opts, '-an', video_output
|
| 69 |
]
|
| 70 |
if faster:
|
|
|
|
| 6 |
import shutil
|
| 7 |
import gradio as gr
|
| 8 |
|
| 9 |
+
# Make sure fdkaac is executable (only needed once, but harmless)
|
| 10 |
+
os.system("chmod +x fdkaac")
|
| 11 |
+
|
| 12 |
+
# Locate binaries and check existence
|
| 13 |
ffmpeg_path = shutil.which("ffmpeg")
|
|
|
|
| 14 |
yt_dlp_path = shutil.which("yt-dlp")
|
| 15 |
+
fdkaac_path = os.path.abspath("./fdkaac")
|
| 16 |
+
|
| 17 |
+
print("[DEBUG] Detected Paths:")
|
| 18 |
+
print("FFmpeg:", ffmpeg_path)
|
| 19 |
+
print("yt-dlp:", yt_dlp_path)
|
| 20 |
+
print("fdkaac:", fdkaac_path)
|
| 21 |
|
| 22 |
if not ffmpeg_path:
|
| 23 |
raise FileNotFoundError("FFmpeg not found in PATH.")
|
|
|
|
| 26 |
if not os.path.isfile(fdkaac_path):
|
| 27 |
raise FileNotFoundError("fdkaac binary not found in current directory.")
|
| 28 |
|
|
|
|
| 29 |
accel = 'auto'
|
| 30 |
video_base_opts = ['-crf', '63', '-c:v', 'libx264', '-tune', 'zerolatency']
|
| 31 |
|
|
|
|
| 35 |
os.makedirs(CONVERTED_FOLDER, exist_ok=True)
|
| 36 |
|
| 37 |
async def run_subprocess(cmd):
|
| 38 |
+
print(f"\n[DEBUG] Running command:\n{' '.join(cmd)}\n")
|
| 39 |
process = await asyncio.create_subprocess_exec(
|
| 40 |
+
*cmd,
|
| 41 |
+
stdout=asyncio.subprocess.PIPE,
|
| 42 |
+
stderr=asyncio.subprocess.PIPE,
|
| 43 |
+
env=os.environ.copy() # Inherit full environment for safety
|
| 44 |
)
|
| 45 |
stdout, stderr = await process.communicate()
|
| 46 |
if process.returncode != 0:
|
| 47 |
+
print(f"[ERROR] Command failed:\n{stderr.decode()}\n")
|
| 48 |
raise subprocess.CalledProcessError(process.returncode, cmd, stderr.decode())
|
| 49 |
+
print(f"[DEBUG] Command succeeded:\n{stdout.decode()}\n")
|
| 50 |
return stdout.decode(), stderr.decode()
|
| 51 |
|
| 52 |
async def convert_video_task(input_path, downscale, faster, use_mp3):
|
| 53 |
if use_mp3:
|
|
|
|
| 54 |
output_path = os.path.join(CONVERTED_FOLDER, f"{uuid.uuid4()}.mp3")
|
| 55 |
ffmpeg_cmd = [
|
| 56 |
+
ffmpeg_path, '-y', '-i', input_path, '-vn',
|
| 57 |
'-c:a', 'libmp3lame', '-b:a', '8k', '-ar', '24000', '-ac', '1', output_path
|
| 58 |
]
|
| 59 |
await run_subprocess(ffmpeg_cmd)
|
| 60 |
return output_path, None
|
| 61 |
|
|
|
|
| 62 |
audio_wav = os.path.join(CONVERTED_FOLDER, f"{uuid.uuid4()}.wav")
|
| 63 |
audio_output = os.path.join(CONVERTED_FOLDER, f"{uuid.uuid4()}.m4a")
|
| 64 |
video_output = os.path.join(CONVERTED_FOLDER, f"{uuid.uuid4()}.mp4")
|
| 65 |
|
|
|
|
| 66 |
await run_subprocess([
|
| 67 |
+
ffmpeg_path, '-y', '-i', input_path, '-ac', '1', '-ar', '8000', audio_wav
|
| 68 |
])
|
| 69 |
|
|
|
|
| 70 |
await run_subprocess([
|
| 71 |
+
fdkaac_path, '-b', '1k', '-C', '-f', '2', '-G', '1', '-w', '8000',
|
| 72 |
+
'-o', audio_output, audio_wav
|
| 73 |
])
|
| 74 |
|
|
|
|
| 75 |
video_cmd = [
|
| 76 |
+
ffmpeg_path, '-y', '-hwaccel', accel, '-i', input_path,
|
| 77 |
*video_base_opts, '-an', video_output
|
| 78 |
]
|
| 79 |
if faster:
|