python-使用ffmpeg批量修改文件的后缀名

import os
import subprocess

def convert_ogg_to_mp3(directory):
    for filename in os.listdir(directory):
        if filename.endswith(".ogg"):
            # 获取文件的完整路径
            file_path = os.path.join(directory, filename)

            # 创建一个新的文件名,只是将扩展名从.ogg更改为.mp3
            new_filename = os.path.splitext(filename)[0] + ".mp3"
            new_file_path = os.path.join(directory, new_filename)

            # 使用ffmpeg命令行工具转换音频格式
            try:
                cmd = [
                    "ffmpeg",
                    "-i", file_path,
                    "-vn",  # 忽略视频流(如果有的话)
                    "-acodec", "libmp3lame",  # 使用MP3编码器
                    new_file_path
                ]
                subprocess.run(cmd, check=True)

                # 删除原始ogg文件
                os.remove(file_path)

            except subprocess.CalledProcessError as e:
                print(f"Error converting {filename}: {e}")
            except Exception as e:
                print(f"An error occurred while processing {filename}: {e}")

# 使用你的目录路径替换下面的'C://testqq//'
convert_ogg_to_mp3('C://testqq//')

运行python脚本music.py:

1、win+r:cmd 

2、python music.py

你可能感兴趣的:(批量改名)