'PyDub AudioSegment fails to load valid file

I use Python to manipulate WAV files. This code used to work:

from pydub import AudioSegment

fp = "/Volumes/.../generic.wav"
sound = AudioSegment.from_wav(fp)

And now I get this error:

Traceback (most recent call last):
  File "/usr/local/lib/python3.7/site-packages/pydub/audio_segment.py", line 670, in from_file
    return cls._from_safe_wav(file)
  File "/usr/local/lib/python3.7/site-packages/pydub/audio_segment.py", line 818, in _from_safe_wav
    file.seek(0)
AttributeError: 'NoneType' object has no attribute 'seek'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "cassandra.py", line 992, in md2episode
    sound = AudioSegment.from_wav(fp)
  File "/usr/local/lib/python3.7/site-packages/pydub/audio_segment.py", line 808, in from_wav
    return cls.from_file(file, 'wav', parameters=parameters)
  File "/usr/local/lib/python3.7/site-packages/pydub/audio_segment.py", line 678, in from_file
    file.seek(0)
AttributeError: 'NoneType' object has no attribute 'seek'

The file plays its 22 seconds and inspecting it with ffprobe seems OK:

$ ffprobe "/Volumes/.../generic.wav"
ffprobe version 4.3.1 Copyright (c) 2007-2020 the FFmpeg developers
  built with Apple clang version 11.0.0 (clang-1100.0.33.17)
  configuration: --prefix=/usr/local/Cellar/ffmpeg/4.3.1_1 --enable-shared --enable-pthreads --enable-version3 --enable-avresample --cc=clang --host-cflags= --host-ldflags= --enable-ffplay --enable-gnutls --enable-gpl --enable-libaom --enable-libbluray --enable-libdav1d --enable-libmp3lame --enable-libopus --enable-librav1e --enable-librubberband --enable-libsnappy --enable-libsrt --enable-libtesseract --enable-libtheora --enable-libvidstab --enable-libvorbis --enable-libvpx --enable-libwebp --enable-libx264 --enable-libx265 --enable-libxml2 --enable-libxvid --enable-lzma --enable-libfontconfig --enable-libfreetype --enable-frei0r --enable-libass --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libopenjpeg --enable-librtmp --enable-libspeex --enable-libsoxr --enable-videotoolbox --disable-libjack --disable-indev=jack
  libavutil      56. 51.100 / 56. 51.100
  libavcodec     58. 91.100 / 58. 91.100
  libavformat    58. 45.100 / 58. 45.100
  libavdevice    58. 10.100 / 58. 10.100
  libavfilter     7. 85.100 /  7. 85.100
  libavresample   4.  0.  0 /  4.  0.  0
  libswscale      5.  7.100 /  5.  7.100
  libswresample   3.  7.100 /  3.  7.100
  libpostproc    55.  7.100 / 55.  7.100
Input #0, wav, from '/Volumes/.../generic.wav':
  Duration: 00:00:22.16, bitrate: 384 kb/s
    Stream #0:0: Audio: pcm_s16le ([1][0][0][0] / 0x0001), 24000 Hz, 1 channels, s16, 384 kb/s

I have checked that pydub is up to date:

$ python3 -m pip install --upgrade pydub
Requirement already up-to-date: pydub in /usr/local/lib/python3.7/site-packages (0.25.1)

My Python version is:

$ python3 --version
Python 3.7.9

How can I use PyDub for loading WAV files? Or where can I find an equivalent substitute?



Solution 1:[1]

Today I cannot reproduce the issue. The input file was the result of Google Cloud speech synthesis, so it may have been an error on their end. Running the code again today fixed the issue.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 miguelmorin