-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAudio fixer.py
More file actions
89 lines (79 loc) · 2.87 KB
/
Audio fixer.py
File metadata and controls
89 lines (79 loc) · 2.87 KB
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
"""
Audio fixer.py is a python script file which uses the meta data to rename the audio files and based on their metadata
stores them in their individual directories created as per their album names.
"""
import os
import eyed3
import shutil
def nomenclature(fname):
"""
The function takes a string as an argument and process the string to make it an eligible file name as per the
windows specification. The following are reserved characters in windows and can't be used:
< (less than)
> (greater than)
: (colon)
" (double quote)
/ (forward slash)
\ (backslash)
| (vertical bar or pipe)
? (question mark)
* (asterisk)
:return: a string which can be used as a file name in as per Windows namespace
"""
filename = ""
Reserved = ['<','>','"','/','\\','|','?','*']
#Iterate over each character of the string given as the argument
for letter in fname:
if letter in Reserved: # if the letter is a reserved character then replace it with space " "
filename = filename + " "
elif letter == ':': #if the letter is a colon(:) reserved character then replace it with "-"
filename = filename + "-"
else: # if it is neither of them then just process it normally
filename = filename + letter
return filename
def file_mover(audiofilename,albumname):
"""
It moves the audio file to the specified directory as per the album name. If the directory already exists then it
moves the file in that directory else it creates a new directory and moves it there.
:return: Nothing
"""
source = os.path.join('E:\\Test\\',audiofilename)#audiofilename should be with .mp3 extension
destination = os.path.join('E:\\','albumname')
if os.path.isdir(destination) is True:
shutil.move(source, destination)
print "success"
else:
os.makedirs(destination)
shutil.move(source, destination)
print("success")
files = os.listdir('E:\\Test\\')
os.chdir('E:\\Test\\')
for file in files:
try:
print file
audio = eyed3.load(file)
tag = eyed3.id3.Tag()
title = tag.title
track_num = tag.track_num
album = tag.album
print title
print album
if (title is not None) | (album is not None):
newfilename = title + " - " + album
newfilename = nomenclature(newfilename)
print "Success" + newfilename
newfilename = '.'.join([newfilename,'mp3'])
print"Success" + newfilename
os.rename(file, newfilename)
print "rename success"
file_mover(newfilename,album)
else:
shutil.move(file,'E:\\unsupported mp3')
except:
print " error found with the file"
"""
TO BE DONE YET :
1.) Issues with redundancy as well as filename length is yet to be done
2.) Testing
3.) Documentation
"""