Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added _read_mp4box_stem_titles function #46

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .github/workflows/test_unittests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ jobs:
env:
FFMPEG_INSTALL: ${{ matrix.pytorch-version }}
run: |
sudo apt-get update
sudo apt-get -y install gpac
conda install -c conda-forge ffmpeg==${{ matrix.ffmpeg-version }}
python -m pip install -e .['tests']
Expand All @@ -40,4 +41,4 @@ jobs:
run: conda list
- name: Run tests
run: |
py.test tests -v
py.test tests -v
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ envffmpeg/
.vscode/
.circleci/

# Pycharm
.idea/

#####=== OSX ===#####
.DS_Store
Expand Down
49 changes: 48 additions & 1 deletion stempeg/read.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@


"""
import codecs
import json
import os
import subprocess

from stempeg.write import FilesWriter
import numpy as np
import warnings
Expand All @@ -13,6 +18,8 @@
import atexit
from functools import partial
import datetime as dt
from .cmds import mp4box_exists, find_cmd


class Reader(object):
"""Base class for reader
Expand Down Expand Up @@ -287,7 +294,7 @@ def read_stems(
]
stem_durations = np.array([t.shape[0] for t in stems])
if not (stem_durations == stem_durations[0]).all():
warnings.warning("Stems differ in length and were shortend")
warnings.warn("Stems differ in length and were shortend")
min_length = np.min(stem_durations)
stems = [t[:min_length, :] for t in stems]

Expand Down Expand Up @@ -321,6 +328,11 @@ def __init__(self, filename):
stream for stream in self.info['streams']
if stream['codec_type'] == 'audio'
]
# Try to get the stem titles using MP4Box if possible, otherwise fall back to numbered stems
stem_titles = _read_mp4box_stem_titles(filename)
if stem_titles is not None:
for i, stream in enumerate(self.audio_streams):
stream['tags']['handler_name'] = stem_titles[i]

@property
def nb_audio_streams(self):
Expand Down Expand Up @@ -385,3 +397,38 @@ def channels(self, idx):
def __repr__(self):
"""Print stream information"""
return pprint.pformat(self.audio_streams)


def _read_mp4box_stem_titles(filename):
"""Reads a mp4 stem titles file using MP4Box
Mainly taken from https://github.com/axeldelafosse/stemgen/blob/master/ni-stem/_internal.py
"""
stem_titles = None
if mp4box_exists():

mp4box = find_cmd("MP4Box")

try:
callArgs = [mp4box]
callArgs.extend(["-dump-udta", "0:stem", filename, '-quiet'])
subprocess.check_call(callArgs)

except subprocess.CalledProcessError as e:
return None

try:
root, ext = os.path.splitext(filename)
udtaFile = root + "_stem.udta"
fileObj = codecs.open(udtaFile, encoding="utf-8")
# fileObj.seek(8) # Not sure why in the original code this is needed?
_metadata = json.load(fileObj)
os.remove(udtaFile)

# add the mixture stem first since its index is 0 as per rest of the project
stem_titles = ['Mixture'] + [d['name'] for d in _metadata['stems']]

except FileNotFoundError as e:
return None

return stem_titles

2 changes: 1 addition & 1 deletion stempeg/write.py
Original file line number Diff line number Diff line change
Expand Up @@ -758,7 +758,7 @@ def write_stems(
"""
# check if ffmpeg installed
if int(stempeg.ffmpeg_version()[0]) < 3:
warnings.warning(
warnings.warn(
"Writing stems with FFMPEG version < 3 is unsupported",
UserWarning
)
Expand Down
6 changes: 6 additions & 0 deletions tests/test_read.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,3 +104,9 @@ def test_info():
fp = stempeg.example_stem_path()
info = stempeg.Info(fp)
S, rate = stempeg.read_stems(fp, info=info)


def test_info_stem_titles():
fp = stempeg.example_stem_path()
info = stempeg.Info(fp)
assert info.title_streams == ['Mixture', 'Drums', 'Bass', 'Other', 'Vox']