当前位置:网站首页>Splicing audio files with ffmpeg-4.3

Splicing audio files with ffmpeg-4.3

2022-06-24 21:03:00 CrazyManhhh

from scipy.io import wavfile
import numpy as np
from os import listdir
from ffmpy import FFmpeg as mpy
import os
import shutil

class ConbineSound(object):
    """docstring for ConbineSound"""

    def __init__(self,saveName):
        super(ConbineSound, self).__init__()
        #mp3 convert to wav Format Directory , That is, the directory of audio splicing 
        self.wavPath = r'C:\Users\HK\Desktop\ speech recognition \wav'
        #mp3 Initial file directory 
        self.mp3Path = r'C:\Users\HK\Desktop\ speech recognition \sound2'
        # The final synthesized audio file 
        # self.saveName = r'C:\Users\HK\Desktop\python\ The code base \ Online character recognition \static\media\result_sound.mp3'
        self.saveName = saveName
    def combineS(self):
        wavs = listdir(self.wavPath)
        wavs = sorted(wavs,key=lambda x:int(x.split('.')[0]))
        all_sounds = []
        for wav in wavs:
            file = self.wavPath + '/' + wav
            data = wavfile.read(file)
            all_sounds.append(data[1][:10 * 14100])
        mix = np.concatenate(all_sounds)
        wavfile.write(self.saveName, 14100, mix)

    #  Empty folder 
    def delFiles(self):
        # 2. Delete folder 
        shutil.rmtree(self.wavPath)
        # 3. Recreate the folder 
        os.mkdir(self.wavPath)

    def trans_to_wav(self,mp3_file, wav_folder):
        '''
         Format conversion 
        :param mp3_file:
        :param wav_folder:
        :return:
        '''
        #  Format file 
        file_fmt = os.path.basename(mp3_file).strip()
        #  Get file format 
        file_fmt = file_fmt.split('.')[-1]
        #  Verify file format 
        if file_fmt.strip() != 'mp3':
            raise Exception(' Change file is not MP3 Format , Please check !')
        elif file_fmt.strip() == '':
            raise Exception(' Exception in file format , Please check !')
        #  establish wav For output after conversion 
        wav_file_path = os.path.join(wav_folder)
        wav_file_path = os.path.join(wav_file_path, '{}.{}'.format(
            os.path.basename(mp3_file).strip().split('.')[0], 'wav'
        ))
        #  Command line parameter string when creating conversion 
        cmder = '-f wav -ac 1 -ar 16000'
        #  Create converter object 
        mpy_obj = mpy(
            inputs={
                mp3_file: None
            },
            outputs={
                wav_file_path: cmder
            }
        )
        print(' perform CMDER  command :{}'.format(mpy_obj.cmd))
        mpy_obj.run()

    def read_folder(self):
        '''
         Folder read function 
        :param mp3_folder:
        :param wav_folder:
        :return:
        '''
        #  Traversing the... That needs to be converted MP3 In folder MP3 file 
        mp3_folder = self.mp3Path
        wav_folder = self.wavPath

        for a in os.listdir(mp3_folder):
            #  establish MP3 The absolute path to the file 
            mp3_file = os.path.join(mp3_folder, a)
            #  Call the format conversion function 
            self.trans_to_wav(mp3_file, wav_folder)

function : Will one mp3 Files are converted in batch to wav Format , And then wav The format depends on the file name ( for example :1.wav,2.wav) Sort and splice .

install : be used ffmpeg-4.3-win64-static Software , After installation D:\ffmpeg-4.3-win64-static\bin Add to environment variables , then pip install ffmpeg==1.4 ;scipy It's best to use it. 1.2.1

原网站

版权声明
本文为[CrazyManhhh]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202211322105784.html