最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

使用Python實(shí)現(xiàn)MP3格式轉(zhuǎn)化

 更新時(shí)間:2025年01月20日 08:52:14   作者:cheese-liang  
這篇文章主要為大家詳細(xì)介紹了如何使用Python實(shí)現(xiàn)MP3格式轉(zhuǎn)化為wav,flac和ogg等,文中的示例代碼講解詳細(xì),有需要的小伙伴可以參考一下

文件準(zhǔn)備

這是我要轉(zhuǎn)化的mp3文件,我想把它轉(zhuǎn)化為wav文件,并存儲(chǔ)到wav之中

代碼準(zhǔn)備

import os
import logging
from pydub import AudioSegment
from concurrent.futures import ThreadPoolExecutor
from tqdm import tqdm
 
# 設(shè)置日志記錄器
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
 
class AudioConverter:
    def __init__(self, input_format, output_format):
        """
        初始化音頻轉(zhuǎn)換器類(lèi)
        :param input_format: 輸入文件格式 (例如: 'mp3', 'wav', 'ogg', 'flac')
        :param output_format: 輸出文件格式 (例如: 'mp3', 'wav', 'ogg', 'flac')
        """
        self.input_format = input_format
        self.output_format = output_format
        logging.info(f"AudioConverter initialized for {input_format} to {output_format} conversion.")
 
    def convert(self, input_file_path, output_file_path):
        """
        執(zhí)行音頻格式轉(zhuǎn)換
        :param input_file_path: 輸入文件路徑
        :param output_file_path: 輸出文件路徑
        """
        try:
            # 檢查輸入文件是否存在
            if not os.path.exists(input_file_path):
                logging.error(f"輸入文件不存在: {input_file_path}")
                return
            
            # 根據(jù)輸入格式加載音頻文件
            logging.info(f"開(kāi)始轉(zhuǎn)換: {input_file_path} -> {output_file_path}")
            audio = AudioSegment.from_file(input_file_path, format=self.input_format)
            
            # 導(dǎo)出為目標(biāo)格式
            audio.export(output_file_path, format=self.output_format)
            logging.info(f"轉(zhuǎn)換完成: {output_file_path}")
        except Exception as e:
            logging.error(f"轉(zhuǎn)換失敗: {e}")
 
    @staticmethod
    def batch_convert(file_pairs, input_format, output_format, max_workers=4):
        """
        批量音頻轉(zhuǎn)換,支持多線程
        :param file_pairs: [(input_file_path, output_file_path), ...] 文件路徑對(duì)
        :param input_format: 輸入文件格式
        :param output_format: 輸出文件格式
        :param max_workers: 最大并發(fā)線程數(shù)
        """
        converter = AudioConverter(input_format, output_format)
        with ThreadPoolExecutor(max_workers=max_workers) as executor:
            # 使用進(jìn)度條跟蹤批量轉(zhuǎn)換
            for _ in tqdm(executor.map(lambda file_pair: converter.convert(*file_pair), file_pairs), total=len(file_pairs)):
                pass
 
# 使用示例
if __name__ == "__main__":
    # 設(shè)置要轉(zhuǎn)換的格式,支持mp3, wav, ogg, flac等格式
    input_format = "mp3"
    output_format = "wav"
    
    # 單文件轉(zhuǎn)換示例
    input_file_path = "D:/400-File/000-Project/000-Pycharm/005-CSDN_File/005-convert_music/mp3/Joel Adams - Please Don't Go (Official Music Video).mp3"
    output_file_path = "D:/400-File/000-Project/000-Pycharm/005-CSDN_File/005-convert_music/wav/Joel Adams - Please Don't Go (Official Music Video).wav"
    
    converter = AudioConverter(input_format, output_format)
    converter.convert(input_file_path, output_file_path)

代碼共修改的位置一共有兩處

    input_file_path = "D:/400-File/000-Project/000-Pycharm/005-CSDN_File/005-convert_music/mp3/Joel Adams - Please Don't Go (Official Music Video).mp3"
    output_file_path = "D:/400-File/000-Project/000-Pycharm/005-CSDN_File/005-convert_music/wav/Joel Adams - Please Don't Go (Official Music Video).wav"

