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
durationand/oroffsetare specified the resulting WAV file will be shortened accordingly.durationandoffsetsupport 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. Ifdurationand/oroffsetare negative, they are interpreted from right to left, whereasdurationstarts from the end of the signal foroffset=None. If the signal is shorter than the requesteddurationand/oroffsetonly 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=-4will return[0].durationandoffsetare 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. IfNonesame path asinfilebut 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 ifoutfileis identical tooutfilekwargs – 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
fileis missing, broken or format is not supportedRuntimeError – if
infilewould need to be overwritten andoverwriteisFalseValueError – if
durationis 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'