Skip to content

Commit 4b28236

Browse files
authored
Merge pull request #3 from acolombier/feat/add-cover-support
feat: add support for MP4 and ID3v2 cover
2 parents ccd6a40 + cf16735 commit 4b28236

File tree

8 files changed

+77
-16
lines changed

8 files changed

+77
-16
lines changed

.github/ISSUE_TEMPLATE/bug.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ body:
1919
attributes:
2020
label: Version
2121
description: What version of stemgen are you running?
22-
placeholder: 0.1.0, main, ...
22+
placeholder: 0.2.0, main, ...
2323
- type: input
2424
id: os
2525
attributes:

Dockerfile

+7-4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
FROM python:3.10 as build
1+
FROM python:3.11 as build
22
WORKDIR /build
33
RUN wget -O taglib.tar.gz https://github.com/taglib/taglib/releases/download/v2.0.1/taglib-2.0.1.tar.gz && \
44
tar xf taglib.tar.gz && \
@@ -12,13 +12,16 @@ RUN python3 -m build --wheel && \
1212
find /usr/lib -name libtag.so.2.0.1 -exec cp '{}' /build/libtag.so.2.0.1 \;
1313

1414

15-
FROM python:3.10
15+
FROM python:3.11
1616
COPY --from=build /build/libtag.so.2.0.1 /usr/local/lib/libtag.so.2.0.1
1717
COPY --from=build /build/dist/stemgen-*.whl /tmp/
1818
COPY --from=build /usr/include/taglib/ /usr/include/taglib/
1919
RUN ln -s /usr/local/lib/libtag.so.2.0.1 /usr/local/lib/libtag.so && \
20+
apt update && apt install -y ffmpeg libboost-python1.74-dev libboost-python1.74.0 && \
2021
python -m pip install /tmp/stemgen-*.whl && \
21-
apt update && apt install -y ffmpeg && \
2222
pip install --upgrade --force torchaudio && \
23-
rm -rf /root/.cache /tmp/* && rm -rf /usr/include/taglib/
23+
apt-get purge -y libboost-python1.74-dev && \
24+
apt-get clean autoclean && \
25+
apt-get autoremove --yes && \
26+
rm -rf /root/.cache /tmp/* && rm -rf /usr/include/taglib/ && rm -rf /var/lib/{apt,dpkg,cache,log}/
2427
CMD ["/usr/local/bin/stemgen"]

README.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ Under the hood, it uses:
3636
> isn't supported
3737
3838
```sh
39-
pip install -e "git+https://github.com/acolombier/stemgen.git@0.1.0#egg=stemgen"
39+
pip install -e "git+https://github.com/acolombier/stemgen.git@0.2.0#egg=stemgen"
4040
```
4141

4242
### Ubuntu 22.04 / Debian Bookworm / PopOS 22.04
@@ -191,7 +191,7 @@ container. Here the simple way to use it:
191191
docker run \
192192
-v /path/to/folder:/path/to/folder \
193193
-it --rm \
194-
aclmb/stemgen:0.1.0 \
194+
aclmb/stemgen:0.2.0 \
195195
/path/to/folder/Artist\ -\ Title.mp3 \
196196
/path/to/folder
197197
```
@@ -204,7 +204,7 @@ docker run \
204204
-v /path/to/folder:/path/to/folder \
205205
-v stemgen_torch_cache:/root/.cache/torch/hub/ \
206206
-it --gpus --rm \
207-
aclmb/stemgen:0.1.0 \
207+
aclmb/stemgen:0.2.0 \
208208
/path/to/folder/Artist\ -\ Title.mp3 \
209209
/path/to/folder
210210
```

pyproject.toml

+4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
11
[build-system]
22
requires = ["setuptools>=61.0", "Cython>=3.0.10"]
33
build-backend = "setuptools.build_meta"
4+
5+
6+
[tool.licensecheck]
7+
using = "requirements:requirements.txt"

requirements.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
demucs @ git+https://github.com/facebookresearch/demucs.git@583db9df0213ba5f5b3491eca5c993e7629f1949#egg=demucs
2+
tagpy @ git+https://github.com/acolombier/tagpy.git@c5de51fe9636b312bfe95dee8b051ab640976d43#egg=tagpy
23
setuptools>=61.0
3-
pytaglib==3.0.0
44
ffmpeg-python==0.2.0
55
torch>=2.1.2
66
torchaudio>=2.1.2

setup.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
URL = "https://github.com/acolombier/stemgen"
1414
EMAIL = "stemgen@acolombier.dev"
1515
AUTHOR = "Antoine Colombier"
16-
REQUIRES_PYTHON = ">=3.10.0"
16+
REQUIRES_PYTHON = ">=3.11.0"
1717

1818
# Get version without explicitly loading the module.
1919
for line in open("stemgen/__init__.py"):

stemgen/__init__.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
__version__ = "0.1.0"
1+
__version__ = "0.2.0"

stemgen/nistemfile.py

+59-5
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,56 @@
11
import click
22

3-
import taglib
3+
import tagpy
4+
import tagpy.id3v2
45
from torchaudio.io import StreamWriter, CodecConfig
56
import stembox
67
import torch
78

89
from .constant import SAMPLE_RATE, CHUNK_SIZE
910

11+
SUPPORTED_TAGS = [
12+
"title",
13+
"artist",
14+
"album",
15+
"comment",
16+
"genre",
17+
"year",
18+
"track",
19+
]
20+
21+
22+
def _extract_cover(f):
23+
tag = None
24+
if isinstance(f, tagpy.FileRef):
25+
tag = f.tag()
26+
f = f.file()
27+
covers = []
28+
if hasattr(tag, "covers"):
29+
covers = tag.covers
30+
elif hasattr(f, "ID3v2Tag"):
31+
covers = [
32+
a
33+
for a in f.ID3v2Tag().frameList()
34+
if isinstance(a, tagpy.id3v2.AttachedPictureFrame)
35+
]
36+
37+
if covers:
38+
cover = covers[0]
39+
fmt = tagpy.mp4.CoverArtFormats.Unknown
40+
if isinstance(cover, tagpy.mp4.CoverArt):
41+
return cover
42+
else:
43+
mime = cover.mimeType().lower().strip()
44+
if "image/jpeg":
45+
fmt = tagpy.mp4.CoverArtFormats.JPEG
46+
elif "image/png":
47+
fmt = tagpy.mp4.CoverArtFormats.PNG
48+
elif "image/bmp":
49+
fmt = tagpy.mp4.CoverArtFormats.BMP
50+
elif "image/gif":
51+
fmt = tagpy.mp4.CoverArtFormats.GIF
52+
return tagpy.mp4.CoverArt(fmt, cover.picture())
53+
1054

1155
class NIStemFile:
1256
STEM_DEFAULT_LABEL = [
@@ -74,10 +118,20 @@ def write(self, original, stems):
74118
progress.finish()
75119

76120
def update_metadata(self, src, **stem_metadata):
77-
with taglib.File(src) as src, taglib.File(
78-
self.__path, save_on_exit=True
79-
) as dst:
80-
dst.tags = src.tags
121+
src = tagpy.FileRef(src)
122+
dst = tagpy.FileRef(self.__path)
123+
124+
src_tag = src.tag()
125+
dst_tag = dst.tag()
126+
for tag in SUPPORTED_TAGS:
127+
setattr(dst_tag, tag, getattr(src_tag, tag))
128+
129+
cover = _extract_cover(src)
130+
if cover:
131+
c = tagpy.mp4.CoverArtList()
132+
c.append(cover)
133+
dst_tag.covers = c
134+
dst.save()
81135

82136
with stembox.Stem(self.__path) as f:
83137
f.stems = [

0 commit comments

Comments
 (0)