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

This commit is contained in:
Fabio Manganiello 2020-10-28 23:16:09 +01:00
parent ebad3c366e
commit ab431bc2f8

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