Skip to content

Commit

Permalink
Merge pull request #8 from descriptinc/ps/ffmpeg_quiet
Browse files Browse the repository at this point in the history
Silent FFMPEG.
  • Loading branch information
pseeth authored Jul 28, 2021
2 parents bab8788 + 33f3b6f commit 925972c
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 7 deletions.
2 changes: 1 addition & 1 deletion audiotools/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
__version__ = "0.0.6"
__version__ = "0.0.7"
from .core import AudioSignal, STFTParams, Meter, util
from . import metrics
12 changes: 8 additions & 4 deletions audiotools/core/ffmpeg.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import torch


def r128stats(filepath):
def r128stats(filepath, quiet):
"""takes a path to an audio file, returns a dict with the loudness
stats computed by the ffmpeg ebur128 filter. Taken lovingly from Scaper."""
ffargs = [
Expand All @@ -20,6 +20,8 @@ def r128stats(filepath):
"null",
"-",
]
if quiet:
ffargs += ["-hide_banner"]
proc = subprocess.Popen(ffargs, stderr=subprocess.PIPE, universal_newlines=True)
stats = proc.communicate()[1]
summary_index = stats.rfind("Summary:")
Expand All @@ -46,19 +48,19 @@ def r128stats(filepath):
class FFMPEGMixin:
_loudness = None

def ffmpeg_loudness(self):
def ffmpeg_loudness(self, quiet=True):
loudness = []

with tempfile.NamedTemporaryFile(suffix=".wav") as f:
for i in range(self.batch_size):
self.write(f.name, i)
loudness_stats = r128stats(f.name)
loudness_stats = r128stats(f.name, quiet=quiet)
loudness.append(loudness_stats["I"])

self._loudness = torch.from_numpy(np.array(loudness))
return self.loudness()

def ffmpeg_resample(self, sample_rate):
def ffmpeg_resample(self, sample_rate, quiet=True):
from audiotools import AudioSignal

if sample_rate == self.sample_rate:
Expand All @@ -68,6 +70,8 @@ def ffmpeg_resample(self, sample_rate):
self.write(f.name)
f_out = f.name.replace("wav", "rs.wav")
command = f"ffmpeg -i {f.name} -ar {sample_rate} {f_out}"
if quiet:
command += " -hide_banner -loglevel error"
subprocess.check_call(shlex.split(command))
resampled = AudioSignal(f_out)
return resampled
2 changes: 1 addition & 1 deletion audiotools/core/playback.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ def embed(self, batch_idx=0, ext=".mp3", display=True):
ff = ffmpy.FFmpeg(
inputs={tmp_wav.name: None},
outputs={
tmp_converted.name: "-write_xing 0 -codec:a libmp3lame -b:a 128k -y"
tmp_converted.name: "-write_xing 0 -codec:a libmp3lame -b:a 128k -y -hide_banner -loglevel error"
},
)
ff.run()
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

setup(
name="audiotools",
version="0.0.6",
version="0.0.7",
classifiers=[
"Intended Audience :: Developers",
"Intended Audience :: Education",
Expand Down

0 comments on commit 925972c

Please sign in to comment.