0

I'm trimming some audio files with librosa. This works fine, but instead of saving the trimmed file, I would like to get the length before trimming and after trimming. The code I have now can print all the information I need, I just need to get it into a pandas data frame where column1 = filename, column2 = length before trim, column3 = length after trim. I don't need 'sr'. I can't seem to figure out how to do that right... help appreciated.

here's the code:

for file in files:
    print(file)
    audio, sr = librosa.load(file, sr= 16000, mono=True)
    print(audio.shape, sr)
    trimmed = librosa.effects.trim(audio, top_db= 45)
    print(trimmed[0].shape, sr)
    

here's the output I'm getting with the print command:

/Desktop/py_scripts/audio_experiments/wav/1031_8637_73170.wav
(39680,) 16000
(38144,) 16000
/Desktop/py_scripts/audio_experiments/wav/1060_8777_76783.wav
(28160,) 16000
(28160,) 16000
/Desktop/py_scripts/audio_experiments/wav/1128_8634_82873.wav
(74240,) 16000
(74240,) 16000

1 Answer 1

1

You should collect the data into lists or a dict and then use it to construct a dataframe

import pandas as pd

file_lst, len_before_lst, len_after_lst = [], [], []
for file in files:
    file_lst.append(file)
    audio, sr = librosa.load(file, sr= 16000, mono=True)
    len_before_lst.append(audio.shape[0])
    trimmed = librosa.effects.trim(audio, top_db= 45)
    len_after_lst.append(trimmed[0].shape)

df = pd.DataFrame([file_lst, len_before_lst, len_after_lst], index=['filename', 'len_before', 'len_after']).T
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.