当前位置:网站首页>文件内音频的时长统计并生成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实际体验没多大区别。
边栏推荐
- BIO, NIO, AIO practical study notes (easy to understand theory)
- Vim tutorial: vimtutor
- 大小屏适配
- Transformer interprets and predicts instance records in detail
- The 25 best free games on mobile in 2020
- [ingress]-ingress使用tcp端口暴露服务
- config.js related configuration summary
- scikit-image图像处理笔记
- 深入分析若依数据权限@datascope (注解+AOP+动态sql拼接) 【循序渐进,附分析过程】
- Browser Storage for H5
猜你喜欢

DevOps process demo (practical record)

深入分析若依数据权限@datascope (注解+AOP+动态sql拼接) 【循序渐进,附分析过程】

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

From "dual card dual standby" to "dual communication", vivo took the lead in promoting the implementation of the DSDA architecture

selenium模块的操作之拉钩

NACOS配置中心设置配置文件

LaTeX 图片加标题 文本分栏自动换行

Passing parameters in multiple threads

Teach you simple steps to achieve industrial raspberries pie properly installed RS232 USB drive

The hook of the operation of the selenium module
随机推荐
Q 2020, the latest senior interview Laya soul, do you know?
May I ask how to read the binlog of the two tables of hologres through flink sql, and then how to join?
Collection of error records (write down when you encounter them)
干货!教您使用工业树莓派结合CODESYS配置EtherCAT主站
Programmers should understand I/O this way
Will intelligent operation and maintenance replace manual operation and maintenance?
The method of using ROS1 bag under ROS2
Tencent greetings function SCF - entry instructions
Mina的长连接和短连接
请问下通过flink sql读取hologres 的两张表的 binlog,然后如何进行join?
Network Protocol Fundamentals - Study Notes
Nacos集群搭建
初识网页与浏览器
Complete mysql offline installation in 5 minutes
Proprietary host CDH
摆脱极域软件的限制
GetEnumerator method and MoveNext and Reset methods in Unity
Vim tutorial: vimtutor
Configuration of routers and static routes
System basics - study notes (some command records)
