What is a .wav File?
Wav is an audio file format that is similar to mp3. Wav audio files can also be played.
There are numerous methods accessible on the internet for determining the duration of wav audio files, but here we use a very simple and straightforward technique. For this task, we will employ Pydub, a Python library.
Pydub module:
To work with audio files, the Pydub library is used. It is useful for a wide range of operations. We can use pydub to play, trim, combine, split, or modify audio files.
Before we work with pydub module we should first install it.
Installation
pip install pydub
Finding the Duration of a WAV File in Python
Method #1:Using Pydub module
Approach:
- Import AudioSegment from pydub module using the import keyword
- Pass the .wav file to the from_file() function to load the audio file and store it in a variable
- Calculate the duration(len of the file) of given .wav audio file using the len() function and apply duration_seconds() function on it to convert it into seconds.
- Here the len() function gives a value in milliseconds hence we convert the duration into seconds
- Converting the above duration from seconds to minutes
- Here we are converting seconds to a certain time duration. We get minutes by dividing it by 60, and the remaining seconds by using the % (Modulo) operator.
- Round off the values using the round() function
- Print the given audio file(.wav) duration in {minutes:seconds} format.
- The Exit of the Program.
Below is the implementation:
# Import AudioSegment from pydub module using the import keyword from pydub import AudioSegment # Pass the .wav file to the from_file() function to load the audio file # and store it in a variable audio = AudioSegment.from_file("samplewavfile.wav") # Calculate the duration(len of the file) of given .wav audio file using the len() # function and apply duration_seconds() function on it to convert it into seconds. # Here the len() function gives a value in milliseconds hence we convert # the duration into seconds audio.duration_seconds == (len(audio) / 1000.0) # Converting the above duration from seconds to minutes # Here we are converting seconds to a certain time duration. We get minutes by dividing it by 60, # and the remaining seconds by using the % (Modulo) operator. minutes_duartion = int(audio.duration_seconds // 60) # Round off the values using the round() function seconds_duration = round((audio.duration_seconds % 60),3) # Print the given audio file(.wav) duration in minutes:seconds format print("The given audio file(.wav) duration in {minutes:seconds} format:") print(minutes_duartion,':',seconds_duration)
Output:
The given audio file(.wav) duration in {minutes:seconds} format: 0 : 5.042
Method #2:Using contextlib module
Get the duration by dividing the totalframes with rateof frames.
import wave import contextlib gvn_wavfile = "samplewavfile.wav" print("The duration of given wav file is:") with contextlib.closing(wave.open(gvn_wavfile,'r')) as wavfile: frames = wavfile.getnframes() rate = wavfile.getframerate() duration = frames / float(rate) print(duration)
Output:
The duration of given wav file is: 5.0416326530612245