Spaces:
Sleeping
Sleeping
Update app/audio_utils.py
Browse files- app/audio_utils.py +95 -114
app/audio_utils.py
CHANGED
|
@@ -1,114 +1,95 @@
|
|
| 1 |
-
from __future__ import annotations
|
| 2 |
-
|
| 3 |
-
import base64
|
| 4 |
-
import os
|
| 5 |
-
import shutil
|
| 6 |
-
import subprocess
|
| 7 |
-
from dataclasses import dataclass
|
| 8 |
-
from typing import List
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
def
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
"-acodec", "copy",
|
| 97 |
-
chunk_path,
|
| 98 |
-
]
|
| 99 |
-
proc = subprocess.run(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)
|
| 100 |
-
if proc.returncode != 0:
|
| 101 |
-
raise RuntimeError(f"切分音频失败:\n{proc.stderr}")
|
| 102 |
-
|
| 103 |
-
chunks.append(AudioChunk(path=chunk_path, start_sec=start, end_sec=end))
|
| 104 |
-
start = end
|
| 105 |
-
idx += 1
|
| 106 |
-
|
| 107 |
-
return chunks
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
def audio_file_to_data_uri(audio_path: str, mime_type: str = "audio/mpeg") -> str:
|
| 111 |
-
with open(audio_path, "rb") as f:
|
| 112 |
-
b64 = base64.b64encode(f.read()).decode("utf-8")
|
| 113 |
-
return f"data:{mime_type};base64,{b64}"
|
| 114 |
-
|
|
|
|
| 1 |
+
from __future__ import annotations
|
| 2 |
+
|
| 3 |
+
import base64
|
| 4 |
+
import os
|
| 5 |
+
import shutil
|
| 6 |
+
import subprocess
|
| 7 |
+
from dataclasses import dataclass
|
| 8 |
+
from typing import List
|
| 9 |
+
|
| 10 |
+
@dataclass
|
| 11 |
+
class AudioChunk:
|
| 12 |
+
path: str
|
| 13 |
+
start_sec: float
|
| 14 |
+
end_sec: float
|
| 15 |
+
|
| 16 |
+
def check_ffmpeg_available() -> bool:
|
| 17 |
+
return shutil.which("ffmpeg") is not None and shutil.which("ffprobe") is not None
|
| 18 |
+
|
| 19 |
+
def ensure_dir(path: str) -> None:
|
| 20 |
+
os.makedirs(path, exist_ok=True)
|
| 21 |
+
|
| 22 |
+
def get_media_duration(path: str) -> float:
|
| 23 |
+
cmd = [
|
| 24 |
+
"ffprobe",
|
| 25 |
+
"-v", "error",
|
| 26 |
+
"-show_entries", "format=duration",
|
| 27 |
+
"-of", "default=noprint_wrappers=1:nokey=1",
|
| 28 |
+
path,
|
| 29 |
+
]
|
| 30 |
+
proc = subprocess.run(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)
|
| 31 |
+
# if proc.returncode != 0:
|
| 32 |
+
# print(f"获取时长失败: {proc.stderr}")
|
| 33 |
+
return float(proc.stdout.strip())
|
| 34 |
+
|
| 35 |
+
def extract_audio_from_video(
|
| 36 |
+
video_path: str,
|
| 37 |
+
output_audio_path: str,
|
| 38 |
+
bitrate: str = "64k",
|
| 39 |
+
) -> str:
|
| 40 |
+
ensure_dir(os.path.dirname(output_audio_path))
|
| 41 |
+
|
| 42 |
+
cmd = [
|
| 43 |
+
"ffmpeg",
|
| 44 |
+
"-y",
|
| 45 |
+
"-i", video_path,
|
| 46 |
+
"-vn",
|
| 47 |
+
"-ac", "1",
|
| 48 |
+
"-ar", "16000",
|
| 49 |
+
"-c:a", "mp3",
|
| 50 |
+
"-b:a", bitrate,
|
| 51 |
+
output_audio_path,
|
| 52 |
+
]
|
| 53 |
+
proc = subprocess.run(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)
|
| 54 |
+
|
| 55 |
+
return output_audio_path
|
| 56 |
+
|
| 57 |
+
def split_audio_to_chunks(
|
| 58 |
+
audio_path: str,
|
| 59 |
+
output_dir: str,
|
| 60 |
+
chunk_seconds: int = 290,
|
| 61 |
+
) -> List[AudioChunk]:
|
| 62 |
+
ensure_dir(output_dir)
|
| 63 |
+
duration = get_media_duration(audio_path)
|
| 64 |
+
chunks: List[AudioChunk] = []
|
| 65 |
+
|
| 66 |
+
start = 0.0
|
| 67 |
+
idx = 0
|
| 68 |
+
while start < duration:
|
| 69 |
+
end = min(duration, start + chunk_seconds)
|
| 70 |
+
chunk_path = os.path.join(output_dir, f"audio_chunk_{idx:03d}.mp3")
|
| 71 |
+
|
| 72 |
+
cmd = [
|
| 73 |
+
"ffmpeg",
|
| 74 |
+
"-y",
|
| 75 |
+
"-i", audio_path,
|
| 76 |
+
"-ss", str(start),
|
| 77 |
+
"-t", str(end - start),
|
| 78 |
+
"-acodec", "copy",
|
| 79 |
+
chunk_path,
|
| 80 |
+
]
|
| 81 |
+
proc = subprocess.run(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)
|
| 82 |
+
if proc.returncode != 0:
|
| 83 |
+
raise RuntimeError(f"切分音频失败:\n{proc.stderr}")
|
| 84 |
+
|
| 85 |
+
chunks.append(AudioChunk(path=chunk_path, start_sec=start, end_sec=end))
|
| 86 |
+
start = end
|
| 87 |
+
idx += 1
|
| 88 |
+
|
| 89 |
+
return chunks
|
| 90 |
+
|
| 91 |
+
def audio_file_to_data_uri(audio_path: str, mime_type: str = "audio/mpeg") -> str:
|
| 92 |
+
with open(audio_path, "rb") as f:
|
| 93 |
+
b64 = base64.b64encode(f.read()).decode("utf-8")
|
| 94 |
+
return f"data:{mime_type};base64,{b64}"
|
| 95 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|