From bfc8a270d045f5b8a13093eeb55386aa343c1c8d Mon Sep 17 00:00:00 2001 From: Fabio Manganiello Date: Thu, 29 Oct 2020 00:45:41 +0100 Subject: [PATCH] Revert "Revert "Send ffmpeg stderr to /dev/null unless debug=True"" This reverts commit 5233baf4515620ec6cb319dc71c2eea135beb3c6. --- micmon/audio/source.py | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/micmon/audio/source.py b/micmon/audio/source.py index f9f1754..ee2ff70 100644 --- a/micmon/audio/source.py +++ b/micmon/audio/source.py @@ -1,8 +1,9 @@ import logging +import os import signal import subprocess from abc import ABC -from typing import Optional, Union +from typing import Optional, Union, IO from micmon.audio.segment import AudioSegment @@ -12,7 +13,8 @@ class AudioSource(ABC): sample_duration: float = 2.0, sample_rate: int = 44100, channels: int = 1, - ffmpeg_bin: str = 'ffmpeg'): + ffmpeg_bin: str = 'ffmpeg', + debug: bool = False): self.ffmpeg_bin = ffmpeg_bin self.ffmpeg_base_args = ( '-f', 's16le', @@ -26,7 +28,10 @@ class AudioSource(ABC): self.sample_duration = sample_duration self.sample_rate = sample_rate self.channels = channels + self.debug = debug self.logger = logging.getLogger(self.__class__.__name__) + self.logger.setLevel(logging.DEBUG if self.debug else logging.INFO) + self.devnull: Optional[IO] = None def __iter__(self): return self @@ -42,7 +47,12 @@ class AudioSource(ABC): raise StopIteration def __enter__(self): - self.ffmpeg = subprocess.Popen(self.ffmpeg_args, stdout=subprocess.PIPE) + kwargs = dict(stdout=subprocess.PIPE) + if not self.debug: + self.devnull = open(os.devnull, 'w') + kwargs['stderr'] = self.devnull + + self.ffmpeg = subprocess.Popen(self.ffmpeg_args, **kwargs) return self def __exit__(self, exc_type, exc_val, exc_tb): @@ -59,6 +69,10 @@ class AudioSource(ABC): self.ffmpeg.wait() self.ffmpeg = None + if self.devnull: + self.devnull.close() + self.devnull = None + def pause(self): if not self.ffmpeg: return