Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce extra_input_cmd to be able to specify input-side parameters #40

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion haffmpeg/camera.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class CameraMjpeg(HAFFmpeg):
"""Implement a camera they convert video stream to MJPEG."""

def open_camera(
self, input_source: str, extra_cmd: Optional[str] = None
self, input_source: str, extra_cmd: Optional[str] = None, extra_input_cmd: Optional[str] = None
) -> Coroutine:
"""Open FFmpeg process as mjpeg video stream.

Expand All @@ -21,4 +21,5 @@ def open_camera(
input_source=input_source,
output="-f mpjpeg -",
extra_cmd=extra_cmd,
extra_input_cmd=extra_input_cmd
)
20 changes: 17 additions & 3 deletions haffmpeg/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,16 +44,21 @@ def _generate_ffmpeg_cmd(
input_source: Optional[str],
output: Optional[str],
extra_cmd: Optional[str] = None,
extra_input_cmd: Optional[str] = None
) -> None:
"""Generate ffmpeg command line."""
self._argv = [self._ffmpeg]

# exists an extra cmd from customer
if extra_input_cmd is not None:
self._argv.extend(shlex.split(extra_input_cmd))

# start command init
if input_source is not None:
self._put_input(input_source)
self._argv.extend(cmd)

# exists a extra cmd from customer
# exists an extra cmd from customer
if extra_cmd is not None:
self._argv.extend(shlex.split(extra_cmd))

Expand Down Expand Up @@ -108,8 +113,9 @@ async def open(
input_source: Optional[str],
output: Optional[str] = "-",
extra_cmd: Optional[str] = None,
extra_input_cmd=None,
stdout_pipe: bool = True,
stderr_pipe: bool = False,
stderr_pipe: bool = False
) -> bool:
"""Start a ffmpeg instance and pipe output."""
stdout = subprocess.PIPE if stdout_pipe else subprocess.DEVNULL
Expand All @@ -120,7 +126,13 @@ async def open(
return True

# set command line
self._generate_ffmpeg_cmd(cmd, input_source, output, extra_cmd)
self._generate_ffmpeg_cmd(
cmd=cmd,
input_source=input_source,
output=output,
extra_cmd=extra_cmd,
extra_input_cmd=extra_input_cmd,
)

# start ffmpeg
_LOGGER.debug("Start FFmpeg with %s", str(self._argv))
Expand Down Expand Up @@ -248,6 +260,7 @@ async def start_worker(
input_source: str,
output: Optional[str] = None,
extra_cmd: Optional[str] = None,
extra_input_cmd=None,
pattern: Optional[str] = None,
reading: str = FFMPEG_STDERR,
) -> None:
Expand All @@ -269,6 +282,7 @@ async def start_worker(
input_source=input_source,
output=output,
extra_cmd=extra_cmd,
extra_input_cmd=extra_input_cmd,
stdout_pipe=stdout,
stderr_pipe=stderr,
)
Expand Down
5 changes: 4 additions & 1 deletion haffmpeg/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ def open_sensor(
input_source: str,
output_dest: Optional[str] = None,
extra_cmd: Optional[str] = None,
extra_input_cmd: Optional[str] = None,
) -> Coroutine:
"""Open FFmpeg process for read autio stream.

Expand All @@ -55,6 +56,7 @@ def open_sensor(
input_source=input_source,
output=output_dest,
extra_cmd=extra_cmd,
extra_input_cmd=extra_input_cmd,
pattern="silence",
)

