How to modify MP3 files using Python

Source: Internet
Author: User
Tags unpack

How to modify MP3 files using Python

This example describes how to modify an MP3 file in Python. Share it with you for your reference. The details are as follows:

The MP3 File Modified by this program is smaller than the original one. Because an image is deleted, it plays a role of "slimming" the MP3 file. In some mp3 files, each of which has an image of more than 400 K and a dozen MP3 files is equivalent to the size of an ordinary MP3 file.

?

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

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

#-*-Coding: cp936 -*-

"""

Remove ID3V2.3 from the MP3 file for playing on the MP3 host.

Usage: mp3lcear [Source mp3 Directory] [generated mp3 Directory]

"""

Import sys

Import OS

Import string

Import shutil

Import struct

Import thread

Import threading

Import time

Mp3suffix = 'mp3'

Class Process (threading. Thread ):

"""

Simply display the progress during running

"""

Def _ init _ (self, msg, sleepTime ):

Threading. Thread. _ init _ (self)

Self. msg = msg

Self. running = True

Self. sleepTime = sleepTime

Def setPause (self, pause ):

Self. pause = pause

Def setRunning (self, running ):

Self. running = running

Def run (self ):

While (self. running ):

Self. pause. wait ()

Print self. msg,

Time. sleep (self. sleepTime)

Def usage (code, msg = ''):

"""

Procedure

"""

Print> sys. stderr, _ doc __

If msg:

Print> sys. stderr, msg

Sys. exit (code)

Def checkDir (argDir, create = False ):

"""

Check whether the directory exists. If create is true, a new directory is created.

"""

TempDir = None

If (not OS. path. isdir (argDir )):

CurrentDir = OS. path. abspath (OS. curdir)

TempDir = OS. path. join (currentDir, argDir)

If (not OS. path. isdir (tempDir) and create ):

OS. mkdir (tempDir)

Else:

Usage (1, "directory" + argDir + "nonexistent ")

Else:

TempDir = OS. path. abspath (argDir)

Return tempDir

Def clearMp3 (srcFile, destFile ):

"""

Modify the mp3 file and create it to the address specified by destFile.

"""

Global process

Srcfp = None

Filesize = OS. path. getsize (srcFile)

Try:

Srcfp = open (srcFile, 'rb ')

Head = srcfp. read (3)

If (head = 'id3 '):

Srcfp. seek (3, 1)

Size = srcfp. read (4)

If (not len (size) = 4 ):

Print srcFile + 'file format error'

Else:

Size0 = struct. unpack ('B', size [0]) [0]

Size1 = struct. unpack ('B', size [1]) [0]

Size2 = struct. unpack ('B', size [2]) [0]

Size3 = struct. unpack ('B', size [3]) [0]

HeadSize = (size0 & 0x7f) <21) | (size1 & 0x7f) <14) | (size2 & 0x7f) <7) | (size3 & 0x7f ))

Filesize = filesize-headSize

Destfp = None

Try:

DataLen = 0

Destfp = open (destFile, 'wb ')

Srcfp. seek (headSize, 1)

Data = srcfp. read (1, 1024)

While (data! = ''):

Destfp. write (data)

Data = srcfp. read (1, 1024)

Except t Exception, e:

Print 'create file' + destFile + 'error', e

Try:

If (destfp! = None ):

Destfp. close

Except t Exception, de:

Print de

Else:

Print srcFile + 'do not need to modify copy ',

Try:

Shutil. copyfile (srcFile, destFile)

Except t Exception, ce:

Print ce

Failed t Exception, oe:

Print 'modification error ', oe

Try:

If (srcfp! = None ):

Srcfp. close ()

Failed t Exception, se:

Print de

If _ name _ = "_ main __":

If (len (sys. argv) <3 ):

Usage (1)

Global process

SourceDir = checkDir (sys. argv [1])

DestDir = checkDir (sys. argv [2], True)

Print 'mp3 source directory', sourceDir

Print 'mp3 destination directory', destDir

Process = Process ('...', 1)

Pause = threading. Event ()

Process. setPause (pause)

Process. start ()

For filename in OS. listdir (sourceDir ):

SrcPath = OS. path. join (sourceDir, filename)

DestPath = OS. path. join (destDir, filename)

If OS. path. isfile (srcPath ):

Print 'start processing '+ filename,

Tempfilename = filename. lower ()

If (not tempfilename. endswith (mp3suffix )):

Print filename + 'not an mp3 file \ N'

Else:

Pause. set ()

ClearMp3 (srcPath, destPath)

Pause. clear ()

Print 'end \ N'

Pause. set ()

Process. running = False

Sys. exit (0)

I hope this article will help you with Python programming.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.