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格式的方法介绍的详细内容,更多文章请关注木庄网络博客!!

返回前面的内容

相关阅读 >>

Python用什么文本编辑器

Python安装到哪个盘

Python实现关键词提取的示例讲解

Python中闭包的简单介绍(附示例)

学习Python看这三本书让你少走一半弯路

Python如何求100内的所有素数

如何获取Python中的对象属性?(实例讲解)

Python中面向对象该如何编程

Python中end=' '是什么意思

Python都有哪些框架

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




打赏

取消

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

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

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

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

评论

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

    正在狠努力加载,请稍候...