python判断视频是否为mp3格式的方法介绍


当前第2页 返回上一页

也就是说,根据TAG_V2(ID3V2),音频数据,TAG_V1(ID3V1)三结构中的开头信息,便可以判断出是不是mp3编码的文件。

2.python代码

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

35

36

37

38

39

40

41

42

43

44

45

46

# coding: utf-8

import os

#mp3filePath是否是mp3格式的

def isMp3Format(mp3filePath):

 #读取文件内字符串

 f = open(mp3filePath, "r");

 fileStr = f.read();

 f.close();

 head3Str = fileStr[:3];

 #判断开头是不是ID3

 if head3Str == "ID3":

  return True;

 #判断结尾有没有TAG

 last32Str = fileStr[-32:];

 if last32Str[:3] == "TAG":

  return True;

 #判断第一帧是不是FFF开头, 转成数字

 # fixme 应该循环遍历每个帧头,这样才能100%判断是不是mp3

 ascii = ord(fileStr[:1]);

 if ascii == 255:

  return True;

 return False;

#遍历folderPath看看是不是都是mp3格式的,

#是就true,不是就是false, 并返回是mp3的list,不是MP3的list

def isMp3FolderTraverse(folderPath):

 mp3List = [];

 notMp3List = [];

 isAllMpFormat = True;

 for dirpath, dirnames, filenames in os.walk(folderPath):

  for filename in filenames:

   path = dirpath + os.sep + filename;

   isMp3 = isMp3Format(path);

   #判断是不是mp3结尾的 并且 是mp3格式的

   if isMp3 == False and str.endswith(path, ".mp3") == True:

    # print("--warning: file " + path + " is not mp3 format!--");

    notMp3List.append(path);

    isAllMpFormat = False;

   else:

    mp3List.append(path);

 return isAllMpFormat, mp3List, notMp3List;

if __name__ == '__main__':

 isMp3Format("s_com_click1.mp3");

 isAllMp3, mp3List, notMp3List = isMp3FolderTraverse("sound");

 print isAllMp3;

 print mp3List;

 print notMp3List;

以上就是python判断视频是否为mp3格式的方法介绍的详细内容,更多文章请关注木庄网络博客!!

返回前面的内容

相关阅读 >>

anaconda和Python区别

Python怎么调试程序

数学建模可以用Python

学习使用Python的statsmodels模块拟合arima模型

Python实现百度语音识别api

Python学起来难吗

Python中的组合数据类型可以分为哪三类

Python如何整段注释

Python小课值吗

如何在Python中使用while语句[适合初学者]

更多相关阅读请进入《Python》频道 >>




打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...