towhee
/
audio-decoder
copied
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Readme
Files and versions
29 lines
819 B
29 lines
819 B
import soundfile as sf
|
|
import numpy as np
|
|
from typing import Generator, NamedTuple
|
|
|
|
import av
|
|
|
|
from towhee.operator.base import Operator
|
|
from torch_vggish import TorchVggish
|
|
|
|
# from torch_vggish import TorchVggish
|
|
|
|
AudioOutput = NamedTuple("Outputs", [("audio_frame", 'ndarray'), ('sample_rate', 'int'), ("TIMESTAMP", 'int')])
|
|
|
|
|
|
class AudioDecoder(Operator):
|
|
"""
|
|
"""
|
|
|
|
def __init__(self) -> None:
|
|
super().__init__()
|
|
|
|
def __call__(self, audio_path: str):
|
|
in_container = av.open(audio_path)
|
|
stream = in_container.streams.get(audio=0)[0]
|
|
for frame in in_container.decode(stream):
|
|
timestamp = int(frame.time * 1000)
|
|
sample_rate = frame.sample_rate
|
|
ndarray = frame.to_ndarray()
|
|
yield AudioOutput(ndarray, sample_rate, timestamp)
|