ffmpeg 提取指定目录下视频的音频文件 python脚本

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)

posted @ 2024-10-29 10:16  simp00  阅读(9)  评论(0编辑  收藏  举报