当前位置:网站首页>文件内音频的时长统计并生成csv文件
文件内音频的时长统计并生成csv文件
2022-08-05 05:25:00 【回首思】
目录
一、需求
请编写 Python 脚本,统计附属压缩文件“wav.zip”中音频的时长,并将统计结果保存在与 “wav.zip”同级目录下,结果文件中音频的时长由高往低的顺序依次排列,A 列为音频文件名称,B 列为 时长(精确到毫秒,时长相同按文件的 MD5 值排序)
二、相关库
- os
- librosa
- hashlib
hanshlib安装时报错,解决办法:更改版本号即可这里用的是0.8.0
pip install hanshlib==0.8.0
- pandas
三、完整代码
因为作者偷懒所以直接放代码,具体步骤:
- 使用os把文件夹内所有的文件名遍历出来
- 把文件名拼上路径
- 用hashlib给文件生成对应的MD5值
- 调用librosa.get_duration来取得音频时长
- 用pandas对结果根据时长和MD5值来排序
- pandas存储到csv文件里
import os
import librosa
import hashlib
import pandas as pd
file_list = []
data_list = []
def get_duration_mp3_and_wav(file_path):
# 文件夹路径file_path是文件名os自动遍历的
file_paths = f'./wav2count/{file_path}'
md5 = hashlib.md5()
f = open(file_paths,'rb')
md5.update(f.read())
f.close()
MD5 = md5.hexdigest()
duration = librosa.get_duration(filename=file_paths)
data_list.append([file_path,duration,MD5])
# 文件夹路径
for a,s,k in os.walk('./wav2count'):
file_list.append(k)
for i in file_list[0]:
get_duration_mp3_and_wav(i)
data = pd.DataFrame(data_list)
datas = data.sort_values([1,2],ascending=False)
# 保存的文件名
datas.to_csv('./wav_result.csv')
四、一些问题
我只试过wav文件,mp3文件一个也可以,前提是文件夹里的文件全是要参与的文件,安装库的时候遇到问题简易回退一个大版本0.9.0=>0.8.0实际体验没多大区别。
边栏推荐
- [ingress]-ingress使用tcp端口暴露服务
- Seven Ways to Center a Box Horizontally and Vertically
- disabledDate 日期选择器 datePicker
- 浏览器存储WebStorage
- Autoware--Beike Tianhui rfans lidar uses the camera & lidar joint calibration file to verify the fusion effect of point cloud images
- D39_ coordinate transformation
- Mina断线重连
- One-arm routing experiment and three-layer switch experiment
- scikit-image图像处理笔记
- VRRP overview and experiment
猜你喜欢
input detailed file upload
多行文本省略
LeetCode practice and self-comprehension record (1)
[问题已处理]-虚拟机报错contains a file system with errors check forced
BIO, NIO, AIO practical study notes (easy to understand theory)
滚动条问题,未解决
VLAN is introduced with the experiment
Complete mysql offline installation in 5 minutes
[Problem has been resolved]-Virtual machine error contains a file system with errors check forced
DevOps流程demo(实操记录)
随机推荐
Browser Storage for H5
What should I do if the SSL certificate prompts that it is expired or invalid?
NACOS配置中心设置配置文件
D39_Eulerian Angles and Quaternions
js判断文字是否超过区域
【考研结束第一天,过于空虚,想对自己进行总结一下】
[Problem has been resolved]-Virtual machine error contains a file system with errors check forced
Shadowless Cloud Desktop
跨域的十种解决方案详解(总结)
Cloud Computing Basics - Study Notes
Mina断线重连
Pytorch分布式并行处理
config.js相关配置汇总
Advantages of overseas servers
numpy.random使用文档
What are some things that you only know when you do operation and maintenance?
初识网页与浏览器
LaTeX 图片加标题 文本分栏自动换行
Collection of error records (write down when you encounter them)
H5开发调试-Fiddler手机抓包