当前位置:网站首页>文件内音频的时长统计并生成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实际体验没多大区别。
边栏推荐
- Tencent Internal Technology: Evolution of Server Architecture of "The Legend of Xuanyuan"
- disabledDate 日期选择器 datePicker
- 通过反射获取Class对象的四种方式
- selenium模块的操作之拉钩
- Mina断线重连
- DevOps process demo (practical record)
- transport layer protocol
- From "dual card dual standby" to "dual communication", vivo took the lead in promoting the implementation of the DSDA architecture
- ROS2下使用ROS1 bag的方法
- Chengyun Technology was invited to attend the 2022 Alibaba Cloud Partner Conference and won the "Gathering Strength and Going Far" Award
猜你喜欢

Problems encountered in installing Yolo3 target detection module in Autoware

selenium learning

numpy.random使用文档

Configuration of routers and static routes

One-arm routing experiment and three-layer switch experiment

Dry!Teach you to use industrial raspberries pie combining CODESYS configuration EtherCAT master station

Chengyun Technology was invited to attend the 2022 Alibaba Cloud Partner Conference and won the "Gathering Strength and Going Far" Award

Transformer详细解读与预测实例记录

Tencent Internal Technology: Evolution of Server Architecture of "The Legend of Xuanyuan"

LeetCode中常用语言的一些基本方法记录
随机推荐
The cocos interview answers you are looking for are all here!
Collection of error records (write down when you encounter them)
Browser Storage for H5
DisabledDate date picker datePicker
NAT experiment
Programmers should understand I/O this way
LaTeX笔记
What is Alibaba Cloud Express Beauty Station?
[ingress]-ingress exposes services using tcp port
云计算基础-学习笔记
大小屏适配
What are some things that you only know when you do operation and maintenance?
disabledDate 日期选择器 datePicker
input详解之文件上传
跨域的十种解决方案详解(总结)
Come, come, let you understand how Cocos Creator reads and writes JSON files
May I ask how to read the binlog of the two tables of hologres through flink sql, and then how to join?
VRRP overview and experiment
From "dual card dual standby" to "dual communication", vivo took the lead in promoting the implementation of the DSDA architecture
link 和@improt的区别
