Finding PyDub stats
You decide it'll be helpful to know the audio attributes of any given file easily. This will be especially helpful for finding out how many channels an audio file has or if the frame rate is adequate for transcription.
In this exercise, we'll create show_pydub_stats() which takes a filename of an audio file as input. It then imports the audio as a PyDub AudioSegment instance and prints attributes such as number of channels, length and more.
It then returns the AudioSegment instance so it can be used later on.
We'll use our function on the newly converted .wav file, call_1.wav
AudioSegment has already imported from PyDub.
Cet exercice fait partie du cours
Spoken Language Processing in Python
Instructions
- Create an
AudioSegmentinstance calledaudio_segmentby importing thefilenameparameter. - Print the number of channels using the
channelsattribute. - Return the
audio_segmentvariable. - Test the function on
"call_1.wav".
Exercice interactif pratique
Essayez cet exercice en complétant cet exemple de code.
def show_pydub_stats(filename):
"""Returns different audio attributes related to an audio file."""
# Create AudioSegment instance
____ = AudioSegment.from_file(____)
# Print audio attributes and return AudioSegment instance
print(f"Channels: {audio_segment.____}")
print(f"Sample width: {audio_segment.sample_width}")
print(f"Frame rate (sample rate): {audio_segment.frame_rate}")
print(f"Frame width: {audio_segment.frame_width}")
print(f"Length (ms): {len(audio_segment)}")
return ____
# Try the function
call_1_audio_segment = ____(____)