Revert "Revert "Send ffmpeg stderr to /dev/null unless debug=True""

This reverts commit 5233baf451.
This commit is contained in:
Fabio Manganiello 2020-10-29 00:45:41 +01:00
parent 5233baf451
commit bfc8a270d0

View file

@ -1,8 +1,9 @@
import logging import logging
import os
import signal import signal
import subprocess import subprocess
from abc import ABC from abc import ABC
from typing import Optional, Union from typing import Optional, Union, IO
from micmon.audio.segment import AudioSegment from micmon.audio.segment import AudioSegment
@ -12,7 +13,8 @@ class AudioSource(ABC):
sample_duration: float = 2.0, sample_duration: float = 2.0,
sample_rate: int = 44100, sample_rate: int = 44100,
channels: int = 1, channels: int = 1,
ffmpeg_bin: str = 'ffmpeg'): ffmpeg_bin: str = 'ffmpeg',
debug: bool = False):
self.ffmpeg_bin = ffmpeg_bin self.ffmpeg_bin = ffmpeg_bin
self.ffmpeg_base_args = ( self.ffmpeg_base_args = (
'-f', 's16le', '-f', 's16le',
@ -26,7 +28,10 @@ class AudioSource(ABC):
self.sample_duration = sample_duration self.sample_duration = sample_duration
self.sample_rate = sample_rate self.sample_rate = sample_rate
self.channels = channels self.channels = channels
self.debug = debug
self.logger = logging.getLogger(self.__class__.__name__) 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): def __iter__(self):
return self return self
@ -42,7 +47,12 @@ class AudioSource(ABC):
raise StopIteration raise StopIteration
def __enter__(self): 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 return self
def __exit__(self, exc_type, exc_val, exc_tb): def __exit__(self, exc_type, exc_val, exc_tb):
@ -59,6 +69,10 @@ class AudioSource(ABC):
self.ffmpeg.wait() self.ffmpeg.wait()
self.ffmpeg = None self.ffmpeg = None
if self.devnull:
self.devnull.close()
self.devnull = None
def pause(self): def pause(self):
if not self.ffmpeg: if not self.ffmpeg:
return return