mp3-cover-adder 0.1.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- mp3_cover_adder-0.1.0/.gitignore +54 -0
- mp3_cover_adder-0.1.0/LICENSE +21 -0
- mp3_cover_adder-0.1.0/PKG-INFO +187 -0
- mp3_cover_adder-0.1.0/README.md +154 -0
- mp3_cover_adder-0.1.0/examples/basic_usage.py +62 -0
- mp3_cover_adder-0.1.0/pyproject.toml +66 -0
- mp3_cover_adder-0.1.0/src/mp3_cover_adder/__init__.py +25 -0
- mp3_cover_adder-0.1.0/src/mp3_cover_adder/core.py +334 -0
- mp3_cover_adder-0.1.0/tests/__init__.py +1 -0
- mp3_cover_adder-0.1.0/tests/conftest.py +113 -0
- mp3_cover_adder-0.1.0/tests/test_core.py +363 -0
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
# Python
|
|
2
|
+
__pycache__/
|
|
3
|
+
*.py[cod]
|
|
4
|
+
*$py.class
|
|
5
|
+
*.so
|
|
6
|
+
.Python
|
|
7
|
+
build/
|
|
8
|
+
develop-eggs/
|
|
9
|
+
dist/
|
|
10
|
+
downloads/
|
|
11
|
+
eggs/
|
|
12
|
+
.eggs/
|
|
13
|
+
lib/
|
|
14
|
+
lib64/
|
|
15
|
+
parts/
|
|
16
|
+
sdist/
|
|
17
|
+
var/
|
|
18
|
+
wheels/
|
|
19
|
+
share/python-wheels/
|
|
20
|
+
*.egg-info/
|
|
21
|
+
.installed.cfg
|
|
22
|
+
*.egg
|
|
23
|
+
MANIFEST
|
|
24
|
+
|
|
25
|
+
# Virtual environments
|
|
26
|
+
venv/
|
|
27
|
+
env/
|
|
28
|
+
ENV/
|
|
29
|
+
env.bak/
|
|
30
|
+
venv.bak/
|
|
31
|
+
|
|
32
|
+
# Testing
|
|
33
|
+
.pytest_cache/
|
|
34
|
+
.coverage
|
|
35
|
+
htmlcov/
|
|
36
|
+
.tox/
|
|
37
|
+
.nox/
|
|
38
|
+
|
|
39
|
+
# IDEs
|
|
40
|
+
.vscode/
|
|
41
|
+
.idea/
|
|
42
|
+
*.swp
|
|
43
|
+
*.swo
|
|
44
|
+
*~
|
|
45
|
+
|
|
46
|
+
# OS
|
|
47
|
+
.DS_Store
|
|
48
|
+
Thumbs.db
|
|
49
|
+
|
|
50
|
+
# Project specific
|
|
51
|
+
*.mp3
|
|
52
|
+
*.jpg
|
|
53
|
+
*.png
|
|
54
|
+
!tests/fixtures/**
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Sherotree
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,187 @@
|
|
|
1
|
+
Metadata-Version: 2.5
|
|
2
|
+
Name: mp3-cover-adder
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Python library to embed MP3 album art and edit ID3 tags
|
|
5
|
+
Project-URL: Homepage, https://github.com/sherotree/mp3-cover-adder-py
|
|
6
|
+
Project-URL: Repository, https://github.com/sherotree/mp3-cover-adder-py
|
|
7
|
+
Project-URL: Web Tool, https://www.uwarp.design/mp3-cover-adder
|
|
8
|
+
Author-email: Sherotree <sherotree@users.noreply.github.com>
|
|
9
|
+
License-Expression: MIT
|
|
10
|
+
License-File: LICENSE
|
|
11
|
+
Keywords: album-art,audio,cover-art,id3,metadata,mp3
|
|
12
|
+
Classifier: Development Status :: 4 - Beta
|
|
13
|
+
Classifier: Intended Audience :: Developers
|
|
14
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
15
|
+
Classifier: Programming Language :: Python :: 3
|
|
16
|
+
Classifier: Programming Language :: Python :: 3.8
|
|
17
|
+
Classifier: Programming Language :: Python :: 3.9
|
|
18
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
19
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
20
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
21
|
+
Classifier: Topic :: Multimedia :: Sound/Audio
|
|
22
|
+
Requires-Python: >=3.8
|
|
23
|
+
Requires-Dist: mutagen>=1.45.0
|
|
24
|
+
Provides-Extra: dev
|
|
25
|
+
Requires-Dist: black>=23.0.0; extra == 'dev'
|
|
26
|
+
Requires-Dist: pytest-cov>=4.0.0; extra == 'dev'
|
|
27
|
+
Requires-Dist: pytest>=7.0.0; extra == 'dev'
|
|
28
|
+
Requires-Dist: ruff>=0.1.0; extra == 'dev'
|
|
29
|
+
Provides-Extra: test
|
|
30
|
+
Requires-Dist: pytest-cov>=4.0.0; extra == 'test'
|
|
31
|
+
Requires-Dist: pytest>=7.0.0; extra == 'test'
|
|
32
|
+
Description-Content-Type: text/markdown
|
|
33
|
+
|
|
34
|
+
# mp3-cover-adder
|
|
35
|
+
|
|
36
|
+
[**Add Cover to MP3**](https://www.uwarp.design/mp3-cover-adder) — free browser tool for embedding album art and editing ID3 tags (the reference UI for this library).
|
|
37
|
+
|
|
38
|
+
Offline Python library for embedding album art and editing ID3 metadata in MP3 files.
|
|
39
|
+
|
|
40
|
+
Package: **`mp3-cover-adder`** on PyPI (`0.1.0`).
|
|
41
|
+
|
|
42
|
+
## Install
|
|
43
|
+
|
|
44
|
+
```bash
|
|
45
|
+
pip install mp3-cover-adder
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
## Quick start
|
|
49
|
+
|
|
50
|
+
```python
|
|
51
|
+
from mp3_cover_adder import MP3CoverAdder
|
|
52
|
+
|
|
53
|
+
# Read and modify an MP3 file
|
|
54
|
+
mp3 = MP3CoverAdder("song.mp3")
|
|
55
|
+
mp3.set_cover("cover.jpg")
|
|
56
|
+
mp3.set_metadata(title="My Song", artist="Artist Name", album="Album Title")
|
|
57
|
+
mp3.write("output.mp3")
|
|
58
|
+
|
|
59
|
+
# Or work with bytes in memory
|
|
60
|
+
result_bytes = mp3.to_bytes()
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
## API
|
|
64
|
+
|
|
65
|
+
### Class API
|
|
66
|
+
|
|
67
|
+
| Method | Description |
|
|
68
|
+
| --- | --- |
|
|
69
|
+
| `MP3CoverAdder(file_path)` | Load MP3 from path, bytes, or BytesIO |
|
|
70
|
+
| `.get_metadata()` | Returns `dict` with title, artist, album |
|
|
71
|
+
| `.get_cover()` | Returns cover image as `bytes` or `None` |
|
|
72
|
+
| `.set_cover(image, mime_type=None)` | Set/replace cover from path or bytes (JPEG/PNG) |
|
|
73
|
+
| `.remove_cover()` | Remove all album art |
|
|
74
|
+
| `.set_metadata(title=None, artist=None, album=None)` | Update ID3 text fields |
|
|
75
|
+
| `.write(output_path=None)` | Save to file (or modify in-place if path is `None`) |
|
|
76
|
+
| `.to_bytes()` | Get modified MP3 as bytes without writing |
|
|
77
|
+
|
|
78
|
+
### Convenience functions
|
|
79
|
+
|
|
80
|
+
| Function | Role |
|
|
81
|
+
| --- | --- |
|
|
82
|
+
| `read_mp3_metadata(file)` | Read metadata + check for cover art |
|
|
83
|
+
| `set_mp3_cover(input, image, output=None)` | Add cover (returns bytes if `output` is `None`) |
|
|
84
|
+
| `remove_mp3_cover(input, output=None)` | Remove cover (returns bytes if `output` is `None`) |
|
|
85
|
+
| `set_mp3_metadata(input, output=None, ...)` | Update metadata (returns bytes if `output` is `None`) |
|
|
86
|
+
| `write_mp3(input, output, image=None, ...)` | One-shot: write MP3 with optional cover + metadata |
|
|
87
|
+
|
|
88
|
+
## Examples
|
|
89
|
+
|
|
90
|
+
### Read metadata and cover
|
|
91
|
+
|
|
92
|
+
```python
|
|
93
|
+
from mp3_cover_adder import read_mp3_metadata, MP3CoverAdder
|
|
94
|
+
|
|
95
|
+
# Quick check
|
|
96
|
+
info = read_mp3_metadata("song.mp3")
|
|
97
|
+
print(info) # {'title': '...', 'artist': '...', 'album': '...', 'has_cover': True}
|
|
98
|
+
|
|
99
|
+
# Extract cover
|
|
100
|
+
mp3 = MP3CoverAdder("song.mp3")
|
|
101
|
+
cover_bytes = mp3.get_cover()
|
|
102
|
+
if cover_bytes:
|
|
103
|
+
with open("extracted_cover.jpg", "wb") as f:
|
|
104
|
+
f.write(cover_bytes)
|
|
105
|
+
```
|
|
106
|
+
|
|
107
|
+
### Add or replace cover art
|
|
108
|
+
|
|
109
|
+
```python
|
|
110
|
+
from mp3_cover_adder import set_mp3_cover
|
|
111
|
+
|
|
112
|
+
# From file path (MIME type auto-detected from extension)
|
|
113
|
+
set_mp3_cover("input.mp3", "cover.jpg", output_path="output.mp3")
|
|
114
|
+
|
|
115
|
+
# From bytes in memory (MIME type auto-detected from magic bytes)
|
|
116
|
+
with open("cover.png", "rb") as f:
|
|
117
|
+
cover_data = f.read()
|
|
118
|
+
|
|
119
|
+
result = set_mp3_cover("input.mp3", cover_data) # returns bytes
|
|
120
|
+
with open("output.mp3", "wb") as f:
|
|
121
|
+
f.write(result)
|
|
122
|
+
```
|
|
123
|
+
|
|
124
|
+
### Update metadata only
|
|
125
|
+
|
|
126
|
+
```python
|
|
127
|
+
from mp3_cover_adder import MP3CoverAdder
|
|
128
|
+
|
|
129
|
+
mp3 = MP3CoverAdder("song.mp3")
|
|
130
|
+
mp3.set_metadata(
|
|
131
|
+
title="Updated Title",
|
|
132
|
+
artist="New Artist",
|
|
133
|
+
album="New Album"
|
|
134
|
+
)
|
|
135
|
+
mp3.write() # modify in-place
|
|
136
|
+
```
|
|
137
|
+
|
|
138
|
+
### Remove album art
|
|
139
|
+
|
|
140
|
+
```python
|
|
141
|
+
from mp3_cover_adder import remove_mp3_cover
|
|
142
|
+
|
|
143
|
+
remove_mp3_cover("input.mp3", output_path="output_no_cover.mp3")
|
|
144
|
+
```
|
|
145
|
+
|
|
146
|
+
### All-in-one convenience
|
|
147
|
+
|
|
148
|
+
```python
|
|
149
|
+
from mp3_cover_adder import write_mp3
|
|
150
|
+
|
|
151
|
+
write_mp3(
|
|
152
|
+
input_path="input.mp3",
|
|
153
|
+
output_path="output.mp3",
|
|
154
|
+
image_source="cover.jpg",
|
|
155
|
+
title="Song Title",
|
|
156
|
+
artist="Artist Name",
|
|
157
|
+
album="Album Name"
|
|
158
|
+
)
|
|
159
|
+
```
|
|
160
|
+
|
|
161
|
+
## Tests
|
|
162
|
+
|
|
163
|
+
```bash
|
|
164
|
+
pytest
|
|
165
|
+
# or with coverage
|
|
166
|
+
pytest --cov=mp3_cover_adder
|
|
167
|
+
```
|
|
168
|
+
|
|
169
|
+
## Development
|
|
170
|
+
|
|
171
|
+
```bash
|
|
172
|
+
# Install dev dependencies
|
|
173
|
+
pip install -e ".[dev]"
|
|
174
|
+
|
|
175
|
+
# Run tests
|
|
176
|
+
pytest
|
|
177
|
+
|
|
178
|
+
# Format code
|
|
179
|
+
black src/ tests/
|
|
180
|
+
|
|
181
|
+
# Lint
|
|
182
|
+
ruff check src/ tests/
|
|
183
|
+
```
|
|
184
|
+
|
|
185
|
+
## License
|
|
186
|
+
|
|
187
|
+
MIT. See [LICENSE](./LICENSE).
|
|
@@ -0,0 +1,154 @@
|
|
|
1
|
+
# mp3-cover-adder
|
|
2
|
+
|
|
3
|
+
[**Add Cover to MP3**](https://www.uwarp.design/mp3-cover-adder) — free browser tool for embedding album art and editing ID3 tags (the reference UI for this library).
|
|
4
|
+
|
|
5
|
+
Offline Python library for embedding album art and editing ID3 metadata in MP3 files.
|
|
6
|
+
|
|
7
|
+
Package: **`mp3-cover-adder`** on PyPI (`0.1.0`).
|
|
8
|
+
|
|
9
|
+
## Install
|
|
10
|
+
|
|
11
|
+
```bash
|
|
12
|
+
pip install mp3-cover-adder
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
## Quick start
|
|
16
|
+
|
|
17
|
+
```python
|
|
18
|
+
from mp3_cover_adder import MP3CoverAdder
|
|
19
|
+
|
|
20
|
+
# Read and modify an MP3 file
|
|
21
|
+
mp3 = MP3CoverAdder("song.mp3")
|
|
22
|
+
mp3.set_cover("cover.jpg")
|
|
23
|
+
mp3.set_metadata(title="My Song", artist="Artist Name", album="Album Title")
|
|
24
|
+
mp3.write("output.mp3")
|
|
25
|
+
|
|
26
|
+
# Or work with bytes in memory
|
|
27
|
+
result_bytes = mp3.to_bytes()
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
## API
|
|
31
|
+
|
|
32
|
+
### Class API
|
|
33
|
+
|
|
34
|
+
| Method | Description |
|
|
35
|
+
| --- | --- |
|
|
36
|
+
| `MP3CoverAdder(file_path)` | Load MP3 from path, bytes, or BytesIO |
|
|
37
|
+
| `.get_metadata()` | Returns `dict` with title, artist, album |
|
|
38
|
+
| `.get_cover()` | Returns cover image as `bytes` or `None` |
|
|
39
|
+
| `.set_cover(image, mime_type=None)` | Set/replace cover from path or bytes (JPEG/PNG) |
|
|
40
|
+
| `.remove_cover()` | Remove all album art |
|
|
41
|
+
| `.set_metadata(title=None, artist=None, album=None)` | Update ID3 text fields |
|
|
42
|
+
| `.write(output_path=None)` | Save to file (or modify in-place if path is `None`) |
|
|
43
|
+
| `.to_bytes()` | Get modified MP3 as bytes without writing |
|
|
44
|
+
|
|
45
|
+
### Convenience functions
|
|
46
|
+
|
|
47
|
+
| Function | Role |
|
|
48
|
+
| --- | --- |
|
|
49
|
+
| `read_mp3_metadata(file)` | Read metadata + check for cover art |
|
|
50
|
+
| `set_mp3_cover(input, image, output=None)` | Add cover (returns bytes if `output` is `None`) |
|
|
51
|
+
| `remove_mp3_cover(input, output=None)` | Remove cover (returns bytes if `output` is `None`) |
|
|
52
|
+
| `set_mp3_metadata(input, output=None, ...)` | Update metadata (returns bytes if `output` is `None`) |
|
|
53
|
+
| `write_mp3(input, output, image=None, ...)` | One-shot: write MP3 with optional cover + metadata |
|
|
54
|
+
|
|
55
|
+
## Examples
|
|
56
|
+
|
|
57
|
+
### Read metadata and cover
|
|
58
|
+
|
|
59
|
+
```python
|
|
60
|
+
from mp3_cover_adder import read_mp3_metadata, MP3CoverAdder
|
|
61
|
+
|
|
62
|
+
# Quick check
|
|
63
|
+
info = read_mp3_metadata("song.mp3")
|
|
64
|
+
print(info) # {'title': '...', 'artist': '...', 'album': '...', 'has_cover': True}
|
|
65
|
+
|
|
66
|
+
# Extract cover
|
|
67
|
+
mp3 = MP3CoverAdder("song.mp3")
|
|
68
|
+
cover_bytes = mp3.get_cover()
|
|
69
|
+
if cover_bytes:
|
|
70
|
+
with open("extracted_cover.jpg", "wb") as f:
|
|
71
|
+
f.write(cover_bytes)
|
|
72
|
+
```
|
|
73
|
+
|
|
74
|
+
### Add or replace cover art
|
|
75
|
+
|
|
76
|
+
```python
|
|
77
|
+
from mp3_cover_adder import set_mp3_cover
|
|
78
|
+
|
|
79
|
+
# From file path (MIME type auto-detected from extension)
|
|
80
|
+
set_mp3_cover("input.mp3", "cover.jpg", output_path="output.mp3")
|
|
81
|
+
|
|
82
|
+
# From bytes in memory (MIME type auto-detected from magic bytes)
|
|
83
|
+
with open("cover.png", "rb") as f:
|
|
84
|
+
cover_data = f.read()
|
|
85
|
+
|
|
86
|
+
result = set_mp3_cover("input.mp3", cover_data) # returns bytes
|
|
87
|
+
with open("output.mp3", "wb") as f:
|
|
88
|
+
f.write(result)
|
|
89
|
+
```
|
|
90
|
+
|
|
91
|
+
### Update metadata only
|
|
92
|
+
|
|
93
|
+
```python
|
|
94
|
+
from mp3_cover_adder import MP3CoverAdder
|
|
95
|
+
|
|
96
|
+
mp3 = MP3CoverAdder("song.mp3")
|
|
97
|
+
mp3.set_metadata(
|
|
98
|
+
title="Updated Title",
|
|
99
|
+
artist="New Artist",
|
|
100
|
+
album="New Album"
|
|
101
|
+
)
|
|
102
|
+
mp3.write() # modify in-place
|
|
103
|
+
```
|
|
104
|
+
|
|
105
|
+
### Remove album art
|
|
106
|
+
|
|
107
|
+
```python
|
|
108
|
+
from mp3_cover_adder import remove_mp3_cover
|
|
109
|
+
|
|
110
|
+
remove_mp3_cover("input.mp3", output_path="output_no_cover.mp3")
|
|
111
|
+
```
|
|
112
|
+
|
|
113
|
+
### All-in-one convenience
|
|
114
|
+
|
|
115
|
+
```python
|
|
116
|
+
from mp3_cover_adder import write_mp3
|
|
117
|
+
|
|
118
|
+
write_mp3(
|
|
119
|
+
input_path="input.mp3",
|
|
120
|
+
output_path="output.mp3",
|
|
121
|
+
image_source="cover.jpg",
|
|
122
|
+
title="Song Title",
|
|
123
|
+
artist="Artist Name",
|
|
124
|
+
album="Album Name"
|
|
125
|
+
)
|
|
126
|
+
```
|
|
127
|
+
|
|
128
|
+
## Tests
|
|
129
|
+
|
|
130
|
+
```bash
|
|
131
|
+
pytest
|
|
132
|
+
# or with coverage
|
|
133
|
+
pytest --cov=mp3_cover_adder
|
|
134
|
+
```
|
|
135
|
+
|
|
136
|
+
## Development
|
|
137
|
+
|
|
138
|
+
```bash
|
|
139
|
+
# Install dev dependencies
|
|
140
|
+
pip install -e ".[dev]"
|
|
141
|
+
|
|
142
|
+
# Run tests
|
|
143
|
+
pytest
|
|
144
|
+
|
|
145
|
+
# Format code
|
|
146
|
+
black src/ tests/
|
|
147
|
+
|
|
148
|
+
# Lint
|
|
149
|
+
ruff check src/ tests/
|
|
150
|
+
```
|
|
151
|
+
|
|
152
|
+
## License
|
|
153
|
+
|
|
154
|
+
MIT. See [LICENSE](./LICENSE).
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
"""Basic usage examples for mp3-cover-adder library."""
|
|
2
|
+
|
|
3
|
+
from mp3_cover_adder import MP3CoverAdder, write_mp3, read_mp3_metadata
|
|
4
|
+
|
|
5
|
+
# Example 1: Read metadata from an MP3 file
|
|
6
|
+
print("Example 1: Read metadata")
|
|
7
|
+
print("-" * 40)
|
|
8
|
+
# info = read_mp3_metadata("song.mp3")
|
|
9
|
+
# print(f"Title: {info['title']}")
|
|
10
|
+
# print(f"Artist: {info['artist']}")
|
|
11
|
+
# print(f"Album: {info['album']}")
|
|
12
|
+
# print(f"Has cover: {info['has_cover']}")
|
|
13
|
+
|
|
14
|
+
# Example 2: Add cover art to an MP3 file
|
|
15
|
+
print("\nExample 2: Add cover art")
|
|
16
|
+
print("-" * 40)
|
|
17
|
+
# mp3 = MP3CoverAdder("input.mp3")
|
|
18
|
+
# mp3.set_cover("cover.jpg")
|
|
19
|
+
# mp3.write("output.mp3")
|
|
20
|
+
# print("Cover art added successfully!")
|
|
21
|
+
|
|
22
|
+
# Example 3: Set metadata
|
|
23
|
+
print("\nExample 3: Set metadata")
|
|
24
|
+
print("-" * 40)
|
|
25
|
+
# mp3 = MP3CoverAdder("song.mp3")
|
|
26
|
+
# mp3.set_metadata(
|
|
27
|
+
# title="My Song Title",
|
|
28
|
+
# artist="Artist Name",
|
|
29
|
+
# album="Album Name"
|
|
30
|
+
# )
|
|
31
|
+
# mp3.write() # modify in-place
|
|
32
|
+
# print("Metadata updated successfully!")
|
|
33
|
+
|
|
34
|
+
# Example 4: All-in-one convenience function
|
|
35
|
+
print("\nExample 4: All-in-one")
|
|
36
|
+
print("-" * 40)
|
|
37
|
+
# write_mp3(
|
|
38
|
+
# input_path="input.mp3",
|
|
39
|
+
# output_path="output.mp3",
|
|
40
|
+
# image_source="cover.jpg",
|
|
41
|
+
# title="Song Title",
|
|
42
|
+
# artist="Artist Name",
|
|
43
|
+
# album="Album Name"
|
|
44
|
+
# )
|
|
45
|
+
# print("MP3 file created with cover and metadata!")
|
|
46
|
+
|
|
47
|
+
# Example 5: Work with bytes in memory
|
|
48
|
+
print("\nExample 5: Work with bytes")
|
|
49
|
+
print("-" * 40)
|
|
50
|
+
# with open("input.mp3", "rb") as f:
|
|
51
|
+
# mp3_bytes = f.read()
|
|
52
|
+
#
|
|
53
|
+
# mp3 = MP3CoverAdder(mp3_bytes)
|
|
54
|
+
# mp3.set_metadata(title="New Title")
|
|
55
|
+
# result_bytes = mp3.to_bytes()
|
|
56
|
+
#
|
|
57
|
+
# with open("output.mp3", "wb") as f:
|
|
58
|
+
# f.write(result_bytes)
|
|
59
|
+
# print("MP3 processed in memory!")
|
|
60
|
+
|
|
61
|
+
print("\n✅ All examples completed!")
|
|
62
|
+
print("Uncomment the code blocks to run with real MP3 files.")
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["hatchling"]
|
|
3
|
+
build-backend = "hatchling.build"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "mp3-cover-adder"
|
|
7
|
+
version = "0.1.0"
|
|
8
|
+
description = "Python library to embed MP3 album art and edit ID3 tags"
|
|
9
|
+
readme = "README.md"
|
|
10
|
+
requires-python = ">=3.8"
|
|
11
|
+
license = "MIT"
|
|
12
|
+
authors = [
|
|
13
|
+
{ name = "Sherotree", email = "sherotree@users.noreply.github.com" }
|
|
14
|
+
]
|
|
15
|
+
keywords = ["mp3", "id3", "cover-art", "album-art", "metadata", "audio"]
|
|
16
|
+
classifiers = [
|
|
17
|
+
"Development Status :: 4 - Beta",
|
|
18
|
+
"Intended Audience :: Developers",
|
|
19
|
+
"License :: OSI Approved :: MIT License",
|
|
20
|
+
"Programming Language :: Python :: 3",
|
|
21
|
+
"Programming Language :: Python :: 3.8",
|
|
22
|
+
"Programming Language :: Python :: 3.9",
|
|
23
|
+
"Programming Language :: Python :: 3.10",
|
|
24
|
+
"Programming Language :: Python :: 3.11",
|
|
25
|
+
"Programming Language :: Python :: 3.12",
|
|
26
|
+
"Topic :: Multimedia :: Sound/Audio",
|
|
27
|
+
]
|
|
28
|
+
dependencies = [
|
|
29
|
+
"mutagen>=1.45.0",
|
|
30
|
+
]
|
|
31
|
+
|
|
32
|
+
[project.urls]
|
|
33
|
+
Homepage = "https://github.com/sherotree/mp3-cover-adder-py"
|
|
34
|
+
Repository = "https://github.com/sherotree/mp3-cover-adder-py"
|
|
35
|
+
"Web Tool" = "https://www.uwarp.design/mp3-cover-adder"
|
|
36
|
+
|
|
37
|
+
[project.optional-dependencies]
|
|
38
|
+
test = [
|
|
39
|
+
"pytest>=7.0.0",
|
|
40
|
+
"pytest-cov>=4.0.0",
|
|
41
|
+
]
|
|
42
|
+
dev = [
|
|
43
|
+
"mp3-cover-adder[test]",
|
|
44
|
+
"black>=23.0.0",
|
|
45
|
+
"ruff>=0.1.0",
|
|
46
|
+
]
|
|
47
|
+
|
|
48
|
+
[tool.hatch.build.targets.wheel]
|
|
49
|
+
packages = ["src/mp3_cover_adder"]
|
|
50
|
+
|
|
51
|
+
[tool.pytest.ini_options]
|
|
52
|
+
testpaths = ["tests"]
|
|
53
|
+
python_files = ["test_*.py"]
|
|
54
|
+
python_classes = ["Test*"]
|
|
55
|
+
python_functions = ["test_*"]
|
|
56
|
+
addopts = "-v --tb=short"
|
|
57
|
+
|
|
58
|
+
[tool.black]
|
|
59
|
+
line-length = 100
|
|
60
|
+
target-version = ["py38"]
|
|
61
|
+
|
|
62
|
+
[tool.ruff]
|
|
63
|
+
line-length = 100
|
|
64
|
+
target-version = "py38"
|
|
65
|
+
select = ["E", "F", "I", "UP"]
|
|
66
|
+
ignore = []
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
"""
|
|
2
|
+
mp3-cover-adder: Python library to embed MP3 album art and edit ID3 tags.
|
|
3
|
+
|
|
4
|
+
This library provides a simple, offline API for reading and writing
|
|
5
|
+
ID3 metadata and album art in MP3 files.
|
|
6
|
+
"""
|
|
7
|
+
|
|
8
|
+
from mp3_cover_adder.core import (
|
|
9
|
+
MP3CoverAdder,
|
|
10
|
+
read_mp3_metadata,
|
|
11
|
+
set_mp3_cover,
|
|
12
|
+
remove_mp3_cover,
|
|
13
|
+
set_mp3_metadata,
|
|
14
|
+
write_mp3,
|
|
15
|
+
)
|
|
16
|
+
|
|
17
|
+
__version__ = "0.1.0"
|
|
18
|
+
__all__ = [
|
|
19
|
+
"MP3CoverAdder",
|
|
20
|
+
"read_mp3_metadata",
|
|
21
|
+
"set_mp3_cover",
|
|
22
|
+
"remove_mp3_cover",
|
|
23
|
+
"set_mp3_metadata",
|
|
24
|
+
"write_mp3",
|
|
25
|
+
]
|
|
@@ -0,0 +1,334 @@
|
|
|
1
|
+
"""Core functionality for MP3 cover art and metadata manipulation."""
|
|
2
|
+
|
|
3
|
+
from pathlib import Path
|
|
4
|
+
from typing import Optional, Union, Dict, Any
|
|
5
|
+
from io import BytesIO
|
|
6
|
+
import shutil
|
|
7
|
+
import tempfile
|
|
8
|
+
import os
|
|
9
|
+
|
|
10
|
+
from mutagen.mp3 import MP3
|
|
11
|
+
from mutagen.id3 import ID3, APIC, TIT2, TPE1, TALB, PictureType
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
class MP3CoverAdder:
|
|
15
|
+
"""
|
|
16
|
+
Main class for manipulating MP3 files with cover art and ID3 metadata.
|
|
17
|
+
|
|
18
|
+
Example:
|
|
19
|
+
>>> mp3 = MP3CoverAdder("song.mp3")
|
|
20
|
+
>>> mp3.set_cover("cover.jpg")
|
|
21
|
+
>>> mp3.set_metadata(title="My Song", artist="Artist Name")
|
|
22
|
+
>>> mp3.write("output.mp3")
|
|
23
|
+
"""
|
|
24
|
+
|
|
25
|
+
def __init__(self, file_path: Union[str, Path, bytes, BytesIO]):
|
|
26
|
+
"""
|
|
27
|
+
Initialize MP3CoverAdder with an MP3 file.
|
|
28
|
+
|
|
29
|
+
Args:
|
|
30
|
+
file_path: Path to MP3 file, bytes, or BytesIO object
|
|
31
|
+
"""
|
|
32
|
+
self._original_data = None
|
|
33
|
+
self._is_fileobj = False
|
|
34
|
+
|
|
35
|
+
if isinstance(file_path, bytes):
|
|
36
|
+
self._original_data = file_path
|
|
37
|
+
file_path = BytesIO(file_path)
|
|
38
|
+
self._is_fileobj = True
|
|
39
|
+
elif isinstance(file_path, BytesIO):
|
|
40
|
+
self._original_data = file_path.getvalue()
|
|
41
|
+
self._is_fileobj = True
|
|
42
|
+
|
|
43
|
+
self.audio = MP3(file_path)
|
|
44
|
+
if self.audio.tags is None:
|
|
45
|
+
self.audio.add_tags()
|
|
46
|
+
|
|
47
|
+
self._file_path = file_path
|
|
48
|
+
|
|
49
|
+
def get_metadata(self) -> Dict[str, Optional[str]]:
|
|
50
|
+
"""
|
|
51
|
+
Read current ID3 metadata from the MP3 file.
|
|
52
|
+
|
|
53
|
+
Returns:
|
|
54
|
+
Dictionary containing title, artist, and album
|
|
55
|
+
"""
|
|
56
|
+
tags = self.audio.tags
|
|
57
|
+
return {
|
|
58
|
+
"title": str(tags.get("TIT2", "")) if tags.get("TIT2") else None,
|
|
59
|
+
"artist": str(tags.get("TPE1", "")) if tags.get("TPE1") else None,
|
|
60
|
+
"album": str(tags.get("TALB", "")) if tags.get("TALB") else None,
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
def get_cover(self) -> Optional[bytes]:
|
|
64
|
+
"""
|
|
65
|
+
Extract the album art from the MP3 file.
|
|
66
|
+
|
|
67
|
+
Returns:
|
|
68
|
+
Cover image as bytes, or None if no cover exists
|
|
69
|
+
"""
|
|
70
|
+
tags = self.audio.tags
|
|
71
|
+
if tags is None:
|
|
72
|
+
return None
|
|
73
|
+
|
|
74
|
+
for key in tags.keys():
|
|
75
|
+
if key.startswith("APIC:"):
|
|
76
|
+
return tags[key].data
|
|
77
|
+
|
|
78
|
+
return None
|
|
79
|
+
|
|
80
|
+
def set_cover(
|
|
81
|
+
self,
|
|
82
|
+
image_source: Union[str, Path, bytes],
|
|
83
|
+
mime_type: Optional[str] = None,
|
|
84
|
+
description: str = "Cover",
|
|
85
|
+
) -> None:
|
|
86
|
+
"""
|
|
87
|
+
Set or replace the album art in the MP3 file.
|
|
88
|
+
|
|
89
|
+
Args:
|
|
90
|
+
image_source: Path to image file or image bytes (JPEG/PNG)
|
|
91
|
+
mime_type: MIME type (e.g., "image/jpeg"). Auto-detected if not provided.
|
|
92
|
+
description: Description for the cover art (default: "Cover")
|
|
93
|
+
"""
|
|
94
|
+
if isinstance(image_source, (str, Path)):
|
|
95
|
+
with open(image_source, "rb") as f:
|
|
96
|
+
image_data = f.read()
|
|
97
|
+
|
|
98
|
+
if mime_type is None:
|
|
99
|
+
ext = Path(image_source).suffix.lower()
|
|
100
|
+
mime_type = {
|
|
101
|
+
".jpg": "image/jpeg",
|
|
102
|
+
".jpeg": "image/jpeg",
|
|
103
|
+
".png": "image/png",
|
|
104
|
+
".gif": "image/gif",
|
|
105
|
+
".bmp": "image/bmp",
|
|
106
|
+
}.get(ext, "image/jpeg")
|
|
107
|
+
else:
|
|
108
|
+
image_data = image_source
|
|
109
|
+
if mime_type is None:
|
|
110
|
+
if image_data.startswith(b"\xff\xd8"):
|
|
111
|
+
mime_type = "image/jpeg"
|
|
112
|
+
elif image_data.startswith(b"\x89PNG"):
|
|
113
|
+
mime_type = "image/png"
|
|
114
|
+
elif image_data.startswith(b"GIF"):
|
|
115
|
+
mime_type = "image/gif"
|
|
116
|
+
else:
|
|
117
|
+
mime_type = "image/jpeg"
|
|
118
|
+
|
|
119
|
+
self.audio.tags.delall("APIC")
|
|
120
|
+
self.audio.tags.add(
|
|
121
|
+
APIC(
|
|
122
|
+
encoding=3,
|
|
123
|
+
mime=mime_type,
|
|
124
|
+
type=PictureType.COVER_FRONT,
|
|
125
|
+
desc=description,
|
|
126
|
+
data=image_data,
|
|
127
|
+
)
|
|
128
|
+
)
|
|
129
|
+
|
|
130
|
+
def remove_cover(self) -> None:
|
|
131
|
+
"""Remove all album art from the MP3 file."""
|
|
132
|
+
self.audio.tags.delall("APIC")
|
|
133
|
+
|
|
134
|
+
def set_metadata(
|
|
135
|
+
self,
|
|
136
|
+
title: Optional[str] = None,
|
|
137
|
+
artist: Optional[str] = None,
|
|
138
|
+
album: Optional[str] = None,
|
|
139
|
+
) -> None:
|
|
140
|
+
"""
|
|
141
|
+
Set ID3 metadata fields.
|
|
142
|
+
|
|
143
|
+
Args:
|
|
144
|
+
title: Song title
|
|
145
|
+
artist: Artist name
|
|
146
|
+
album: Album name
|
|
147
|
+
"""
|
|
148
|
+
if title is not None:
|
|
149
|
+
self.audio.tags.delall("TIT2")
|
|
150
|
+
self.audio.tags.add(TIT2(encoding=3, text=title))
|
|
151
|
+
|
|
152
|
+
if artist is not None:
|
|
153
|
+
self.audio.tags.delall("TPE1")
|
|
154
|
+
self.audio.tags.add(TPE1(encoding=3, text=artist))
|
|
155
|
+
|
|
156
|
+
if album is not None:
|
|
157
|
+
self.audio.tags.delall("TALB")
|
|
158
|
+
self.audio.tags.add(TALB(encoding=3, text=album))
|
|
159
|
+
|
|
160
|
+
def write(self, output_path: Optional[Union[str, Path]] = None) -> None:
|
|
161
|
+
"""
|
|
162
|
+
Write changes to the MP3 file.
|
|
163
|
+
|
|
164
|
+
Args:
|
|
165
|
+
output_path: Path for output file. If None, modifies the original file.
|
|
166
|
+
"""
|
|
167
|
+
if output_path is None:
|
|
168
|
+
self.audio.save()
|
|
169
|
+
else:
|
|
170
|
+
output_path = Path(output_path)
|
|
171
|
+
output_path.parent.mkdir(parents=True, exist_ok=True)
|
|
172
|
+
|
|
173
|
+
if isinstance(self._file_path, (str, Path)):
|
|
174
|
+
shutil.copy2(self._file_path, output_path)
|
|
175
|
+
elif self._original_data is not None:
|
|
176
|
+
output_path.write_bytes(self._original_data)
|
|
177
|
+
|
|
178
|
+
new_mp3 = MP3(output_path)
|
|
179
|
+
if new_mp3.tags is None:
|
|
180
|
+
new_mp3.add_tags()
|
|
181
|
+
|
|
182
|
+
new_mp3.tags.clear()
|
|
183
|
+
for key, value in self.audio.tags.items():
|
|
184
|
+
new_mp3.tags[key] = value
|
|
185
|
+
|
|
186
|
+
new_mp3.save()
|
|
187
|
+
|
|
188
|
+
def to_bytes(self) -> bytes:
|
|
189
|
+
"""
|
|
190
|
+
Get the modified MP3 as bytes without writing to disk.
|
|
191
|
+
|
|
192
|
+
Returns:
|
|
193
|
+
MP3 file content as bytes
|
|
194
|
+
"""
|
|
195
|
+
with tempfile.NamedTemporaryFile(mode='wb', suffix='.mp3', delete=False) as tmp:
|
|
196
|
+
tmp_path = tmp.name
|
|
197
|
+
|
|
198
|
+
try:
|
|
199
|
+
self.write(tmp_path)
|
|
200
|
+
with open(tmp_path, 'rb') as f:
|
|
201
|
+
return f.read()
|
|
202
|
+
finally:
|
|
203
|
+
if os.path.exists(tmp_path):
|
|
204
|
+
os.unlink(tmp_path)
|
|
205
|
+
|
|
206
|
+
|
|
207
|
+
def read_mp3_metadata(file_path: Union[str, Path, bytes, BytesIO]) -> Dict[str, Any]:
|
|
208
|
+
"""
|
|
209
|
+
Read ID3 metadata and cover art from an MP3 file (convenience function).
|
|
210
|
+
|
|
211
|
+
Args:
|
|
212
|
+
file_path: Path to MP3 file, bytes, or BytesIO object
|
|
213
|
+
|
|
214
|
+
Returns:
|
|
215
|
+
Dictionary with 'title', 'artist', 'album', and 'has_cover' keys
|
|
216
|
+
"""
|
|
217
|
+
mp3 = MP3CoverAdder(file_path)
|
|
218
|
+
metadata = mp3.get_metadata()
|
|
219
|
+
metadata["has_cover"] = mp3.get_cover() is not None
|
|
220
|
+
return metadata
|
|
221
|
+
|
|
222
|
+
|
|
223
|
+
def set_mp3_cover(
|
|
224
|
+
input_path: Union[str, Path, bytes, BytesIO],
|
|
225
|
+
image_source: Union[str, Path, bytes],
|
|
226
|
+
output_path: Optional[Union[str, Path]] = None,
|
|
227
|
+
mime_type: Optional[str] = None,
|
|
228
|
+
) -> Optional[bytes]:
|
|
229
|
+
"""
|
|
230
|
+
Set cover art on an MP3 file (convenience function).
|
|
231
|
+
|
|
232
|
+
Args:
|
|
233
|
+
input_path: Path to input MP3 file, bytes, or BytesIO
|
|
234
|
+
image_source: Path to image file or image bytes
|
|
235
|
+
output_path: Path for output file. If None, returns bytes instead.
|
|
236
|
+
mime_type: MIME type (auto-detected if not provided)
|
|
237
|
+
|
|
238
|
+
Returns:
|
|
239
|
+
MP3 bytes if output_path is None, otherwise None
|
|
240
|
+
"""
|
|
241
|
+
mp3 = MP3CoverAdder(input_path)
|
|
242
|
+
mp3.set_cover(image_source, mime_type=mime_type)
|
|
243
|
+
|
|
244
|
+
if output_path is None:
|
|
245
|
+
return mp3.to_bytes()
|
|
246
|
+
else:
|
|
247
|
+
mp3.write(output_path)
|
|
248
|
+
return None
|
|
249
|
+
|
|
250
|
+
|
|
251
|
+
def remove_mp3_cover(
|
|
252
|
+
input_path: Union[str, Path, bytes, BytesIO],
|
|
253
|
+
output_path: Optional[Union[str, Path]] = None,
|
|
254
|
+
) -> Optional[bytes]:
|
|
255
|
+
"""
|
|
256
|
+
Remove cover art from an MP3 file (convenience function).
|
|
257
|
+
|
|
258
|
+
Args:
|
|
259
|
+
input_path: Path to input MP3 file, bytes, or BytesIO
|
|
260
|
+
output_path: Path for output file. If None, returns bytes instead.
|
|
261
|
+
|
|
262
|
+
Returns:
|
|
263
|
+
MP3 bytes if output_path is None, otherwise None
|
|
264
|
+
"""
|
|
265
|
+
mp3 = MP3CoverAdder(input_path)
|
|
266
|
+
mp3.remove_cover()
|
|
267
|
+
|
|
268
|
+
if output_path is None:
|
|
269
|
+
return mp3.to_bytes()
|
|
270
|
+
else:
|
|
271
|
+
mp3.write(output_path)
|
|
272
|
+
return None
|
|
273
|
+
|
|
274
|
+
|
|
275
|
+
def set_mp3_metadata(
|
|
276
|
+
input_path: Union[str, Path, bytes, BytesIO],
|
|
277
|
+
output_path: Optional[Union[str, Path]] = None,
|
|
278
|
+
title: Optional[str] = None,
|
|
279
|
+
artist: Optional[str] = None,
|
|
280
|
+
album: Optional[str] = None,
|
|
281
|
+
) -> Optional[bytes]:
|
|
282
|
+
"""
|
|
283
|
+
Set ID3 metadata on an MP3 file (convenience function).
|
|
284
|
+
|
|
285
|
+
Args:
|
|
286
|
+
input_path: Path to input MP3 file, bytes, or BytesIO
|
|
287
|
+
output_path: Path for output file. If None, returns bytes instead.
|
|
288
|
+
title: Song title
|
|
289
|
+
artist: Artist name
|
|
290
|
+
album: Album name
|
|
291
|
+
|
|
292
|
+
Returns:
|
|
293
|
+
MP3 bytes if output_path is None, otherwise None
|
|
294
|
+
"""
|
|
295
|
+
mp3 = MP3CoverAdder(input_path)
|
|
296
|
+
mp3.set_metadata(title=title, artist=artist, album=album)
|
|
297
|
+
|
|
298
|
+
if output_path is None:
|
|
299
|
+
return mp3.to_bytes()
|
|
300
|
+
else:
|
|
301
|
+
mp3.write(output_path)
|
|
302
|
+
return None
|
|
303
|
+
|
|
304
|
+
|
|
305
|
+
def write_mp3(
|
|
306
|
+
input_path: Union[str, Path, bytes, BytesIO],
|
|
307
|
+
output_path: Union[str, Path],
|
|
308
|
+
image_source: Optional[Union[str, Path, bytes]] = None,
|
|
309
|
+
title: Optional[str] = None,
|
|
310
|
+
artist: Optional[str] = None,
|
|
311
|
+
album: Optional[str] = None,
|
|
312
|
+
mime_type: Optional[str] = None,
|
|
313
|
+
) -> None:
|
|
314
|
+
"""
|
|
315
|
+
Write an MP3 file with optional cover art and metadata (convenience function).
|
|
316
|
+
|
|
317
|
+
Args:
|
|
318
|
+
input_path: Path to input MP3 file, bytes, or BytesIO
|
|
319
|
+
output_path: Path for output file
|
|
320
|
+
image_source: Optional path to image file or image bytes
|
|
321
|
+
title: Optional song title
|
|
322
|
+
artist: Optional artist name
|
|
323
|
+
album: Optional album name
|
|
324
|
+
mime_type: Optional MIME type for image (auto-detected if not provided)
|
|
325
|
+
"""
|
|
326
|
+
mp3 = MP3CoverAdder(input_path)
|
|
327
|
+
|
|
328
|
+
if image_source is not None:
|
|
329
|
+
mp3.set_cover(image_source, mime_type=mime_type)
|
|
330
|
+
|
|
331
|
+
if title is not None or artist is not None or album is not None:
|
|
332
|
+
mp3.set_metadata(title=title, artist=artist, album=album)
|
|
333
|
+
|
|
334
|
+
mp3.write(output_path)
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
# Test suite for mp3-cover-adder
|
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
"""Pytest configuration and fixtures."""
|
|
2
|
+
|
|
3
|
+
import io
|
|
4
|
+
import struct
|
|
5
|
+
from pathlib import Path
|
|
6
|
+
import pytest
|
|
7
|
+
from mutagen.mp3 import MP3
|
|
8
|
+
from mutagen.id3 import ID3
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
@pytest.fixture
|
|
12
|
+
def test_mp3_bytes():
|
|
13
|
+
"""
|
|
14
|
+
Generate a minimal valid MP3 file as bytes.
|
|
15
|
+
Creates a proper MPEG Layer III frame that mutagen can parse.
|
|
16
|
+
"""
|
|
17
|
+
# Create valid MPEG-1 Layer III frames
|
|
18
|
+
# Frame sync (11 bits all set) + MPEG Version 1 + Layer III + no CRC
|
|
19
|
+
# Bitrate: 64 kbps (index 0100) + Sample rate: 44.1 kHz (index 00) + no padding + stereo
|
|
20
|
+
|
|
21
|
+
frames = []
|
|
22
|
+
for _ in range(30):
|
|
23
|
+
# Header bytes for MPEG-1 Layer III, 64kbps, 44.1kHz, stereo
|
|
24
|
+
header = bytes([
|
|
25
|
+
0xFF, # Sync word high byte
|
|
26
|
+
0xFB, # Sync word low + MPEG-1 + Layer III + no CRC
|
|
27
|
+
0x50, # Bitrate 64kbps + Sample rate 44.1kHz + no padding
|
|
28
|
+
0x00, # Stereo mode + no mode extension + no copyright + original
|
|
29
|
+
])
|
|
30
|
+
|
|
31
|
+
# For 64 kbps at 44.1 kHz: frame size = 144 * bitrate / samplerate = 144 * 64000 / 44100 = ~208 bytes
|
|
32
|
+
# Main data for the frame (204 bytes after the 4-byte header)
|
|
33
|
+
main_data = b"\x00" * 204
|
|
34
|
+
|
|
35
|
+
frames.append(header + main_data)
|
|
36
|
+
|
|
37
|
+
return b"".join(frames)
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
@pytest.fixture
|
|
41
|
+
def test_mp3_path(tmp_path, test_mp3_bytes):
|
|
42
|
+
"""Create a temporary MP3 file."""
|
|
43
|
+
mp3_file = tmp_path / "test.mp3"
|
|
44
|
+
mp3_file.write_bytes(test_mp3_bytes)
|
|
45
|
+
return mp3_file
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
@pytest.fixture
|
|
49
|
+
def test_jpeg_bytes():
|
|
50
|
+
"""
|
|
51
|
+
Generate minimal valid JPEG image bytes (1x1 red pixel).
|
|
52
|
+
"""
|
|
53
|
+
jpeg_data = (
|
|
54
|
+
b"\xff\xd8\xff\xe0\x00\x10JFIF\x00\x01\x01\x00\x00\x01\x00\x01\x00\x00"
|
|
55
|
+
b"\xff\xdb\x00C\x00\x08\x06\x06\x07\x06\x05\x08\x07\x07\x07\t\t\x08\n\x0c"
|
|
56
|
+
b"\x14\r\x0c\x0b\x0b\x0c\x19\x12\x13\x0f\x14\x1d\x1a\x1f\x1e\x1d\x1a\x1c"
|
|
57
|
+
b"\x1c $.\' \",#\x1c\x1c(7),01444\x1f\'9=82<.342"
|
|
58
|
+
b"\xff\xdb\x00C\x01\t\t\t\x0c\x0b\x0c\x18\r\r\x182!\x1c!22222222222222222222222222222222222222222222222222"
|
|
59
|
+
b"\xff\xc0\x00\x11\x08\x00\x01\x00\x01\x03\x01\"\x00\x02\x11\x01\x03\x11\x01"
|
|
60
|
+
b"\xff\xc4\x00\x1f\x00\x00\x01\x05\x01\x01\x01\x01\x01\x01\x00\x00\x00\x00"
|
|
61
|
+
b"\x00\x00\x00\x00\x01\x02\x03\x04\x05\x06\x07\x08\t\n\x0b"
|
|
62
|
+
b"\xff\xc4\x00\xb5\x10\x00\x02\x01\x03\x03\x02\x04\x03\x05\x05\x04\x04\x00"
|
|
63
|
+
b"\x00\x01}\x01\x02\x03\x00\x04\x11\x05\x12!1A\x06\x13Qa\x07\"q\x142\x81"
|
|
64
|
+
b"\x91\xa1\x08#B\xb1\xc1\x15R\xd1\xf0$3br\x82\t\n\x16\x17\x18\x19\x1a%&'"
|
|
65
|
+
b"()*456789:CDEFGHIJSTUVWXYZcdefghijstuvwxyz\x83\x84\x85\x86\x87\x88\x89"
|
|
66
|
+
b"\x8a\x92\x93\x94\x95\x96\x97\x98\x99\x9a\xa2\xa3\xa4\xa5\xa6\xa7\xa8\xa9"
|
|
67
|
+
b"\xaa\xb2\xb3\xb4\xb5\xb6\xb7\xb8\xb9\xba\xc2\xc3\xc4\xc5\xc6\xc7\xc8\xc9"
|
|
68
|
+
b"\xca\xd2\xd3\xd4\xd5\xd6\xd7\xd8\xd9\xda\xe1\xe2\xe3\xe4\xe5\xe6\xe7\xe8"
|
|
69
|
+
b"\xe9\xea\xf1\xf2\xf3\xf4\xf5\xf6\xf7\xf8\xf9\xfa"
|
|
70
|
+
b"\xff\xc4\x00\x1f\x01\x00\x03\x01\x01\x01\x01\x01\x01\x01\x01\x01\x00\x00"
|
|
71
|
+
b"\x00\x00\x00\x00\x01\x02\x03\x04\x05\x06\x07\x08\t\n\x0b"
|
|
72
|
+
b"\xff\xc4\x00\xb5\x11\x00\x02\x01\x02\x04\x04\x03\x04\x07\x05\x04\x04\x00"
|
|
73
|
+
b"\x01\x02w\x00\x01\x02\x03\x11\x04\x05!1\x06\x12AQ\x07aq\x13\"2\x81\x08"
|
|
74
|
+
b"\x14B\x91\xa1\xb1\xc1\t#3R\xf0\x15br\xd1\n\x16$4\xe1%\xf1\x17\x18\x19"
|
|
75
|
+
b"\x1a&'()*56789:CDEFGHIJSTUVWXYZcdefghijstuvwxyz\x82\x83\x84\x85\x86\x87"
|
|
76
|
+
b"\x88\x89\x8a\x92\x93\x94\x95\x96\x97\x98\x99\x9a\xa2\xa3\xa4\xa5\xa6\xa7"
|
|
77
|
+
b"\xa8\xa9\xaa\xb2\xb3\xb4\xb5\xb6\xb7\xb8\xb9\xba\xc2\xc3\xc4\xc5\xc6\xc7"
|
|
78
|
+
b"\xc8\xc9\xca\xd2\xd3\xd4\xd5\xd6\xd7\xd8\xd9\xda\xe2\xe3\xe4\xe5\xe6\xe7"
|
|
79
|
+
b"\xe8\xe9\xea\xf2\xf3\xf4\xf5\xf6\xf7\xf8\xf9\xfa"
|
|
80
|
+
b"\xff\xda\x00\x0c\x03\x01\x00\x02\x11\x03\x11\x00?\x00\xfc\x00\x00\xff\xd9"
|
|
81
|
+
)
|
|
82
|
+
return jpeg_data
|
|
83
|
+
|
|
84
|
+
|
|
85
|
+
@pytest.fixture
|
|
86
|
+
def test_jpeg_path(tmp_path, test_jpeg_bytes):
|
|
87
|
+
"""Create a temporary JPEG file."""
|
|
88
|
+
jpeg_file = tmp_path / "cover.jpg"
|
|
89
|
+
jpeg_file.write_bytes(test_jpeg_bytes)
|
|
90
|
+
return jpeg_file
|
|
91
|
+
|
|
92
|
+
|
|
93
|
+
@pytest.fixture
|
|
94
|
+
def test_png_bytes():
|
|
95
|
+
"""
|
|
96
|
+
Generate minimal valid PNG image bytes (1x1 red pixel).
|
|
97
|
+
"""
|
|
98
|
+
png_data = (
|
|
99
|
+
b"\x89PNG\r\n\x1a\n"
|
|
100
|
+
b"\x00\x00\x00\rIHDR\x00\x00\x00\x01\x00\x00\x00\x01\x08\x02\x00\x00\x00\x90wS\xde"
|
|
101
|
+
b"\x00\x00\x00\x0cIDATx\x9cc\xf8\xcf\xc0\x00\x00\x00\x03\x00\x01\x00\x00\x00\x00\xff"
|
|
102
|
+
b"\xff\x03\x00\x00\x06\x00\x05W\xbfh\xd4"
|
|
103
|
+
b"\x00\x00\x00\x00IEND\xaeB`\x82"
|
|
104
|
+
)
|
|
105
|
+
return png_data
|
|
106
|
+
|
|
107
|
+
|
|
108
|
+
@pytest.fixture
|
|
109
|
+
def test_png_path(tmp_path, test_png_bytes):
|
|
110
|
+
"""Create a temporary PNG file."""
|
|
111
|
+
png_file = tmp_path / "cover.png"
|
|
112
|
+
png_file.write_bytes(test_png_bytes)
|
|
113
|
+
return png_file
|
|
@@ -0,0 +1,363 @@
|
|
|
1
|
+
"""Tests for core MP3CoverAdder functionality."""
|
|
2
|
+
|
|
3
|
+
import io
|
|
4
|
+
from pathlib import Path
|
|
5
|
+
import pytest
|
|
6
|
+
|
|
7
|
+
from mp3_cover_adder import (
|
|
8
|
+
MP3CoverAdder,
|
|
9
|
+
read_mp3_metadata,
|
|
10
|
+
set_mp3_cover,
|
|
11
|
+
remove_mp3_cover,
|
|
12
|
+
set_mp3_metadata,
|
|
13
|
+
write_mp3,
|
|
14
|
+
)
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
class TestMP3CoverAdder:
|
|
18
|
+
"""Test the main MP3CoverAdder class."""
|
|
19
|
+
|
|
20
|
+
def test_init_from_path(self, test_mp3_path):
|
|
21
|
+
"""Test initialization from file path."""
|
|
22
|
+
mp3 = MP3CoverAdder(test_mp3_path)
|
|
23
|
+
assert mp3.audio is not None
|
|
24
|
+
assert mp3.audio.tags is not None
|
|
25
|
+
|
|
26
|
+
def test_init_from_bytes(self, test_mp3_bytes):
|
|
27
|
+
"""Test initialization from bytes."""
|
|
28
|
+
mp3 = MP3CoverAdder(test_mp3_bytes)
|
|
29
|
+
assert mp3.audio is not None
|
|
30
|
+
assert mp3.audio.tags is not None
|
|
31
|
+
|
|
32
|
+
def test_init_from_bytesio(self, test_mp3_bytes):
|
|
33
|
+
"""Test initialization from BytesIO."""
|
|
34
|
+
buffer = io.BytesIO(test_mp3_bytes)
|
|
35
|
+
mp3 = MP3CoverAdder(buffer)
|
|
36
|
+
assert mp3.audio is not None
|
|
37
|
+
assert mp3.audio.tags is not None
|
|
38
|
+
|
|
39
|
+
def test_get_metadata_empty(self, test_mp3_path):
|
|
40
|
+
"""Test reading metadata from MP3 without tags."""
|
|
41
|
+
mp3 = MP3CoverAdder(test_mp3_path)
|
|
42
|
+
metadata = mp3.get_metadata()
|
|
43
|
+
assert metadata["title"] is None
|
|
44
|
+
assert metadata["artist"] is None
|
|
45
|
+
assert metadata["album"] is None
|
|
46
|
+
|
|
47
|
+
def test_set_metadata(self, test_mp3_path):
|
|
48
|
+
"""Test setting metadata."""
|
|
49
|
+
mp3 = MP3CoverAdder(test_mp3_path)
|
|
50
|
+
mp3.set_metadata(
|
|
51
|
+
title="Test Song",
|
|
52
|
+
artist="Test Artist",
|
|
53
|
+
album="Test Album"
|
|
54
|
+
)
|
|
55
|
+
metadata = mp3.get_metadata()
|
|
56
|
+
assert metadata["title"] == "Test Song"
|
|
57
|
+
assert metadata["artist"] == "Test Artist"
|
|
58
|
+
assert metadata["album"] == "Test Album"
|
|
59
|
+
|
|
60
|
+
def test_set_metadata_partial(self, test_mp3_path):
|
|
61
|
+
"""Test setting only some metadata fields."""
|
|
62
|
+
mp3 = MP3CoverAdder(test_mp3_path)
|
|
63
|
+
mp3.set_metadata(title="Only Title")
|
|
64
|
+
metadata = mp3.get_metadata()
|
|
65
|
+
assert metadata["title"] == "Only Title"
|
|
66
|
+
assert metadata["artist"] is None
|
|
67
|
+
assert metadata["album"] is None
|
|
68
|
+
|
|
69
|
+
def test_get_cover_no_cover(self, test_mp3_path):
|
|
70
|
+
"""Test getting cover from MP3 without cover."""
|
|
71
|
+
mp3 = MP3CoverAdder(test_mp3_path)
|
|
72
|
+
cover = mp3.get_cover()
|
|
73
|
+
assert cover is None
|
|
74
|
+
|
|
75
|
+
def test_set_cover_from_path_jpeg(self, test_mp3_path, test_jpeg_path):
|
|
76
|
+
"""Test setting cover from JPEG file path."""
|
|
77
|
+
mp3 = MP3CoverAdder(test_mp3_path)
|
|
78
|
+
mp3.set_cover(test_jpeg_path)
|
|
79
|
+
cover = mp3.get_cover()
|
|
80
|
+
assert cover is not None
|
|
81
|
+
assert cover.startswith(b"\xff\xd8")
|
|
82
|
+
|
|
83
|
+
def test_set_cover_from_path_png(self, test_mp3_path, test_png_path):
|
|
84
|
+
"""Test setting cover from PNG file path."""
|
|
85
|
+
mp3 = MP3CoverAdder(test_mp3_path)
|
|
86
|
+
mp3.set_cover(test_png_path)
|
|
87
|
+
cover = mp3.get_cover()
|
|
88
|
+
assert cover is not None
|
|
89
|
+
assert cover.startswith(b"\x89PNG")
|
|
90
|
+
|
|
91
|
+
def test_set_cover_from_bytes_jpeg(self, test_mp3_path, test_jpeg_bytes):
|
|
92
|
+
"""Test setting cover from JPEG bytes."""
|
|
93
|
+
mp3 = MP3CoverAdder(test_mp3_path)
|
|
94
|
+
mp3.set_cover(test_jpeg_bytes)
|
|
95
|
+
cover = mp3.get_cover()
|
|
96
|
+
assert cover is not None
|
|
97
|
+
assert cover.startswith(b"\xff\xd8")
|
|
98
|
+
|
|
99
|
+
def test_set_cover_from_bytes_png(self, test_mp3_path, test_png_bytes):
|
|
100
|
+
"""Test setting cover from PNG bytes."""
|
|
101
|
+
mp3 = MP3CoverAdder(test_mp3_path)
|
|
102
|
+
mp3.set_cover(test_png_bytes)
|
|
103
|
+
cover = mp3.get_cover()
|
|
104
|
+
assert cover is not None
|
|
105
|
+
assert cover.startswith(b"\x89PNG")
|
|
106
|
+
|
|
107
|
+
def test_set_cover_with_explicit_mime(self, test_mp3_path, test_jpeg_bytes):
|
|
108
|
+
"""Test setting cover with explicit MIME type."""
|
|
109
|
+
mp3 = MP3CoverAdder(test_mp3_path)
|
|
110
|
+
mp3.set_cover(test_jpeg_bytes, mime_type="image/jpeg")
|
|
111
|
+
cover = mp3.get_cover()
|
|
112
|
+
assert cover is not None
|
|
113
|
+
|
|
114
|
+
def test_remove_cover(self, test_mp3_path, test_jpeg_bytes):
|
|
115
|
+
"""Test removing cover art."""
|
|
116
|
+
mp3 = MP3CoverAdder(test_mp3_path)
|
|
117
|
+
mp3.set_cover(test_jpeg_bytes)
|
|
118
|
+
assert mp3.get_cover() is not None
|
|
119
|
+
mp3.remove_cover()
|
|
120
|
+
assert mp3.get_cover() is None
|
|
121
|
+
|
|
122
|
+
def test_replace_cover(self, test_mp3_path, test_jpeg_bytes, test_png_bytes):
|
|
123
|
+
"""Test replacing existing cover."""
|
|
124
|
+
mp3 = MP3CoverAdder(test_mp3_path)
|
|
125
|
+
mp3.set_cover(test_jpeg_bytes)
|
|
126
|
+
assert mp3.get_cover().startswith(b"\xff\xd8")
|
|
127
|
+
mp3.set_cover(test_png_bytes)
|
|
128
|
+
assert mp3.get_cover().startswith(b"\x89PNG")
|
|
129
|
+
|
|
130
|
+
def test_write_to_new_file(self, test_mp3_path, tmp_path):
|
|
131
|
+
"""Test writing to a new file."""
|
|
132
|
+
output_path = tmp_path / "output.mp3"
|
|
133
|
+
mp3 = MP3CoverAdder(test_mp3_path)
|
|
134
|
+
mp3.set_metadata(title="Output Test")
|
|
135
|
+
mp3.write(output_path)
|
|
136
|
+
assert output_path.exists()
|
|
137
|
+
|
|
138
|
+
# Verify the written file
|
|
139
|
+
mp3_new = MP3CoverAdder(output_path)
|
|
140
|
+
assert mp3_new.get_metadata()["title"] == "Output Test"
|
|
141
|
+
|
|
142
|
+
def test_write_in_place(self, test_mp3_path):
|
|
143
|
+
"""Test writing changes in place."""
|
|
144
|
+
mp3 = MP3CoverAdder(test_mp3_path)
|
|
145
|
+
mp3.set_metadata(title="In-Place Test")
|
|
146
|
+
mp3.write()
|
|
147
|
+
|
|
148
|
+
# Verify the file was modified
|
|
149
|
+
mp3_new = MP3CoverAdder(test_mp3_path)
|
|
150
|
+
assert mp3_new.get_metadata()["title"] == "In-Place Test"
|
|
151
|
+
|
|
152
|
+
def test_to_bytes(self, test_mp3_path, test_jpeg_bytes):
|
|
153
|
+
"""Test converting to bytes."""
|
|
154
|
+
mp3 = MP3CoverAdder(test_mp3_path)
|
|
155
|
+
mp3.set_metadata(title="Bytes Test")
|
|
156
|
+
mp3.set_cover(test_jpeg_bytes)
|
|
157
|
+
result = mp3.to_bytes()
|
|
158
|
+
assert isinstance(result, bytes)
|
|
159
|
+
assert len(result) > 0
|
|
160
|
+
|
|
161
|
+
# Verify the bytes can be loaded back
|
|
162
|
+
mp3_new = MP3CoverAdder(result)
|
|
163
|
+
assert mp3_new.get_metadata()["title"] == "Bytes Test"
|
|
164
|
+
assert mp3_new.get_cover() is not None
|
|
165
|
+
|
|
166
|
+
|
|
167
|
+
class TestConvenienceFunctions:
|
|
168
|
+
"""Test convenience functions."""
|
|
169
|
+
|
|
170
|
+
def test_read_mp3_metadata_no_tags(self, test_mp3_path):
|
|
171
|
+
"""Test reading metadata with no tags."""
|
|
172
|
+
metadata = read_mp3_metadata(test_mp3_path)
|
|
173
|
+
assert metadata["title"] is None
|
|
174
|
+
assert metadata["artist"] is None
|
|
175
|
+
assert metadata["album"] is None
|
|
176
|
+
assert metadata["has_cover"] is False
|
|
177
|
+
|
|
178
|
+
def test_read_mp3_metadata_with_tags(self, test_mp3_path, test_jpeg_bytes):
|
|
179
|
+
"""Test reading metadata with tags."""
|
|
180
|
+
mp3 = MP3CoverAdder(test_mp3_path)
|
|
181
|
+
mp3.set_metadata(title="Test", artist="Artist")
|
|
182
|
+
mp3.set_cover(test_jpeg_bytes)
|
|
183
|
+
mp3.write()
|
|
184
|
+
|
|
185
|
+
metadata = read_mp3_metadata(test_mp3_path)
|
|
186
|
+
assert metadata["title"] == "Test"
|
|
187
|
+
assert metadata["artist"] == "Artist"
|
|
188
|
+
assert metadata["has_cover"] is True
|
|
189
|
+
|
|
190
|
+
def test_set_mp3_cover_to_file(self, test_mp3_path, test_jpeg_bytes, tmp_path):
|
|
191
|
+
"""Test set_mp3_cover with output path."""
|
|
192
|
+
output_path = tmp_path / "output.mp3"
|
|
193
|
+
set_mp3_cover(test_mp3_path, test_jpeg_bytes, output_path=output_path)
|
|
194
|
+
assert output_path.exists()
|
|
195
|
+
|
|
196
|
+
mp3 = MP3CoverAdder(output_path)
|
|
197
|
+
assert mp3.get_cover() is not None
|
|
198
|
+
|
|
199
|
+
def test_set_mp3_cover_to_bytes(self, test_mp3_path, test_jpeg_bytes):
|
|
200
|
+
"""Test set_mp3_cover returning bytes."""
|
|
201
|
+
result = set_mp3_cover(test_mp3_path, test_jpeg_bytes)
|
|
202
|
+
assert isinstance(result, bytes)
|
|
203
|
+
|
|
204
|
+
mp3 = MP3CoverAdder(result)
|
|
205
|
+
assert mp3.get_cover() is not None
|
|
206
|
+
|
|
207
|
+
def test_remove_mp3_cover_to_file(self, test_mp3_path, test_jpeg_bytes, tmp_path):
|
|
208
|
+
"""Test remove_mp3_cover with output path."""
|
|
209
|
+
mp3 = MP3CoverAdder(test_mp3_path)
|
|
210
|
+
mp3.set_cover(test_jpeg_bytes)
|
|
211
|
+
mp3.write()
|
|
212
|
+
|
|
213
|
+
output_path = tmp_path / "output.mp3"
|
|
214
|
+
remove_mp3_cover(test_mp3_path, output_path=output_path)
|
|
215
|
+
assert output_path.exists()
|
|
216
|
+
|
|
217
|
+
mp3_new = MP3CoverAdder(output_path)
|
|
218
|
+
assert mp3_new.get_cover() is None
|
|
219
|
+
|
|
220
|
+
def test_remove_mp3_cover_to_bytes(self, test_mp3_path, test_jpeg_bytes):
|
|
221
|
+
"""Test remove_mp3_cover returning bytes."""
|
|
222
|
+
mp3 = MP3CoverAdder(test_mp3_path)
|
|
223
|
+
mp3.set_cover(test_jpeg_bytes)
|
|
224
|
+
mp3.write()
|
|
225
|
+
|
|
226
|
+
result = remove_mp3_cover(test_mp3_path)
|
|
227
|
+
assert isinstance(result, bytes)
|
|
228
|
+
|
|
229
|
+
mp3_new = MP3CoverAdder(result)
|
|
230
|
+
assert mp3_new.get_cover() is None
|
|
231
|
+
|
|
232
|
+
def test_set_mp3_metadata_to_file(self, test_mp3_path, tmp_path):
|
|
233
|
+
"""Test set_mp3_metadata with output path."""
|
|
234
|
+
output_path = tmp_path / "output.mp3"
|
|
235
|
+
set_mp3_metadata(
|
|
236
|
+
test_mp3_path,
|
|
237
|
+
output_path=output_path,
|
|
238
|
+
title="Title",
|
|
239
|
+
artist="Artist"
|
|
240
|
+
)
|
|
241
|
+
assert output_path.exists()
|
|
242
|
+
|
|
243
|
+
mp3 = MP3CoverAdder(output_path)
|
|
244
|
+
metadata = mp3.get_metadata()
|
|
245
|
+
assert metadata["title"] == "Title"
|
|
246
|
+
assert metadata["artist"] == "Artist"
|
|
247
|
+
|
|
248
|
+
def test_set_mp3_metadata_to_bytes(self, test_mp3_path):
|
|
249
|
+
"""Test set_mp3_metadata returning bytes."""
|
|
250
|
+
result = set_mp3_metadata(
|
|
251
|
+
test_mp3_path,
|
|
252
|
+
title="Title",
|
|
253
|
+
artist="Artist"
|
|
254
|
+
)
|
|
255
|
+
assert isinstance(result, bytes)
|
|
256
|
+
|
|
257
|
+
mp3 = MP3CoverAdder(result)
|
|
258
|
+
metadata = mp3.get_metadata()
|
|
259
|
+
assert metadata["title"] == "Title"
|
|
260
|
+
assert metadata["artist"] == "Artist"
|
|
261
|
+
|
|
262
|
+
def test_write_mp3_full(self, test_mp3_path, test_jpeg_bytes, tmp_path):
|
|
263
|
+
"""Test write_mp3 with all parameters."""
|
|
264
|
+
output_path = tmp_path / "output.mp3"
|
|
265
|
+
write_mp3(
|
|
266
|
+
test_mp3_path,
|
|
267
|
+
output_path,
|
|
268
|
+
image_source=test_jpeg_bytes,
|
|
269
|
+
title="Full Test",
|
|
270
|
+
artist="Test Artist",
|
|
271
|
+
album="Test Album"
|
|
272
|
+
)
|
|
273
|
+
assert output_path.exists()
|
|
274
|
+
|
|
275
|
+
mp3 = MP3CoverAdder(output_path)
|
|
276
|
+
metadata = mp3.get_metadata()
|
|
277
|
+
assert metadata["title"] == "Full Test"
|
|
278
|
+
assert metadata["artist"] == "Test Artist"
|
|
279
|
+
assert metadata["album"] == "Test Album"
|
|
280
|
+
assert mp3.get_cover() is not None
|
|
281
|
+
|
|
282
|
+
def test_write_mp3_metadata_only(self, test_mp3_path, tmp_path):
|
|
283
|
+
"""Test write_mp3 with metadata only."""
|
|
284
|
+
output_path = tmp_path / "output.mp3"
|
|
285
|
+
write_mp3(
|
|
286
|
+
test_mp3_path,
|
|
287
|
+
output_path,
|
|
288
|
+
title="Metadata Only"
|
|
289
|
+
)
|
|
290
|
+
assert output_path.exists()
|
|
291
|
+
|
|
292
|
+
mp3 = MP3CoverAdder(output_path)
|
|
293
|
+
metadata = mp3.get_metadata()
|
|
294
|
+
assert metadata["title"] == "Metadata Only"
|
|
295
|
+
assert mp3.get_cover() is None
|
|
296
|
+
|
|
297
|
+
def test_write_mp3_cover_only(self, test_mp3_path, test_jpeg_bytes, tmp_path):
|
|
298
|
+
"""Test write_mp3 with cover only."""
|
|
299
|
+
output_path = tmp_path / "output.mp3"
|
|
300
|
+
write_mp3(test_mp3_path, output_path, image_source=test_jpeg_bytes)
|
|
301
|
+
assert output_path.exists()
|
|
302
|
+
|
|
303
|
+
mp3 = MP3CoverAdder(output_path)
|
|
304
|
+
assert mp3.get_cover() is not None
|
|
305
|
+
assert mp3.get_metadata()["title"] is None
|
|
306
|
+
|
|
307
|
+
|
|
308
|
+
class TestEdgeCases:
|
|
309
|
+
"""Test edge cases and error handling."""
|
|
310
|
+
|
|
311
|
+
def test_empty_metadata_strings(self, test_mp3_path):
|
|
312
|
+
"""Test setting empty strings as metadata."""
|
|
313
|
+
mp3 = MP3CoverAdder(test_mp3_path)
|
|
314
|
+
mp3.set_metadata(title="", artist="", album="")
|
|
315
|
+
metadata = mp3.get_metadata()
|
|
316
|
+
# Empty strings should be preserved
|
|
317
|
+
assert metadata["title"] == ""
|
|
318
|
+
assert metadata["artist"] == ""
|
|
319
|
+
assert metadata["album"] == ""
|
|
320
|
+
|
|
321
|
+
def test_unicode_metadata(self, test_mp3_path):
|
|
322
|
+
"""Test setting Unicode characters in metadata."""
|
|
323
|
+
mp3 = MP3CoverAdder(test_mp3_path)
|
|
324
|
+
mp3.set_metadata(
|
|
325
|
+
title="Test 中文 🎵",
|
|
326
|
+
artist="Артист",
|
|
327
|
+
album="Álbum"
|
|
328
|
+
)
|
|
329
|
+
metadata = mp3.get_metadata()
|
|
330
|
+
assert metadata["title"] == "Test 中文 🎵"
|
|
331
|
+
assert metadata["artist"] == "Артист"
|
|
332
|
+
assert metadata["album"] == "Álbum"
|
|
333
|
+
|
|
334
|
+
def test_long_metadata(self, test_mp3_path):
|
|
335
|
+
"""Test setting very long metadata strings."""
|
|
336
|
+
long_title = "A" * 1000
|
|
337
|
+
mp3 = MP3CoverAdder(test_mp3_path)
|
|
338
|
+
mp3.set_metadata(title=long_title)
|
|
339
|
+
metadata = mp3.get_metadata()
|
|
340
|
+
assert metadata["title"] == long_title
|
|
341
|
+
|
|
342
|
+
def test_multiple_operations(self, test_mp3_path, test_jpeg_bytes, test_png_bytes):
|
|
343
|
+
"""Test multiple operations in sequence."""
|
|
344
|
+
mp3 = MP3CoverAdder(test_mp3_path)
|
|
345
|
+
|
|
346
|
+
mp3.set_metadata(title="First")
|
|
347
|
+
assert mp3.get_metadata()["title"] == "First"
|
|
348
|
+
|
|
349
|
+
mp3.set_cover(test_jpeg_bytes)
|
|
350
|
+
assert mp3.get_cover() is not None
|
|
351
|
+
|
|
352
|
+
mp3.set_metadata(artist="Second")
|
|
353
|
+
assert mp3.get_metadata()["artist"] == "Second"
|
|
354
|
+
|
|
355
|
+
mp3.set_cover(test_png_bytes)
|
|
356
|
+
assert mp3.get_cover().startswith(b"\x89PNG")
|
|
357
|
+
|
|
358
|
+
mp3.remove_cover()
|
|
359
|
+
assert mp3.get_cover() is None
|
|
360
|
+
|
|
361
|
+
# Verify metadata persists
|
|
362
|
+
assert mp3.get_metadata()["title"] == "First"
|
|
363
|
+
assert mp3.get_metadata()["artist"] == "Second"
|