当前位置:网站首页>文件内音频的时长统计并生成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实际体验没多大区别。
边栏推荐
- js判断文字是否超过区域
- 系统基础-学习笔记(一些命令记录)
- DevOps流程demo(实操记录)
- [Problem has been resolved]-Virtual machine error contains a file system with errors check forced
- Mina的长连接和短连接
- selenium learning
- 深入分析若依数据权限@datascope (注解+AOP+动态sql拼接) 【循序渐进,附分析过程】
- 网络层协议介绍
- [问题已处理]-jenkins流水线checkout超时
- [问题已处理]-虚拟机报错contains a file system with errors check forced
猜你喜欢

深夜小酌,50道经典SQL题,真香~

Autoware--Beike Tianhui rfans lidar uses the camera & lidar joint calibration file to verify the fusion effect of point cloud images

前置++和后置++的区别

LeetCode刷题记录(2)

Collision, character controller, Cloth components (cloth), joints in the Unity physics engine

LeetCode中常用语言的一些基本方法记录

Introduction to Network Layer Protocols

从“双卡双待“到”双通“,vivo率先推动DSDA架构落地

el-autocomplete use

LeetCode练习及自己理解记录(1)
随机推荐
LeetCode中常用语言的一些基本方法记录
网络层协议介绍
Seven Ways to Center a Box Horizontally and Vertically
el-autocomplete use
Xiaodu Xiaodu is here!
D39_Eulerian Angles and Quaternions
前置++和后置++的区别
System basics - study notes (some command records)
Matplotlib绘图笔记
摆脱极域软件的限制
BIO,NIO,AIO实践学习笔记(便于理解理论)
数组&的运算
Mina断线重连
干货!教您使用工业树莓派结合CODESYS配置EtherCAT主站
浏览器存储WebStorage
教您简单几步实现工业树莓派正确安装RS232转USB驱动
The method of using ROS1 bag under ROS2
[ingress]-ingress exposes services using tcp port
wc, grep, tar, vi/vim
One-arm routing experiment and three-layer switch experiment
