Created
September 17, 2023 03:39
-
-
Save gau-nernst/a1e60fde12b74a1690235f26424362be to your computer and use it in GitHub Desktop.
Read audio with ffmpeg for PyTorch
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import subprocess | |
import torch | |
def load_audio(path: str, sample_rate: int) -> torch.Tensor: | |
cmd = f"{FFMPEG_PATH} -i {path} -ar {sample_rate} -ac 1 -f s32le -" | |
proc = subprocess.run(shlex.split(cmd), capture_output=True) | |
if proc.returncode: | |
raise RuntimeError(proc.stderr.decode()) | |
return torch.frombuffer(proc.stdout, dtype=torch.int32) / 2_147_483_648 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment