get the amplitude data from an mp3 audio files using python - python

I have an mp3 file and I want to basically plot the amplitude spectrum present in that audio sample.
I know that we can do this very easily if we have a wav file. There are lot of python packages available for handling wav file format. However, I do not want to convert the file into wav format then store it and then use it.
What I am trying to achieve is to get the amplitude of an mp3 file directly and even if I have to convert it into wav format, the script should do it on air during runtime without actually storing the file in the database.
I know we can convert the file like follows:
from pydub import AudioSegment
sound = AudioSegment.from_mp3("test.mp3")
sound.export("temp.wav", format="wav")
and it creates the temp.wav which it supposed to but can we just use the content without storing the actual file?

MP3 is encoded wave (+ tags and other stuff). All you need to do is decode it using MP3 decoder. Decoder will give you whole audio data you need for further processing.
How to decode mp3? I am shocked there are so few available tools for Python. Although I found a good one in this question. It's called pydub and I hope I can use a sample snippet from author (I updated it with more info from wiki):
from pydub import AudioSegment
sound = AudioSegment.from_mp3("test.mp3")
# get raw audio data as a bytestring
raw_data = sound.raw_data
# get the frame rate
sample_rate = sound.frame_rate
# get amount of bytes contained in one sample
sample_size = sound.sample_width
# get channels
channels = sound.channels
Note that raw_data is 'on air' at this point ;). Now it's up to you how do you want to use gathered data, but this module seems to give you everything you need.

Related

Get Audio Amplitude Value from a Video Streaming Source

I need to calculate amplitude of the audio from a video streaming source which is in .asf format in PYTHON. Currently, I have tried to convert it into .wav file and used wave python package but I need to do it in real time. In short, need to perform following steps;
Continously read input video stream
Pre processing the audio signal
Calculate amplitude in given interval
Currentl used wave library of python and read the stored wav format clip, then extracted the amplitude from the wave.readframes() output such that
wf = wave.open()
data = wf.readframes()
amplitude = data[2]

Converting a wav file to an image and back again

How would i convert a wave file to an image in such a way that i can recover the original wave file from the image, in python please.
I have heard of wav2vec library but its not clear from the documentation how i would convert the vector back into a wave.
ggg=[]
for wav in os.listdir('/content/drive/My Drive/New folder'):
fs, data = wavfile.read(wav)
ggg.append(data)`
I would like to append instead the image as i am creating a data-set to train an algorithm on. After completing training, the algorithm will generate images from the same distribution that another piece of code should convert to a wave file.

Python - How do I extract audio and video from the same parts of a file?

My goal is to analyze a video file (in this case an mp4 file) for the occurrence of certain features and create a new video file that just contains the video and audio from slightly before and slightly after those features occurring.
I'm using Python/OpenCV and can correctly identify the features in the video and can create the new video file that I want.
I can also use the subprocess module and ffmpeg to extract the full audio from the original file and I can use the wave module to iterate over the audio frames. I'm also planning to use ffmpeg to combine the resulting audio and video files.
My issue is extracting the audio that matches up with the frames in the new, condensed video file. The number of frames in the original video file (according to OpenCV) doesn't equal the number of frames returned from Wave.getnframes(), so I'm not sure how to extract just the audio that I need.

How do I write a file in headerless PCM format?

How do I write a file in headerless PCM format? The source data is in a numpy array, and I have an application that is expecting the data in a headerless PCM file, but I can't find any documentation of the file format? Is it basically the same as the data chunk in a wave file?
The problem is that there is no one "headerless PCM" format. For example, 8-bit mono 22K and little-endian 16-bit stereo 48K are both perfectly fine examples of PCM, and the whole point of "headerless" is that you need to know that information through some out-of-band channel.
But, assuming you have the expected format, it's as simple as it sounds.
for sample in samples:
for channel in channels:
f.write(the_bytes_in_the_right_endianness)
And if you've already got data in that format, just write it to the file as one big block.
The "default" codec for WAV files—and the only one supported by Python's wave module—is PCM. So, yes, if you have a WAV file in the same format you want, you can just copy its raw frame data into a headerless file, and you've got a headerless PCM file. (And if you have a wav in the wrong format, you can usually use a simple transformation, or at worst something out of audioop, to convert it.)

Determine MP3 bit depth in Python via Mutagen

Is there a way to determine an MP3 file's encoded bit depth (ie 8, 16, 24, 32) in Python using the Mutagen library?
The transformations done by the MP3 encoding process drop completely the concept of “bit depth”. You can only know the bit depth of the source audio if such information was stored in a tag of the MP3 file. Otherwise, you can take the MP3 data and produce 8-bit, 16-bit or 24-bit audio.
I've not heard "bit depth" with regard to mp3s so I'm assuming you mean bit rate. From the Mutagen tutorial:
from mutagen.mp3 import MP3
audio = MP3("example.mp3")
print audio.info.length, audio.info.bitrate
That second portion (audio.info.bitrate) should be what you need.

Categories

Resources