48 lines
1.3 KiB
Python
48 lines
1.3 KiB
Python
|
|
from __future__ import annotations
|
||
|
|
|
||
|
|
import os
|
||
|
|
import subprocess
|
||
|
|
import tempfile
|
||
|
|
|
||
|
|
|
||
|
|
VIDEO_EXTENSIONS = {".mp4", ".webm", ".mov", ".avi", ".mkv", ".m4v", ".ogv"}
|
||
|
|
|
||
|
|
|
||
|
|
def is_video_filename(filename: str = "", mime_type: str = "") -> bool:
|
||
|
|
ext = os.path.splitext(filename or "")[1].lower()
|
||
|
|
mime = (mime_type or "").strip().lower()
|
||
|
|
return ext in VIDEO_EXTENSIONS or mime.startswith("video/")
|
||
|
|
|
||
|
|
|
||
|
|
def extract_audio_wav_bytes(input_path: str) -> bytes:
|
||
|
|
if not input_path or not os.path.exists(input_path):
|
||
|
|
raise FileNotFoundError("输入媒体文件不存在")
|
||
|
|
|
||
|
|
fd, output_path = tempfile.mkstemp(suffix=".wav")
|
||
|
|
os.close(fd)
|
||
|
|
try:
|
||
|
|
subprocess.run(
|
||
|
|
[
|
||
|
|
"ffmpeg",
|
||
|
|
"-y",
|
||
|
|
"-i",
|
||
|
|
input_path,
|
||
|
|
"-vn",
|
||
|
|
"-acodec",
|
||
|
|
"pcm_s16le",
|
||
|
|
"-ar",
|
||
|
|
"16000",
|
||
|
|
"-ac",
|
||
|
|
"1",
|
||
|
|
output_path,
|
||
|
|
],
|
||
|
|
check=True,
|
||
|
|
stdout=subprocess.PIPE,
|
||
|
|
stderr=subprocess.PIPE,
|
||
|
|
)
|
||
|
|
with open(output_path, "rb") as handle:
|
||
|
|
return handle.read()
|
||
|
|
finally:
|
||
|
|
if os.path.exists(output_path):
|
||
|
|
os.unlink(output_path)
|