Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to split the audio file in python

I'am working on Speech sentiment analysis on customer care data. I have an audio file where the customer care official has asked the question and the customer has given his review.

I need to split this audio, and get only the review part from the customer to do sentiment analysis, whether the customer is happy, sad or neutral.

Please let me know, how to split audio file to get only the audio of the customer. The audio is in the format ".aac"

So far this is what i have done:

from os import path
AUDIO_FILE = path.join(path.dirname(path.realpath('C:\\Users\\anagha\\Documents\\Python Scripts')),"Python Scripts\\audioa.aac")

halfway_point = len(AUDIO_FILE) / 2
like image 451
Anagha Avatar asked Apr 04 '17 10:04

Anagha


People also ask

How do you split an audio file into parts in Python?

This is a python code snippet that I use for splitting files as per necessity. I use the pydub library from https://github.com/jiaaro/pydub. You can modify the snippet to suit your requirement. from pydub import AudioSegment t1 = t1 * 1000 #Works in milliseconds t2 = t2 * 1000 newAudio = AudioSegment.

How do you cut audio in Python?

We can do this using the pip command as shown below in your terminal or shell. After executing the above command pydub will be installed in your machine. In the next code, we can select the duration of the file we want to cut. Or we can select the portion we required.


2 Answers

me thinks its too late to answer the original question but someone stumbling upon this question might find the procedure useful

-> use a tool to diarize the data. I have used LIUM (http://www-lium.univ-lemans.fr/diarization/doku.php)

-> interpret the output based on this beautifully simple SO post (Parsing LIUM Speaker Diarization Output)

and then finally use the timings obtained from above to splice the audio file! converting the speech to text though, is a totally different challenge and will either need a deep approach (with huge amounts of data) or reliance on an API provider (like google)

like image 28
Vikram Murthy Avatar answered Oct 04 '22 20:10

Vikram Murthy


since you used the pydub tag, here's how to do it with pydub

from pydub import AudioSegment
sound = AudioSegment.from_file(AUDIO_FILE)

halfway_point = len(sound) // 2
first_half = sound[:halfway_point]

# create a new file "first_half.mp3":
first_half.export("/path/to/first_half.mp3", format="mp3")
like image 141
Jiaaro Avatar answered Oct 04 '22 21:10

Jiaaro