import os
import subprocess
def extract_audio_from_mp4(directory):
# 检索目录下所有MP4文件
for filename in os.listdir(directory):
if filename.endswith(".mp4"):
mp4_path = os.path.join(directory, filename)
mp3_path = os.path.splitext(mp4_path)[0] + ".mp3"
# 使用ffmpeg提取音频
command = ["E:\\Downloads\\ffmpeg.exe", "-i", f'"{mp4_path}"', "-q:a", "0", "-map", "a", f'"{mp3_path}"']
if os.name == 'nt':
command = ' '.join(command)
subprocess.run(command,shell=True)
# 使用示例
directory = "F:\mp4"
extract_audio_from_mp4(directory)