-
Notifications
You must be signed in to change notification settings - Fork 0
/
metadataeditor.py
executable file
·150 lines (137 loc) · 4.5 KB
/
metadataeditor.py
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
#!/usr/bin/env python
"""
Metadata Editor
by Andrew lathama Latham
For files in an input directory:
* Set the editor/creator metadata
* Set the title/description based on the filename.
Use cases:
* Correct titles of ripped DVDs
* Rename titles for DLNA client/server usage
* Version via editor/creator metadata
"""
import argparse
import glob
import os
import sys
import unittest
sys.path.insert(0, 'mutagen-1.46.0-py3-none-any.whl')
import mutagen # pylint: disable=import-error,wrong-import-position
VERSION = 'Metadata Editor v1.0'
DEFAULT_TYPE = 'mp4'
DEFAULT_TOOL = 'Metadata Editor v1.0'
def showmetadata(filename):
"""
for a single file show the metadata found and print
"""
print(mutagen.File(filename))
def showallmetadata(filepath):
"""
for a single file show the metadata found and print
"""
allfiles = filematchlist(filepath)
for filename in allfiles:
showmetadata(filename)
def filematchlist(filepath, filetype=DEFAULT_TYPE):
"""
Return a list of files that match a type
"""
filelist = glob.glob(filepath + '*.' + filetype )
return filelist
def changemetadata(filename):
"""
Edit the metadata of a file
"""
print(filename + ' metadata before')
showmetadata(filename)
fileholder = mutagen.mp4.MP4(filename)
fileholder['©nam'] = medianamecleanup(filename)
fileholder['©too'] = DEFAULT_TOOL
fileholder.save()
print(filename + ' saved')
print(filename + ' metadata after')
showmetadata(filename)
def changeallmetadata(filepath):
"""
Edit the metadata of all files in filepath
"""
allfiles = filematchlist(filepath)
for filename in allfiles:
changemetadata(filename)
def medianamecleanup(filename):
"""
Clean the path and extension along with underscores from filename
"""
filepath = os.path.dirname(filename)
cleanedname = filename.removeprefix(filepath + '/')
cleanedname = cleanedname.replace('_', ' ')
cleanedname = cleanedname.split('.')[0]
return cleanedname
class Testmetadataeditor(unittest.TestCase):
"""
Unit testing just a few functions because reasons
"""
def setUp(self):
"""
Setup some useful variables
"""
self.filename_good = 'my_movie.mp4'
self.filename_bad = 'my.movie.mp4_.mp4'
self.filename_result_good = 'my movie'
def test_medianamecleanup(self):
"""
Test the filenames are as expected
"""
self.assertEqual(
medianamecleanup(self.filename_good), self.filename_result_good)
self.assertNotEqual(
medianamecleanup(self.filename_bad), self.filename_result_good)
if __name__ == '__main__':
theparser = argparse.ArgumentParser(
prog='metadataeditor',
description='Metadata Editor',
epilog='by Andrew lathama Latham')
groupa = theparser.add_mutually_exclusive_group(required=True)
groupa.add_argument(
'-s', '--show', action='store_true', help='Show Metadata')
groupa.add_argument(
'-c', '--change', action='store_true', help='Change Metadata')
groupa.add_argument(
'-v', '--version', action='store_true', help='Show version')
groupa.add_argument(
'-t', '--runtests', action='store_true', help='Run Unit tests')
groupb = theparser.add_mutually_exclusive_group()
groupb.add_argument(
'-f', '--filename', help='Single Filename')
groupb.add_argument(
'-d', '--directory', help='Directory')
args = theparser.parse_args()
if args.show:
if args.filename:
if os.path.isfile(args.filename):
showmetadata(str(args.filename))
else:
print('File not found')
if args.directory:
if os.path.isdir(args.directory):
showallmetadata(str(args.directory))
else:
print('Directory not found')
if args.change:
if args.filename:
if os.path.isfile(args.filename):
changemetadata(str(args.filename))
#changemetadata('/media/media/media/Yellowstone_Bears.mp4')
else:
print('File not found')
if args.directory:
if os.path.isdir(args.directory):
changeallmetadata(str(args.directory))
#changeallmetadata('/media/media/media/')
else:
print('Directory not found')
if args.version:
print(VERSION)
if args.runtests:
del sys.argv[1:]
unittest.main(verbosity=2)