解决问题:获取音频长度,并生成固定的时间格式。
遇到问题
安装插件
- 系统安装相应插件ffpemgh或ffprobe
- 注意根据windows或其他系统进行选择ffmpeg版本
- 我的版本时windows 7 64位亲测可用
- 安装过程参考https://wqian.net/blog/2018/1107-split-sound-index.html
组织代码
- 懒虫测试文件下,得到时间格式是
00:00:20.000000
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34# -*- coding: utf-8 -*-
import subprocess
import json
def main():
mp3ss=getLenTime("03.wav")
print(mp3ss)
mp3ss,mp3ms=mp3ss.split(".")
m, s = divmod(int(mp3ss), 60)
h, m = divmod(m, 60)
hmsms="%02d:%02d:%02d" % (h, m, s) + "." + mp3ms
print (hmsms)
def getLength(filename):
command = ["ffprobe.exe","-loglevel","quiet","-print_format","json","-show_format","-show_streams","-i",filename]
result = subprocess.Popen(command,shell=True,stdout = subprocess.PIPE, stderr = subprocess.STDOUT)
out = result.stdout.read()
#print(str(out))
temp = str(out.decode('utf-8'))
try:
data = json.loads(temp)['streams'][1]['width']
except:
data = json.loads(temp)['streams'][0]['width']
return data
def getLenTime(filename):
command = ["ffprobe.exe","-loglevel","quiet","-print_format","json","-show_format","-show_streams","-i",filename]
result = subprocess.Popen(command,shell=True,stdout = subprocess.PIPE, stderr = subprocess.STDOUT)
out = result.stdout.read()
#print(str(out))
temp = str(out.decode('utf-8'))
data = json.loads(temp)["format"]['duration']
return data
if __name__ == '__main__':
main()
运用python获取mp3音频时间长度函数
- 函数如下,可以调用
getMp3Len("03.wav"),得到的是
0.000S`的格式1
2
3
4
5
6
7
8
9
10def getMp3Len(filename):
#mp3时间长度
#print('获取音频文件长度'+ filename)
command = ["ffprobe.exe","-loglevel","quiet","-print_format","json","-show_format","-show_streams","-i",filename]
result = subprocess.Popen(command,shell=True,stdout = subprocess.PIPE, stderr = subprocess.STDOUT)
out = result.stdout.read()
#print(str(out))
temp = str(out.decode('utf-8'))
data = json.loads(temp)["format"]['duration']
return float(data)