自动统计文件夹下所有音频时长与个数

  • 服务器上每天自动 统计文件夹下所有音频文件的个数音频时长

    • 程序目录:/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()
      config.read("./config/aq.ini", "UTF-8")
      # 需要统计的文件夹
      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))
      
      
      
    原文作者:小蜗牛爱远行
    原文地址: https://blog.csdn.net/weixin_46046193/article/details/117411640
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