Skip to content

Commit

Permalink
feat: add support for MP4 and ID3v2 cover
Browse files Browse the repository at this point in the history
  • Loading branch information
acolombier committed May 21, 2024
1 parent ccd6a40 commit 0954820
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 6 deletions.
4 changes: 4 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
[build-system]
requires = ["setuptools>=61.0", "Cython>=3.0.10"]
build-backend = "setuptools.build_meta"


[tool.licensecheck]
using = "requirements:requirements.txt"
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
demucs @ git+https://github.com/facebookresearch/demucs.git@583db9df0213ba5f5b3491eca5c993e7629f1949#egg=demucs
tagpy @ git+https://github.com/acolombier/tagpy.git@c5de51fe9636b312bfe95dee8b051ab640976d43#egg=tagpy
setuptools>=61.0
pytaglib==3.0.0
ffmpeg-python==0.2.0
torch>=2.1.2
torchaudio>=2.1.2
Expand Down
64 changes: 59 additions & 5 deletions stemgen/nistemfile.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,56 @@
import click

import taglib
import tagpy
import tagpy.id3v2
from torchaudio.io import StreamWriter, CodecConfig
import stembox
import torch

from .constant import SAMPLE_RATE, CHUNK_SIZE

SUPPORTED_TAGS = [
"title",
"artist",
"album",
"comment",
"genre",
"year",
"track",
]


def _extract_cover(f):
tag = None
if isinstance(f, tagpy.FileRef):
tag = f.tag()
f = f.file()
covers = []
if hasattr(tag, "covers"):
covers = tag.covers
elif hasattr(f, "ID3v2Tag"):
covers = [
a
for a in f.ID3v2Tag().frameList()
if isinstance(a, tagpy.id3v2.AttachedPictureFrame)
]

if covers:
cover = covers[0]
fmt = tagpy.mp4.CoverArtFormats.Unknown
if isinstance(cover, tagpy.mp4.CoverArt):
return cover
else:
mime = cover.mimeType().lower().strip()
if "image/jpeg":
fmt = tagpy.mp4.CoverArtFormats.JPEG
elif "image/png":
fmt = tagpy.mp4.CoverArtFormats.PNG
elif "image/bmp":
fmt = tagpy.mp4.CoverArtFormats.BMP
elif "image/gif":
fmt = tagpy.mp4.CoverArtFormats.GIF
return tagpy.mp4.CoverArt(fmt, cover.picture())


class NIStemFile:
STEM_DEFAULT_LABEL = [
Expand Down Expand Up @@ -74,10 +118,20 @@ def write(self, original, stems):
progress.finish()

def update_metadata(self, src, **stem_metadata):
with taglib.File(src) as src, taglib.File(
self.__path, save_on_exit=True
) as dst:
dst.tags = src.tags
src = tagpy.FileRef(src)
dst = tagpy.FileRef(self.__path)

src_tag = src.tag()
dst_tag = dst.tag()
for tag in SUPPORTED_TAGS:
setattr(dst_tag, tag, getattr(src_tag, tag))

cover = _extract_cover(src)
if cover:
c = tagpy.mp4.CoverArtList()
c.append(cover)
dst_tag.covers = c
dst.save()

with stembox.Stem(self.__path) as f:
f.stems = [
Expand Down

0 comments on commit 0954820

Please sign in to comment.