自动统计文件夹下所有音频时长与个数
Posted 小蜗牛爱远行
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了自动统计文件夹下所有音频时长与个数相关的知识,希望对你有一定的参考价值。
-
服务器上每天自动 统计文件夹下所有音频文件的个数和音频时长
-
程序目录:
/home/workspace/countTime/
- 程序子文件夹:
/home/workspace/countTime/record_path/
- 程序子文件夹:
-
程序代码:
# -*- coding: utf-8 -*- import os import configparser import logging import wave import contextlib import time from datetime import datetime logging.basicConfig(level=logging.DEBUG, format='%(asctime)s line:%(lineno)d %(levelname)s : %(message)s', datefmt=' %Y-%m-%d %H:%M:%S', filename='/home/workspace/countTime/info.log', filemode='a+') today = datetime.now().strftime('%Y_%m_%d') config = configparser.ConfigParser() # 需要统计的文件夹 recording_dir = '/home/recording/' + today + '/' # 存放音频路径 recording_txt = '/home/workspace/countTime/record_path/%s.txt' % today # 存放每天的统计结果 count_txt = '/home/workspace/countTime/duration.txt' comond = "find %s -name '*.wav' -type f > %s" % (recording_dir, recording_txt) try: out_put = os.popen('%s' % comond) except Exception as e: logging.info(e) time.sleep(3) total_time = 0 num = 0 with open(recording_txt, 'r', encoding='utf-8') as rt: for wav_path in rt.read().splitlines(): try: with contextlib.closing(wave.open(wav_path, 'r')) as v: frames = v.getnframes() # 帧数 rate = v.getframerate() # 帧率(每秒的帧数) duration = frames / float(rate) # 单位:秒 total_time = total_time + duration num += 1 # content = file + " " + str(duration) + "\\n" except Exception as e: logging.info(e) #break with open(count_txt, "a+") as ct: ct.write("%s录音总量为:%s,总时长为(秒):%.2f \\n" % (today, num, total_time))
-
以上是关于自动统计文件夹下所有音频时长与个数的主要内容,如果未能解决你的问题,请参考以下文章