Skip to content

Instantly share code, notes, and snippets.

@gau-nernst
Created September 17, 2023 03:39
Show Gist options
  • Save gau-nernst/a1e60fde12b74a1690235f26424362be to your computer and use it in GitHub Desktop.
Save gau-nernst/a1e60fde12b74a1690235f26424362be to your computer and use it in GitHub Desktop.
Read audio with ffmpeg for PyTorch
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