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

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

代码在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
# [email protected]
# 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

猜你喜欢

转载自blog.csdn.net/jiangyupu/article/details/89054049