|
1 | 1 | import click
|
2 | 2 |
|
3 |
| -import taglib |
| 3 | +import tagpy |
| 4 | +import tagpy.id3v2 |
4 | 5 | from torchaudio.io import StreamWriter, CodecConfig
|
5 | 6 | import stembox
|
6 | 7 | import torch
|
7 | 8 |
|
8 | 9 | from .constant import SAMPLE_RATE, CHUNK_SIZE
|
9 | 10 |
|
| 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 | + |
10 | 54 |
|
11 | 55 | class NIStemFile:
|
12 | 56 | STEM_DEFAULT_LABEL = [
|
@@ -74,10 +118,20 @@ def write(self, original, stems):
|
74 | 118 | progress.finish()
|
75 | 119 |
|
76 | 120 | 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() |
81 | 135 |
|
82 | 136 | with stembox.Stem(self.__path) as f:
|
83 | 137 | f.stems = [
|
|
0 commit comments