Expand Down Expand Up @@ -150,7 +152,7 @@ def set_options(
self._changes = changes

async def open_sensor(
self, input_source: str, extra_cmd: Optional[str] = None
self, input_source: str, extra_cmd: Optional[str] = None, extra_input_cmd: Optional[str] = None
) -> Coroutine:
"""Open FFmpeg process a video stream for motion detection.

Expand All @@ -168,6 +170,7 @@ async def open_sensor(
input_source=input_source,
output="-f framemd5 -",
extra_cmd=extra_cmd,
extra_input_cmd=extra_input_cmd,
pattern=self.MATCH,
reading=FFMPEG_STDOUT,
)
Expand Down
2 changes: 2 additions & 0 deletions haffmpeg/tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ async def get_image(
input_source: str,
output_format: str = IMAGE_JPEG,
extra_cmd: Optional[str] = None,
extra_input_cmd: Optional[str] = None,
timeout: int = 15,
) -> Optional[bytes]:
"""Open FFmpeg process as capture 1 frame."""
Expand All @@ -32,6 +33,7 @@ async def get_image(
input_source=input_source,
output="-f image2pipe -",
extra_cmd=extra_cmd,
extra_input_cmd=extra_input_cmd,
)

# error after open?
Expand Down
5 changes: 3 additions & 2 deletions test/camera_mjpeg.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,14 @@
@click.option("--source", "-s", help="Input file for ffmpeg")
@click.option("--output", "-o", help="Output image path")
@click.option("--extra", "-e", help="Extra ffmpeg command line arguments")
def cli(ffmpeg, source, output, extra):
@click.option("--extra-input", "-E", help="Extra ffmpeg command line arguments for input")
def cli(ffmpeg, source, output, extra, extra_input):
"""FFMPEG capture frame as image."""

async def read_stream():
"""Read stream inside loop."""
stream = CameraMjpeg(ffmpeg_bin=ffmpeg)
await stream.open_camera(source, extra)
await stream.open_camera(input_source=source, extra_cmd=extra, extra_input_cmd=extra_input)

reader = await stream.get_reader()
try:
Expand Down
5 changes: 3 additions & 2 deletions test/sensor_motion.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@
help="Scene change settings or percent of image they need change",
)
@click.option("--extra", "-e", help="Extra ffmpeg command line arguments")
def cli(ffmpeg, source, reset, repeat_time, repeat, changes, extra):
@click.option("--extra-input", "-E", help="Extra ffmpeg command line arguments for input")
def cli(ffmpeg, source, reset, repeat_time, repeat, changes, extra, extra_input):
"""FFMPEG noise detection."""

def callback(state):
Expand All @@ -52,7 +53,7 @@ async def run():
sensor.set_options(
time_reset=reset, changes=changes, repeat=repeat, time_repeat=repeat_time
)
await sensor.open_sensor(input_source=source, extra_cmd=extra)
await sensor.open_sensor(input_source=source, extra_cmd=extra, extra_input_cmd=extra_input)
try:
while True:
await asyncio.sleep(0.1)
Expand Down
5 changes: 3 additions & 2 deletions test/sensor_noise.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@
"--peak", "-p", default=-30, type=int, help="dB for detect a peak. Default -30"
)
@click.option("--extra", "-e", help="Extra ffmpeg command line arguments")
def cli(ffmpeg, source, output, duration, reset, peak, extra):
@click.option("--extra-input", "-E", help="Extra ffmpeg command line arguments for input")
def cli(ffmpeg, source, output, duration, reset, peak, extra, extra_input):
"""FFMPEG noise detection."""

def callback(state):
Expand All @@ -41,7 +42,7 @@ async def run():
sensor = SensorNoise(ffmpeg_bin=ffmpeg, callback=callback)
sensor.set_options(time_duration=duration, time_reset=reset, peak=peak)
await sensor.open_sensor(
input_source=source, output_dest=output, extra_cmd=extra
input_source=source, output_dest=output, extra_cmd=extra, extra_input_cmd=extra_input
)
try:
while True:
Expand Down
5 changes: 3 additions & 2 deletions test/tools_imageframe.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,14 @@
@click.option("--format_img", "-f", default=IMAGE_JPEG, help="Image output format")
@click.option("--output", "-o", required=True, help="Output image file")
@click.option("--extra", "-e", help="Extra ffmpeg command line arguments")
def cli(ffmpeg, source, format_img, output, extra):
@click.option("--extra-input", "-E", help="Extra ffmpeg command line arguments for input")
def cli(ffmpeg, source, format_img, output, extra, extra_input):
"""FFMPEG capture frame as image."""

async def capture_image():
stream = ImageFrame(ffmpeg_bin=ffmpeg)
return await stream.get_image(
input_source=source, output_format=format_img, extra_cmd=extra
input_source=source, output_format=format_img, extra_cmd=extra, extra_input_cmd=extra_input
)

image = asyncio.run(capture_image())
Expand Down