1

我正在使用 Librosa 函数librosa.output.write_wav将时间序列输出为 .wav 文件,然后尝试在 pydub 中打开它,但 pydub 正在抛出一条FileNotFoundError消息(见下文)。*.wav 文件绝对与我的 Jupyter 笔记本 ( convert_spect_to_wav.ipynb) 位于同一目录中。奇怪的是,如果我输入不是由 Librosa 生成的 *.wav 文件,我的代码工作得非常好。我可以收听 Librosa *.wav 文件,所以我不知道 Librosa 做了什么奇怪的事情导致 pydub 找不到 Librosa *.wav 文件。

目录

[lashen.lashen-MOBL] ➤ ls -l
total 442
-rwx------    1 lashen   UsersGrp    103566 Feb 17 14:47 bdl_arctic_a0001.wav
-rwx------    1 lashen   UsersGrp     12973 Mar 17 12:56 convert_spect_to_wav.ipynb
drwx------    1 lashen   UsersGrp         0 Mar 17 12:38 generated
-rwx------    1 lashen   UsersGrp     43066 Mar 17 12:55 generated_0.wav
-rwx------    1 lashen   UsersGrp     43066 Mar 17 12:55 generated_1.wav
-rwx------    1 lashen   UsersGrp     43066 Mar 17 12:55 generated_2.wav
-rwx------    1 lashen   UsersGrp     43066 Mar 17 12:55 generated_3.wav
-rwx------    1 lashen   UsersGrp     43066 Mar 17 12:55 generated_4.wav
-rwx------    1 lashen   UsersGrp     43066 Mar 17 12:55 generated_5.wav
-rwx------    1 lashen   UsersGrp     43066 Mar 17 12:48 hello.wav
-rwx------    1 lashen   UsersGrp    103566 Feb 17 14:58 ksp_arctic_a0001.wav
-rwx------    1 lashen   UsersGrp     44530 Mar 17 12:40 ksp_arctic_a0001_0.wav
-rwx------    1 lashen   UsersGrp      1408 Mar  6 20:21 parameters.mat
-rwx------    1 lashen   UsersGrp     25971 Mar  6 19:54 style_transfer.ipynb
-rwx------    1 lashen   UsersGrp     96044 Mar 17 12:40 whole_clip.wav

FileNotFound错误信息

---------------------------------------------------------------------------
FileNotFoundError                         Traceback (most recent call last)
<ipython-input-67-6381f1840332> in <module>()
----> 1 convert_spect_to_wav('../dataset_processed/bdl_spectrogram_array.npy')

<ipython-input-66-a201b054f438> in convert_spect_to_wav(file_name)
     19 
     20     # Piece together the wav files
---> 21     stitch_wavs_together(file_names)

<ipython-input-65-63d881a5987a> in stitch_wavs_together(file_names)
      7     for file in clip_file_list:
      8         print (file)
----> 9         clip = AudioSegment.from_wav(file)
     10         wav_clips.append(clip)
     11 

~\AppData\Local\Continuum\anaconda3\lib\site-packages\pydub\audio_segment.py in from_wav(cls, file, parameters)
    542     @classmethod
    543     def from_wav(cls, file, parameters=None):
--> 544         return cls.from_file(file, 'wav', parameters)
    545 
    546     @classmethod

~\AppData\Local\Continuum\anaconda3\lib\site-packages\pydub\audio_segment.py in from_file(cls, file, format, codec, parameters, **kwargs)
    510 
    511         with open(os.devnull, 'rb') as devnull:
--> 512             p = subprocess.Popen(conversion_command, stdin=devnull, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
    513         p_out, p_err = p.communicate()
    514 

~\AppData\Local\Continuum\anaconda3\lib\subprocess.py in __init__(self, args, bufsize, executable, stdin, stdout, stderr, preexec_fn, close_fds, shell, cwd, env, universal_newlines, startupinfo, creationflags, restore_signals, start_new_session, pass_fds, encoding, errors)
    707                                 c2pread, c2pwrite,
    708                                 errread, errwrite,
--> 709                                 restore_signals, start_new_session)
    710         except:
    711             # Cleanup if the child failed starting.

~\AppData\Local\Continuum\anaconda3\lib\subprocess.py in _execute_child(self, args, executable, preexec_fn, close_fds, pass_fds, cwd, env, startupinfo, creationflags, shell, p2cread, p2cwrite, c2pread, c2pwrite, errread, errwrite, unused_restore_signals, unused_start_new_session)
    995                                          env,
    996                                          os.fspath(cwd) if cwd is not None else None,
--> 997                                          startupinfo)
    998             finally:
    999                 # Child is launched. Close the parent's copy of those pipe

FileNotFoundError: [WinError 2] The system cannot find the file specified

不知道为什么pydub找不到Librosa输出的*.wav文件。问题在于 Librosa *.wav 文件,因为其他 *.wav 文件可以正常工作。有没有其他人遇到过这个?

这里有一些相关代码:write_time_series_to_wav创建 Librosa *.wav 文件,stitch_wavs_together这是我尝试使用 pydub 读取 Librosa *.wav 文件的地方,并convert_spect_to_wav调用这两个函数。

def write_time_series_to_wav(time_series, sampling_rate):
    file_prefix = 'generated_'
    file_names_list = []

    i = 0
    for series in time_series:
        file_name = file_prefix + str(i) + '.wav'
        librosa.output.write_wav(file_name, series, sampling_rate)
        file_names_list.append(file_name)
        i = i + 1

    return file_names_list

def stitch_wavs_together(file_names):

    wav_clips = []
    clip_file_list = glob.glob('generated_*.wav')
    print (clip_file_list)

    for file in clip_file_list:
        print (file)
        clip = AudioSegment.from_wav(file)
        wav_clips.append(clip)

    whole_clip = wav_clips[0]

    for i in range(1, len(wav_clips)):
        whole_clip = whole_clip + wav_clips[i]

    whole_clip.export('whole_clip.wav', format='wav')

def convert_spect_to_wav(file_name):
    spectrogram_array = np.load(file_name)
    print (spectrogram_array.shape)
    spectrogram_list = []

    for spectrogram in spectrogram_array:
        #print (spectrogram.shape)
        spectrogram_list.append(spectrogram)

    #spectrogram_list = spectrogram_array.tolist()
    #print (spectrogram_list)

    # Convert spectrogram to time series
    time_series = convert_spect_to_time_series(spectrogram_list)

    # Write time series out to wav files
    file_names = write_time_series_to_wav(time_series, 22050)
    print (file_names)

    # Piece together the wav files
    stitch_wavs_together(file_names)

任何帮助或输入表示赞赏!

4

0 回答 0