convert_to_wav()¶
- audiofile.convert_to_wav(infile, outfile=None, offset=None, duration=None, bit_depth=16, normalize=False, overwrite=False, **kwargs)[source]¶
Convert any audio/video file to WAV.
It uses soundfile for reading WAV, FLAC, MP3, OGG files, and sox or ffmpeg for reading all other files. If
duration
and/oroffset
are specified the resulting WAV file will be shortened accordingly.duration
andoffset
support all formats mentioned inaudmath.duration_in_seconds()
, like'2 ms'
, orpd.to_timedelta(2, 's')
. The exception is that float and integer values are always interpreted as seconds and strings without unit always as samples. Ifduration
and/oroffset
are negative, they are interpreted from right to left, whereasduration
starts from the end of the signal foroffset=None
. If the signal is shorter than the requestedduration
and/oroffset
only the part of the signal overlapping with the requested signal is returned, e.g. for a file containing the signal[0, 1, 2]
,duration=2
,offset=-4
will return[0]
.duration
andoffset
are evenly rounded after conversion to samples.It then uses
soundfile.write()
to write the WAV file, which limits the number of supported channels to 65535.- Parameters
infile (
str
) – audio/video file nameoutfile (
Optional
[str
]) – WAV file name. IfNone
same path asinfile
but file extension is replaced by'wav'
duration (
Union
[float
,int
,str
,timedelta64
,None
]) – return only a specified durationoffset (
Union
[float
,int
,str
,timedelta64
,None
]) – start reading at offsetbit_depth (
int
) – bit depth of written file in bit, can be 8, 16, 24normalize (
bool
) – normalize audio data before writingoverwrite (
bool
) – force overwriting ifoutfile
is identical tooutfile
kwargs – pass on further arguments to
soundfile.write()
- Return type
- Returns
absolute path to resulting WAV file
- Raises
FileNotFoundError – if ffmpeg binary is needed, but cannot be found
RuntimeError – if
file
is missing, broken or format is not supportedRuntimeError – if
infile
would need to be overwritten andoverwrite
isFalse
ValueError – if
duration
is a string that does not match a valid ‘<value><unit>’ pattern or the provided unit is not supported
Examples
>>> path = convert_to_wav("stereo.flac") >>> os.path.basename(path) 'stereo.wav'