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
1 changed files with 17 additions and 3 deletions

View File

@ -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