這兩處第一個(gè)是轉(zhuǎn)化MP3文件的路徑,第二處是準(zhǔn)換結(jié)果wav文件的路徑

升級(jí)用法一

    # 設(shè)置要轉(zhuǎn)換的格式,支持mp3, wav, ogg, flac等格式
    input_format = "mp3"
    output_format = "wav"

我們可以修改此處的input和output,支持的格式有,ogg,wav,flac等等等等,也就是說(shuō),如果我想從MP3到flac那么我的代碼將會(huì)是

    # 設(shè)置要轉(zhuǎn)換的格式,支持mp3, wav, ogg, flac等格式
    input_format = "mp3"
    output_format = "flac"

只用修改對(duì)應(yīng)的位置即可,對(duì)應(yīng)位置在下方

升級(jí)用法二批量準(zhǔn)換

    # 批量轉(zhuǎn)換示例
    file_pairs = [
        ("file1.mp3", "file1.wav"),
        ("file2.mp3", "file2.wav"),
        ("file3.mp3", "file3.wav")
    ]
    
    # 批量轉(zhuǎn)換mp3到wav
    AudioConverter.batch_convert(file_pairs, input_format, output_format)

批量轉(zhuǎn)換只需將 file1.MP3替換為路徑,file1.wav替換為輸出路徑

    file_pairs = [
        ("D:/400-File/000-Project/000-Pycharm/005-CSDN_File/005-convert_music/mp3/Joel Adams - Please Don't Go (Official Music Video).mp3", "D:/400-File/000-Project/000-Pycharm/005-CSDN_File/005-convert_music/wav/Joel Adams - Please Don't Go (Official Music Video).wav"),
        ("D:/400-File/000-Project/000-Pycharm/005-CSDN_File/005-convert_music/mp3/Swag _ Miyauchi _ Audio.mp3", "D:/400-File/000-Project/000-Pycharm/005-CSDN_File/005-convert_music/wav/Swag _ Miyauchi _ Audio.wav"),
    ]
    
    # 批量轉(zhuǎn)換mp3到wav
    AudioConverter.batch_convert(file_pairs, input_format, output_format)

為方便觀看我將()中,的部分換行,顯示到下方

    file_pairs = [
        ("D:/400-File/000-Project/000-Pycharm/005-CSDN_File/005-convert_music/mp3/Joel Adams - Please Don't Go (Official Music Video).mp3"
   , "D:/400-File/000-Project/000-Pycharm/005-CSDN_File/005-convert_music/wav/Joel Adams - Please Don't Go (Official Music Video).wav"),
        ("D:/400-File/000-Project/000-Pycharm/005-CSDN_File/005-convert_music/mp3/Swag _ Miyauchi _ Audio.mp3"
         , "D:/400-File/000-Project/000-Pycharm/005-CSDN_File/005-convert_music/wav/Swag _ Miyauchi _ Audio.wav"),
    ]

 需要修改的位置對(duì)應(yīng)到代碼處為

運(yùn)行結(jié)果

單個(gè)轉(zhuǎn)換

批量準(zhǔn)換

到此這篇關(guān)于使用Python實(shí)現(xiàn)MP3格式轉(zhuǎn)化的文章就介紹到這了,更多相關(guān)Python MP3格式轉(zhuǎn)化內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論

山东省| 修武县| 湘潭市| 定襄县| 苏州市| 宜丰县| 江永县| 册亨县| 东平县| 定陶县| 十堰市| 英德市| 北票市| 南安市| 扎囊县| 监利县| 米泉市| 禹城市| 密云县| 大兴区| 焉耆| 界首市| 湟中县| 威海市| 辽阳市| 延津县| 沿河| 玉树县| 宣城市| 资阳市| 柘城县| 兰考县| 金坛市| 马公市| 灵武市| 宜宾县| 长兴县| 鲁甸县| 安阳市| 宣威市| 景洪市|