Spaces:
Sleeping
Sleeping
File size: 4,948 Bytes
adec1cb | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 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 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 | #!/usr/bin/env python3
"""
检查ffmpeg安装情况
"""
import subprocess
import os
import sys
def check_ffmpeg():
"""检查ffmpeg是否可用"""
print("🔍 检查ffmpeg安装情况...")
# 方法1: 检查系统PATH中的ffmpeg
try:
result = subprocess.run(['ffmpeg', '-version'],
capture_output=True, text=True, timeout=10)
if result.returncode == 0:
print("✅ ffmpeg在系统PATH中可用")
print(f" 版本信息: {result.stdout.split('ffmpeg version')[1].split('\n')[0]}")
return True
except (subprocess.TimeoutExpired, FileNotFoundError, subprocess.CalledProcessError):
print("❌ ffmpeg不在系统PATH中")
# 方法2: 检查conda环境中的ffmpeg
try:
conda_prefix = os.environ.get('CONDA_PREFIX')
if conda_prefix:
ffmpeg_path = os.path.join(conda_prefix, 'bin', 'ffmpeg')
if os.path.exists(ffmpeg_path):
print(f"✅ 在conda环境中找到ffmpeg: {ffmpeg_path}")
return True
else:
print(f"❌ conda环境中没有ffmpeg: {ffmpeg_path}")
except Exception as e:
print(f"❌ 检查conda环境失败: {e}")
# 方法3: 检查常见的ffmpeg安装路径
common_paths = [
r"C:\ffmpeg\bin\ffmpeg.exe",
r"C:\Program Files\ffmpeg\bin\ffmpeg.exe",
r"C:\Program Files (x86)\ffmpeg\bin\ffmpeg.exe",
os.path.expanduser(r"~\ffmpeg\bin\ffmpeg.exe")
]
for path in common_paths:
if os.path.exists(path):
print(f"✅ 找到ffmpeg: {path}")
return True
print("❌ 未找到ffmpeg")
return False
def install_ffmpeg_conda():
"""通过conda安装ffmpeg"""
print("\n📦 尝试通过conda安装ffmpeg...")
try:
result = subprocess.run(['conda', 'install', '-c', 'conda-forge', 'ffmpeg', '-y'],
capture_output=True, text=True, timeout=60)
if result.returncode == 0:
print("✅ ffmpeg安装成功")
return True
else:
print(f"❌ ffmpeg安装失败: {result.stderr}")
return False
except Exception as e:
print(f"❌ conda安装失败: {e}")
return False
def install_ffmpeg_pip():
"""通过pip安装ffmpeg-python"""
print("\n📦 尝试通过pip安装ffmpeg-python...")
try:
result = subprocess.run([sys.executable, '-m', 'pip', 'install', 'ffmpeg-python'],
capture_output=True, text=True, timeout=60)
if result.returncode == 0:
print("✅ ffmpeg-python安装成功")
return True
else:
print(f"❌ ffmpeg-python安装失败: {result.stderr}")
return False
except Exception as e:
print(f"❌ pip安装失败: {e}")
return False
def test_audio_without_ffmpeg():
"""测试不使用ffmpeg的音频处理"""
print("\n🎵 测试不使用ffmpeg的音频处理...")
try:
import yt_dlp
print("✅ yt-dlp可用")
# 测试下载音频(不转换格式)
test_url = "https://www.youtube.com/watch?v=dQw4w9WgXcQ"
ydl_opts = {
'format': 'bestaudio/best',
'outtmpl': 'downloads/test_audio.%(ext)s',
'quiet': True
}
with yt_dlp.YoutubeDL(ydl_opts) as ydl:
info = ydl.extract_info(test_url, download=True)
audio_path = ydl.prepare_filename(info)
if os.path.exists(audio_path):
print(f"✅ 音频下载成功: {audio_path}")
print(f" 文件大小: {os.path.getsize(audio_path)} bytes")
return True
else:
print(f"❌ 音频下载失败")
return False
except Exception as e:
print(f"❌ 音频处理测试失败: {e}")
return False
def main():
print("🔧 ffmpeg检查和安装工具")
print("="*50)
# 检查ffmpeg
ffmpeg_available = check_ffmpeg()
if not ffmpeg_available:
print("\n📋 解决方案:")
print("1. 通过conda安装ffmpeg")
print("2. 手动下载ffmpeg并添加到PATH")
print("3. 使用不依赖ffmpeg的音频处理方法")
choice = input("\n选择解决方案 (1/2/3): ").strip()
if choice == "1":
install_ffmpeg_conda()
elif choice == "2":
print("请手动下载ffmpeg并添加到系统PATH")
print("下载地址: https://ffmpeg.org/download.html")
elif choice == "3":
test_audio_without_ffmpeg()
else:
print("无效选择")
else:
print("\n✅ ffmpeg已可用,可以正常进行音频处理")
test_audio_without_ffmpeg()
if __name__ == "__main__":
main()
|