You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用pydub转换WMA到WAV触发FileNotFoundError,求解决方案

问题:WMA批量转WAV代码触发FileNotFoundError

我写了一段批量转换目录下.wma文件为.wav的代码:

import os

from pydub import AudioSegment

# Define the directory containing the audio files
directory = r"C:\Users\test\MFG\audio_files/"

# Iterate through the folder and convert all .wma and .wav files to .mp3
for file in os.listdir(directory):
    if file.endswith(".wma"):
        wma_audio = AudioSegment.from_file(os.path.join(directory, file), format="asf")
        wma_audio.export(os.path.join(directory, file.replace(".wma", ".wav")), format="wav")
        print("Converted " + file + " to wav")

执行时在这行代码报错:

wma_audio = AudioSegment.from_file(os.path.join(directory, file), format="asf")

错误信息如下:

FileNotFoundError                         Traceback (most recent call last)
Cell In[1], line 11
      9 for file in os.listdir(directory):
     10     if file.endswith(".wma"):
---> 11         wma_audio = AudioSegment.from_file(os.path.join(directory, file), format="asf")
     12         wma_audio.export(os.path.join(directory, file.replace(".wma", ".wav")), format="wav")
     13         print("Converted " + file + " to wav")

File C:\Users\test\MFG\myenv\Lib\site-packages\pydub\audio_segment.py:728, in AudioSegment.from_file(cls, file, format, codec, parameters, start_second, duration, **kwargs)
    726     info = None
    727 else:
--> 728     info = mediainfo_json(orig_file, read_ahead_limit=read_ahead_limit)
    729 if info:
    730     audio_streams = [x for x in info['streams']
    731                      if x['codec_type'] == 'audio']

File C:\Users\test\MFG\myenv\Lib\site-packages\pydub\utils.py:274, in mediainfo_json(filepath, read_ahead_limit)
    271         file.close()
    273 command = [prober, '-of', 'json'] + command_args
--> 274 res = Popen(command, stdin=stdin_parameter, stdout=PIPE, stderr=PIPE)
    275 output, stderr = res.communicate(input=stdin_data)
    276 output = output.decode("utf-8", 'ignore')

File C:\Python312\Lib\subprocess.py:1026, in Popen.__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, user, group, extra_groups, encoding, errors, text, umask, pipesize, process_group)
   1022         if self.text_mode:
   1023             self.stderr = io.TextIOWrapper(self.stderr,
   1024                     encoding=encoding, errors=errors)
-> 1026     self._execute_child(args, executable, preexec_fn, close_fds,
   1027                         pass_fds, cwd, env,
   1028                         startupinfo, creationflags, shell,
   1029                         p2cread, p2cwrite,
   1030                         c2pread, c2pwrite,
   1031                         errread, errwrite,
   1032                         restore_signals,
   1033                         gid, gids, uid, umask,
   1034                         start_new_session, process_group)
   1035 except:
   1036     # Cleanup if the child failed starting.
   1037     for f in filter(None, (self.stdin, self.stdout, self.stderr)):

File C:\Python312\Lib\subprocess.py:1538, in Popen._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_gid, unused_gids, unused_uid, unused_umask, unused_start_new_session, unused_process_group)
   1536 # Start the process
   1537 try:
-> 1538     hp, ht, pid, tid = _winapi.CreateProcess(executable, args,
   1539                              # no special security
   1540                              None, None,
   1541                              int(not close_fds),
   1542                              creationflags,
   1543                              env,
   1544                              cwd,
   1545                              startupinfo)
   1546 finally:
   1547     # Child is launched. Close the parent's copy of those pipe
   1548     # handles that only the child should have open.  You need
   (...)
   1551     # pipe will not close when the child process exits and the
   1552     # ReadFile will hang.
   1553     self._close_pipe_fds(p2cread, p2cwrite,
   1554                          c2pread, c2pwrite,
   1555                          errread, errwrite)

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

解决方案

这个错误不是找不到你的WMA文件,而是pydub依赖的FFmpeg工具未安装或未配置到系统环境变量。pydub本身不处理音频编码解码,完全依赖FFmpeg完成格式转换。

解决步骤:

  • 下载FFmpeg静态编译包(Windows版本选full_build,包含所有编码解码器)。
  • 解压后找到bin目录(内含ffmpeg.exe、ffprobe.exe),将该路径添加到系统PATH环境变量。
  • 打开新的命令提示符,输入ffmpeg -version,能显示版本信息说明配置成功。
  • 重启Python运行环境(如Jupyter、PyCharm),再执行转换代码。

另外,代码里的注释有误:注释写的是“convert all .wma and .wav files to .mp3”,实际是转WAV,建议修正注释避免混淆:

# Iterate through the folder and convert all .wma files to .wav

内容的提问来源于stack exchange,提问作者Vallalar_dev

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.18 11:34:52