批量统计多媒体文件的时长

在做语音识别的实验时,语音文件的总时长是个很重要的指标。我们单位有一批生语料,是在安静环境下的朗读文件。每个文件长短不一。为了得到所有文件的总时长,专门写了这段代码。

代码在ubuntu16.04下,使用python2.7编写,用到的包有:commands,os,sys,需要系统安装mediainfo软件。主要思路是遍历文件夹下的所有媒体文件,对每一个文件使用mediainfo获取时长。单个文件信息的获取和结果为:

 

$ mediainfo train/wav/SN0306-085776-KASHI-null-M-21-Xiaomi-MI2SC-302.wav
General
Complete name                            : train/wav/SN0306-085776-KASHI-null-M-21-Xiaomi-MI2SC-302.wav
Format                                   : Wave
File size                                : 4.52 KiB
Duration                                 : 142ms
Overall bit rate mode                    : Constant
Overall bit rate                         : 261 Kbps

Audio
Format                                   : PCM
Format settings, Endianness              : Little
Format settings, Sign                    : Signed
Codec ID                                 : 1
Duration                                 : 142ms
Bit rate mode                            : Constant
Bit rate                                 : 256 Kbps
Channel(s)                               : 1 channel
Sampling rate                            : 16.0 KHz
Bit depth                                : 16 bits
Stream size                              : 4.46 KiB (99%)

其中:Duration: 142ms就是声音文件的时长。我们通过分析结果的结构,可以使用python语言的切片和字符替换得到时长(如这里的142ms)。通过文件遍历,将目标文件夹下的所有声音文件中的时长相加,即可得到一批文件的总时长。代码如下:

#coding=utf8
#python2.7
#data:20190404
#author:JiangYP
# jiangyupu@hotmail.com
# XinJiang University
# Statistic the Duration of medias,such as muisic or moves
#usage : python statisticDuration.py path/path
#ps:ValueErrors or IndexErrors should be addressed after the code is  complete,and nerver interrupt the program
import commands
import os
import sys
path = sys.argv[1]  # 
seconds = 0 #second
mseconds = 0 #  millisecond

for _,_,files in os.walk(path): #Traverse the current folder
    for filen in files:
        try:
                filename = path+'/'+filen # Get the path of current file
                commLine ='mediainfo '+filename #Execute 
                (_, output) = commands.getstatusoutput(commLine) # Get the output
                output = output[:output.find('Audio')] #Get the info while contain duration
                output = output.replace(' ','').strip() # 
                timeItem=output[output.find('Duration:'):output.find('Overallbitratemode:')]
                timeDur =timeItem.split(':')[1].replace('ms','')  # Get the timeDuration
                # print u'timeDur: ',timeDur
                Sc, Msc=timeDur.strip().split('s')
                #timeDur=float(timeDur)
                Sc = Sc[Sc.find(':')+1:]
                Scf = float(Sc.strip()) #Get second
                Mscf= float(Msc.strip())/(10**len(Msc.strip()))# Get millisecond
                seconds = seconds + Scf 
                mseconds = mseconds + Mscf
        except ValueError:  #Log possible error
                print u'ErrorType 1:ValueError:  ', filen
        except IndexError:
                print u'ErrorType 2':IndexError:  ', filen
        
print 'Total: ',seconds + mseconds , 'S' # Returns total length of time

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值